I build a program using the basic idea of detecting squares from square.c
of openCV samples. I detect the squares in CvSeq*
and then i calculate the minimum_x, min_y, width and columns to create seperate image for each detected square.
Problem: But sometimes i am getting min_x = 149186927
and min_y = 149186937
I am doing the following process in a for loop
i.e. for(int i=0; i<square.size(); i+=4)
. So, if i detect 12 square then it works for all 12 square. But my program crash after sometime. It works for sometime whether it is one square or more than one.
First of all, i store four points of the square as following:
CvPoint pt_reader[4] ;
pt_reader[0].x = ( (CvPoint*)cvGetSeqElem(squares, i) )->x; // first coordinate
pt_reader[0].y = ( (CvPoint*)cvGetSeqElem(squares, i) )->y;
pt_reader[1].x = ( (CvPoint*)cvGetSeqElem(squares, i+1) )->x;// second coordinate
pt_reader[1].y = ( (CvPoint*)cvGetSeqElem(squares, i+1) )->y;
pt_reader[2].x = ( (CvPoint*)cvGetSeqElem(squares, i+2) )->x;
pt_reader[2].y = ( (CvPoint*)cvGetSeqElem(squares, i+2) )->y;
pt_reader[3].x = ( (CvPoint*)cvGetSeqElem(squares, i+3) )->x;
pt_reader[3].y = ( (CvPoint*)cvGetSeqElem(squares, i+3) )->y;
Then i try to get the minimum_x and minimum_y from those 4 coordinates of the square so that i can create a ROI
int min_x = findMin_x_pointA(pt_reader);
int min_y = findMin_y_pointB(pt_reader);
int max_x = findMax_x_pointC(pt_reader);
int max_y = findMax_y_pointD(pt_reader);
Calculating the number of rows and cols for ROI
int cols = max_x - min_x;
int rows = max_y - min_y;
Now, i am finally creating an image for ROI
if(rows>0 && rows<300 && cols>0 && cols<300)
{
Mat mySquare;
mySquare.create(rows, cols, CV_8UC3);
cout<<"\nROI: "<<min_x<<" "<<min_y<<" "<<cols<<" "<<rows;
Rect regionOfInterest = Rect (min_x,min_y, cols, rows);
mySquare= original_frame(regionOfInterest);
squareImages.push_back(mySquare);
}
My functions to calculate the minimum values look like following:
int FindRect::findMin_x_pointA(CvPoint pt_reader[] )
{
CvPoint pointA;
int min_x =pt_reader[0].x;
for(int i=1; i<4; i++)
{
if(min_x > pt_reader[i].x)
{
min_x= pt_reader[i].x;
}
}
return min_x;
}