如何删除导致opencv中重叠的正方形/矩形的CvSeq点?

时间:2014-01-27 19:41:46

标签: opencv

我能够在给定的WEBCAM帧中检测所有正方形/矩形(任何轮廓由4个点组成)。但我面临一个问题,即它检测到一个正方形/矩形为3-4个正方形/矩形,它们或多或少地覆盖了图像的相同部分(不完全相同)。

问题:我想删除那些CvSeq,它们会给我类似的" square"已经处理/绘制的。

overlapping square

示例: 让我们说一个矩形是{(3,3), (3, 50), (20,4), (25,56)}而另一个矩形是{(2,2), (4, 52), (21,6), (23,58)}等等......所以如果你看到那么这两个矩形覆盖了图像的相似部分。所以我只想保留其中任何一个

我收到有关CvSeq中包含的所有正方形的信息后,我试图绘制正方形的代码部分如下:

// the function draws all the squares in the image
void drawSquares( IplImage* img, CvSeq* squares ) // CvSeq* squares--> "square" contains all the points for all the squares.
{

    CvSeqReader reader;
    IplImage* cpy = cvCloneImage( img );
    int i;    


    // initialize reader of the sequence
    cvStartReadSeq( squares, &reader, 0 );

    // read 4 sequence elements at a time (all vertices of a square)
    for( i = 0; i < squares->total; i += 4 ) // increaing i by 4 because one square consists of 4 points
    {
        CvPoint pt[4], *rect = pt;
        int count = 4;

        // read 4 vertices
        CV_READ_SEQ_ELEM( pt[0], reader );
        CV_READ_SEQ_ELEM( pt[1], reader );
        CV_READ_SEQ_ELEM( pt[2], reader );
        CV_READ_SEQ_ELEM( pt[3], reader );

        // draw the square as a closed polyline
        cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 );

    }

    // show the resultant image
    cvShowImage( wndname, cpy );
    cvReleaseImage( &cpy );
}

2 个答案:

答案 0 :(得分:0)

看看square.c中的这段代码

for( c = 0; c < 3; c++ )
{
    // extract the c-th color plane
    cvSetImageCOI( timg, c+1 );
    cvCopy( timg, tgray, 0 );

    // try several threshold levels
    for( l = 0; l < N; l++ )
    {

我建议暂时将其简化,以便删除这两个循环。使用CvColor将原始图像转换为灰度,然后应用单个CvCanny和单个CvThreshold操作开始。您可以尝试参数,然后在基本检测工作时执行更复杂的操作。

答案 1 :(得分:0)

找到重叠的矩形并应用cv :: groupRectangles()

更多here