gpt4 book ai didi

c - OpenCV cvFindContours - 我如何分离轮廓的组件

转载 作者:太空狗 更新时间:2023-10-29 16:38:24 25 4
gpt4 key购买 nike

我一直在玩弄 OpenCV,经过大量的反复试验,我学会了如何检测照片中的圆圈(硬币)。一切都很好,除非我将硬币直接并排放置(如下所示,请忽略第二张图片倒置的事实)。

Original Photo Contours Found

似乎因为硬币靠得太近,cvFindContours 认为它​​们是同一个对象。我的问题是如何将这些轮廓分离到它们单独的对象中,或者获取已经分离的轮廓列表。

我用于 cvFindContours 的参数是:

cvFindContours( img, storage, &contour, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0) );

如有任何帮助或建议,我们将不胜感激。

最佳答案

这不是很好,但它显示了如何到达那里:

IplImage* src = cvLoadImage(argv[1], CV_LOAD_IMAGE_UNCHANGED);
IplImage* gray = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvCvtColor(src, gray, CV_BGR2GRAY);
cvSmooth(gray, gray, CV_GAUSSIAN, 7, 7);

IplImage* cc_img = cvCreateImage(cvGetSize(gray), gray->depth, 3);
cvSetZero(cc_img);
CvScalar(ext_color);

cvCanny(gray, gray, 10, 30, 3);

CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* circles = cvHoughCircles(gray, storage, CV_HOUGH_GRADIENT, 1, src->height/6, 100, 50);
cvCvtColor(gray, src, CV_GRAY2BGR);
for (size_t i = 0; i < circles->total; i++)
{
// round the floats to an int
float* p = (float*)cvGetSeqElem(circles, i);
cv::Point center(cvRound(p[0]), cvRound(p[1]));
int radius = cvRound(p[2]);

// draw the circle center
//cvCircle(cc_img, center, 3, CV_RGB(0,255,0), -1, 8, 0 );

// draw the circle outline
cvCircle(cc_img, center, radius+1, CV_RGB(0,0,255), 2, 8, 0 );

//printf("x: %d y: %d r: %d\n", center.x, center.y, radius);
}

CvMemStorage *mem;
mem = cvCreateMemStorage(0);
CvSeq *contours = 0;
cvCvtColor(cc_img, gray, CV_BGR2GRAY);
// Use either this:
int n = cvFindContours(gray, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
// Or this:
//int n = cvFindContours(gray, mem, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

for (; contours != 0; contours = contours->h_next)
{
ext_color = CV_RGB( rand()&255, rand()&255, rand()&255 ); //randomly coloring different contours
cvDrawContours(cc_img, contours, ext_color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
}

cvSaveImage("out.png", cc_img);

enter image description here

关于c - OpenCV cvFindContours - 我如何分离轮廓的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6044119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com