【问题标题】:Draw mat from contour?从轮廓绘制垫子?
【发布时间】:2019-01-24 15:29:50
【问题描述】:

我使用 openCV 来识别轮廓。现在我想创建一个包含此轮廓所有坐标的新二进制垫。

  1. 应用 Canny 边缘检测
  2. 找到轮廓(红色的是我要使用的)
  3. 只是轮廓内的坐标被绘制到新的垫子中

这是我目前得到的:

vector<cv::Point> contour; // red marked contour;
cv::Rect boundingBox = cv::boundingRect(contour);

Mat newMat;
vector<cv::Point> insideContour;
for (int i=0; i<contour.size(); i++) {
    // get all coordinates inside of contour
    // insideContour.push_back(?)
}

for (int y=0; y<boundingBox.height; y++) {
    for (int x=0; x<boundingBox.width; x++) {
       // newMat      
    }
}

对于如何继续进行的任何帮助将不胜感激,因为我完全一无所知。

【问题讨论】:

  • 如果您想要的是 ROI,那么只需使用边界框来裁剪图像。更难的方法:如果所选轮廓是非矩形的,则获取轮廓的二进制图像(洪水填充应该起作用)并通过使用二进制图像作为键控器从原始图像有选择地深度复制来创建新的垫子。
  • 您介意分享一些与您的评论相匹配的行吗? @seccpur
  • @jonas00 你能分享一下你自己应用 Canny 边缘检测后的图像吗?它会让帮助你更容易。此外,您用于专门查找图像轮廓的代码也会有所帮助。这样可以减少我需要多少修修补补来获得你想要的轮廓。

标签: c++ algorithm opencv math contour


【解决方案1】:

试试这个。为简单起见 cv::Point(250, 219) 是红色轮廓内的一个点,使用 Haar 找到边界框,它实际上是中心。

cv::Mat image = imread("Smiley.jpg");
cv::Mat image2 = imread("Smiley2.jpg");

// subtract images and floodfill to prepare red mask 
Mat red_contour, red_mask, maskMat, outputMat;
subtract(image2, image, red_contour);   
threshold(red_contour, red_mask, 100, 255, THRESH_BINARY);
int filling = cv::floodFill(red_mask, cv::Point(250, 219), cv::Scalar(0, 0, 255), (cv::Rect*)0, cv::Scalar(), cv::Scalar(), 4);

//prepare a grey mask
cv::cvtColor(red_mask, maskMat, CV_BGR2GRAY);
threshold(maskMat, maskMat, 0, 255, THRESH_BINARY);

// use mask to crop original image
image.copyTo(outputMat, maskMat);


cv::namedWindow("Image");
cv::imshow("Image", outputMat);

cv::waitKey();

return 0;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 2021-09-07
    • 2018-05-13
    • 2023-03-20
    • 2018-07-09
    • 2021-01-17
    相关资源
    最近更新 更多