3

バイナリ イメージのブロブの周囲に輪郭を描画しようとしていますが、openCV が 2 つの異なるブロブの周囲に 1 つの輪郭を描画することがあります。以下は例です。この問題を解決するにはどうすればよいですか? 代替テキスト

ここでは、右側のブロブ用に 2 つの境界ボックスを描画し、左側のブロブ用に個別に描画する必要があります。私はそれらが近いことに同意しますが、それらの間に十分な距離があります。ツリーまたはリストの代わりに、外部輪郭のみを描画しています。これは私の場合のより簡単な実装であるため、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 に答える