【问题标题】:OpenCV sub-image from a Mat image [duplicate]来自 Mat 图像的 OpenCV 子图像 [重复]
【发布时间】:2012-10-07 13:14:35
【问题描述】:

可能重复:
Understanding region of interest in 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);
}

【问题讨论】:

  • 这个问题已经被问及回答了,至少herethere

标签: c++ image image-processing opencv contour


【解决方案1】:

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

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

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

希望对你有帮助 祝你好运

【讨论】:

  • 谢谢!我得到了一个具有正确输出但还包括其他轮廓的输出。我使用 RETR_EXTERNAL 而不是 RETR_TREE 以便轮廓更小。我如何确定哪个轮廓是正确的?
  • @OgNamdik 您可以遍历轮廓并计算每个边界矩形的面积或面积(或其他参数)。在您的情况下,您似乎可以简单地保留面积最大的轮廓......此外,如果您对它感到满意,请不要犹豫接受答案。 :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 2020-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多