首页 > 解决方案 > 从轮廓绘制垫子?

问题描述

我使用 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      
    }
}

任何帮助如何继续将不胜感激,因为我绝对一无所知。

标签: c++algorithmopencvmathcontour

解决方案


尝试这个。为简单起见 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;

推荐阅读