c++ - 从轮廓绘制垫子?
问题描述
我使用 openCV 来识别轮廓。现在我想创建一个包含此轮廓所有坐标的新二进制垫。
- 应用 Canny 边缘检测
- 找到轮廓的(红色的是我想要使用的)
- 只是轮廓内的坐标被绘制到新的垫子中
这是我到目前为止所得到的:
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
}
}
任何帮助如何继续将不胜感激,因为我绝对一无所知。
解决方案
尝试这个。为简单起见 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;
推荐阅读
- javascript - 如何让 VS Code Intellisense 识别 JSDoc 注解?
- typescript - 使用打字稿获取羽毛聊天教程的错误消息“错误:必须向'users.get'方法提供一个ID”
- vue.js - Vue-CLI 和 Visual Studio Code 格式冲突
- authentication - 独立 Blazor WebAssembly 身份验证非常慢
- java - FlowLayout 使标签居中?
- apache-spark - 无法使用 spark 将结果写入 kafka 主题
- api - Invidious API 创建令牌
- android - 如何在 Android 10 中使用 XML 样式更改最左侧“主页”图标的颜色
- xamarin - 如何更改 Xamarin Forms 中控件的视觉状态?
- mysql - 我可以使用 VB 6.0 中的 SQL 语句向 MS Access 记录中插入多个值吗?