C++ 来自 Mat 图像的 OpenCV 子图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12931621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:46:44  来源:igfitidea点击:

OpenCV sub-image from a Mat image

c++imageimage-processingopencvcontour

提问by Og Namdik

Possible Duplicate:
Understanding region of interest in openCV 2.4

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

i want to get a sub-image (the one bounded by the red box below) from an image (Mat format). how do i do this?

我想从图像(Mat 格式)中获取子图像(由下面的红色框包围的图像)。我该怎么做呢?

enter image description here

在此处输入图片说明

here's my progress so far:

这是我到目前为止的进展:

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);
}

回答by Quentin Geissmann

You can start picking a contour (in your case, the contour corresponding to the hand). Then, you calculate the bounding rectangle for this contour. Finally you make a new matrix header from it.

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

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);

Warning:In this case, miniMat is a subregion of imgray. This means that if you modify the former, you also modify the latter. Use miniMat.copyTo(anotherMat)to avoid this.

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

I hope it helped, Good luck

我希望它有帮助,祝你好运