I am able to detect all the squares/rectangles (any contour consists of 4 points) in a given WEBCAM frame. But i am facing a problem that it detects a single square/rectangle as 3-4 squares/rectangles which covers more or less the same portion of the image (Not exactly the same).
Problem: I want to remove those CvSeq
which give me the similar "square" which has already been processed/drawn.
Example: Lets say one rectangle is {(3,3), (3, 50), (20,4), (25,56)}
and another rectangle as {(2,2), (4, 52), (21,6), (23,58)}
and so on....so if you see then these two rectangle are covering the similar portion of image..so i just want to keep any one of them
The part of my code where i am trying to draw the squares after receiving the information about all the squares contained in CvSeq
is following:
// 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 );
}