3

我试图在二进制图像中围绕斑点绘制轮廓,但是,有时,openCV 在两个不同的斑点周围绘制单个轮廓。下面是一个例子。我该如何解决这个问题? 替代文字

在这里,它应该为右侧的 blob 绘制两个边界框,并分别为左侧的一个绘制边界框。我同意它们很近,但它们之间有足够的距离。我只绘制外部轮廓而不是树或列表。我也在使用 cvFindNextContour(contourscanner) 因为这对我来说是一个更简单的实现。

谢谢

编辑:“输出”窗口中显示的图像来自一个不同的功能,它只是图像减法。“轮廓”窗口中显示的图像在函数 pplfind() 中。“输出”图像被传递给 img_con()。


IplImage* img_con(IplImage* image){
    int ppl;
    CvMemStorage* memstr = cvCreateMemStorage();
    IplImage* edges = cvCreateImage(cvGetSize(image),8,1);
    cvCanny(image,edges,130,255);
    CvContourScanner cscan = cvStartFindContours(image,memstr,sizeof(CvContour),CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,cvPoint(0,0));

ppl = pplfind(cscan,cvGetSize(image));
if (ppl !=0 )
    printf("Estimated number of people: %d\n",ppl);
cvEndFindContours(&cscan);
cvClearMemStorage(memstr);

return edges;

}

int pplfind(CvContourScanner cscan, CvSize frSize){ ofstream file; char buff[50]; file.open("box.txt",ofstream::app); int ppl =0; CvSeq* c; IplImage *out = cvCreateImage(frSize,8,3); while (c = cvFindNextContour(cscan)){ CvRect box = cvBoundingRect(c,1); if ((box.height > int(box.width*1.2))&&(box.height>20)){//&&(box.width<20)){// ppl++; cvRectangle(out,cvPoint(box.x,box.y),cvPoint(box.x+box.width,box.y+box.height),CV_RGB(255,0,50),1);

        cvShowImage("contours",out);
        //cvWaitKey();
    }
    //printf("Box Height: %d , Box Width: %d ,People: %d\n",box.height,box.width,ppl);
    //cvWaitKey(0);
    int coord = sprintf_s(buff,"%d,%d,%d\n",box.width,box.height,ppl);
    file.write(buff,coord);
}
file.close();
cvReleaseImage(&out);
return ppl;

}

4

1 回答 1

3

我从未使用过cvFindNextContour,但在您的图像上运行cvFindContours似乎CV_RETR_EXTERNAL可以正常工作:

替代文字

我使用 OpenCV + Python,所以这段代码可能对你没有用,但为了完整起见,这里是:

contours = cv.findContours(img, cv.CreateMemStorage(0), mode=cv.CV_RETR_EXTERNAL)
while contours:
    (x,y,w,h) = cv.BoundingRect(contours)
    cv.Rectangle(colorImg, (x,y), (x+w,y+h), cv.Scalar(0,255,255,255))
    contours = contours.h_next()

编辑:您问如何仅绘制具有某些属性的轮廓;它会是这样的:

contours = cv.findContours(img, cv.CreateMemStorage(0), mode=cv.CV_RETR_EXTERNAL)
while contours:
    (x,y,w,h) = cv.BoundingRect(contours)
    if h > w*1.2 and h > 20:
        cv.Rectangle(colorImg, (x,y), (x+w,y+h), cv.Scalar(0,255,255,255))
    contours = contours.h_next()
于 2011-01-10T18:32:50.747 回答