来自Mat图像的OpenCV子图像

Og *_*dik 12 c++ opencv image image-processing contour

可能重复:
了解openCV 2.4中感兴趣的区域

我想从图像(Mat格式)获得一个子图像(由下面的红框限定).我该怎么做呢?

在此输入图像描述

这是我目前的进展:

include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;
int main()
{
    Mat imgray, thresh;
    vector<vector<Point> >contours;
    vector<Point> cnt;
    vector<Vec4i> hierarchy;
    Point leftmost;

    Mat im = imread("igoy1.jpg");
    cvtColor(im, imgray, COLOR_BGR2GRAY);
    threshold(imgray, thresh, 127, 255, 0);
    findContours(thresh, contours, hierarchy, RETR_TREE,CHAIN_APPROX_SIMPLE);
}
Run Code Online (Sandbox Code Playgroud)

Que*_*ann 28

您可以开始选择轮廓(在您的情况下,对应于手的轮廓).然后,计算此轮廓的边界矩形.最后,你从中创建一个新的矩阵标题.

int n=0;// Here you will need to define n differently (for instance pick the largest contour instead of the first one)
cv::Rect rect(contours[n]);
cv::Mat miniMat;
miniMat = imgray(rect);
Run Code Online (Sandbox Code Playgroud)

警告:在这种情况下,miniMat是一个imgray的子​​区域.这意味着如果修改前者,也可以修改后者.使用miniMat.copyTo(anotherMat)避免这种情况.

祝它好运,祝你好运