OpenCV C++/Obj-C:连接附近的轮廓
是否有连接两个(或多个)附近轮廓的功能?看看我的输入/输出,你就会明白我的意思......
我的代码:
[... some processing ...]
// getting contours
std::vector<std::vector<cv::Point> > contours;
findContours(input, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
// approximate contours
std::vector<std::vector<cv::Point> > contours_poly( contours.size() );
for( int i = 0; i < contours.size(); i++ ) {
approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 5, true );
}
// debugging
cv::Scalar colors[3];
colors[0] = cv::Scalar(255, 0, 0);
colors[1] = cv::Scalar(0, 255, 0);
colors[2] = cv::Scalar(0, 0, 255);
for (int idx = 0; idx < contours_poly.size(); idx++) {
cv::drawContours(output, contours_poly, idx, colors[idx % 3]);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出了这个解决方案,因为我只需要整个对象周围的边界框:
有时消除胡椒噪声可以带来更好的结果:
I came up with this solution, because I just need the bounding box around the whole object:
Sometimes removing pepper noise can lead to better results:
只需遍历点并找到最近的起点或终点,然后将它们连接起来即可。在您的情况下很难决定轮廓是否应该连接。如果 Adrian Popovici 所说的形态学没有帮助,您必须指定一些最大距离来决定点是否要连接。
Simply go through points and find the closest startpoints or endpoints and then connect them. It's hard to decide in your case if contours should be connected or not. If morfology as Adrian Popovici said doesn't help you must specify some max distance which decide if points are to be connected.