cvFindcontours提取的轮廓显示出来是断断续续的点

ITSophia 2016-04-16 10:11:46
加精

原图(待提取轮廓)

使用cvFindcontours提取的轮廓,可以看到轮廓上的点是断断续续的,想知道这是为什么?
代码如下:

int main()
{
char * src_filename = "test//0.jpg";
//读入原图
IplImage *SrcImage = cvLoadImage(src_filename, CV_LOAD_IMAGE_ANYDEPTH);
//显示原图
cvNamedWindow("src", CV_WINDOW_AUTOSIZE);
cvShowImage("src", SrcImage);

//二值化
cvThreshold(SrcImage, SrcImage, 200, 255, THRESH_BINARY_INV);
CvMemStorage *src_storage = cvCreateMemStorage(0); //开辟内存空间
CvSeq* src_contour = NULL;
cvFindContours(SrcImage, src_storage, &src_contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);//这函数可选参数还有不少


//显示轮廓
cvNamedWindow("src_img", CV_WINDOW_AUTOSIZE);
cvShowImage("src_img", SrcImage);
cvWaitKey(0);
cvReleaseImage(&SrcImage);
cvDestroyWindow("src_img");
cvDestroyWindow("src");
return 0;
}
...全文
5482 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LXHappiness 2016-11-01
  • 打赏
  • 举报
回复 2
我也遇到了同样的问题,然后尝试修改了各项参数,发现改成这样就是连续的轮廓(我用的是java) Imgproc.findContours(cannyContour, contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE); 关键参数应该是最后一项
ID870177103 2016-04-26
  • 打赏
  • 举报
回复
不是这个函数的问题,这个函数不会改变srcimage,是二值化函数的问题,最近也在做opencv,opencv的二值化函数并不理想
ssbqrm 2016-04-18
  • 打赏
  • 举报
回复
是不是显示的问题啊
shiter 2016-04-17
  • 打赏
  • 举报
回复
这个主要是参照论文:[Suzuki85] Suzuki, S. and Abe, K., Topological Structural Analysis of Digitized Binary Images by Border Following. CVGIP 30 1, pp 32-46 (1985) 实现的,可以看看论文
shiter 2016-04-17
  • 打赏
  • 举报
回复
Contour retrieval mode. CV_RETR_EXTERNAL retrieves only the extreme outer contours. It sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours. CV_RETR_LIST retrieves all of the contours without establishing any hierarchical relationships. CV_RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy. At the top level, there are external boundaries of the components. At the second level, there are boundaries of the holes. If there is another contour inside a hole of a connected component, it is still put at the top level. CV_RETR_TREE retrieves all of the contours and reconstructs a full hierarchy of nested contours. This full hierarchy is built and shown in the OpenCV contours.c demo. method – Contour approximation method. CV_CHAIN_APPROX_NONE stores absolutely all the contour points. That is, any 2 subsequent points (x1,y1) and (x2,y2) of the contour will be either horizontal, vertical or diagonal neighbors, that is, max(abs(x1-x2),abs(y2-y1))==1. CV_CHAIN_APPROX_SIMPLE compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, an up-right rectangular contour is encoded with 4 points. CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS applies one of the flavors of the Teh-Chin chain approximation algorithm. See [TehChin89] for details.
ITSophia 2016-04-16
  • 打赏
  • 举报
回复 4
使用cvDrawContours()把轮廓绘制出来就可以了。
添加以下代码:

cvZero(SrcImage);
cvDrawContours(
SrcImage,
src_contour,
cvScalar(100),
cvScalar(100),
1 //#2 需更改区域
);


注意,如果图像是没有hole的,如:

这时,如果在cvFindcontours中的参数为CV_RETR_CCOMP,提取的结果是空的,因为对于CV_RETR_CCOMP,它是提取hole和最外框之间的外轮廓和内轮廓,如果是实心图像,即找不到hole,提取不到对应的轮廓,所以此时需要使用参数CV_RETR_LIST,就可以将轮廓提取出来

如果图像为:

用CV_RETR_LIST提取出的轮廓为:

用CV_RETR_CCOMP提取出的轮廓为:

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧