0

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;
    }
skm
  • 5,015
  • 8
  • 43
  • 104
  • Where's the definition for `findMin_x_pointA`, etc..... – Captain Obvlious Feb 18 '14 at 20:45
  • i have updated the post. please have a look – skm Feb 18 '14 at 20:46
  • how? i have written `if(min_x > pt_reader[i].x)` then `min_x= pt_reader[i].x;` so thereby i am storing the minimum value – skm Feb 18 '14 at 20:55
  • For some reasons people can't seem to wrap their head around using ">" to find the min, but that code works as far as I can see. In situations where you get garbage, you're likely not detecting squares. Have you used a debugger to make sure you are getting the correct result? – David Nilosek Feb 18 '14 at 21:11
  • @DavidNilosek: sorry i don't have a debugger...but i wonder why it is enterning inside the `if loop`? i have already made a check in `if loop` – skm Feb 18 '14 at 21:26
  • Unsure, but I would certainly recommend learning to use a debugger, it would help you solve a lot of these problems. If you are on a linux platform, GDB is a wonderful tool. – David Nilosek Feb 19 '14 at 00:39

2 Answers2

0

There is problem in your findMin_x_pointA function. You should compare with < operator to return min value.

The other problem, as you are getting min_x = 149186927 and min_y = 149186937, to solve this problem check values in pt_reader right after you copy points from squares CvSeq.

Ghimire
  • 191
  • 3
  • 7
  • min_x > pt_reader[i].x is the same as pt_reader[i].x < min_x ... that works to find the min value.... – David Nilosek Feb 18 '14 at 21:10
  • i have already made a check `if (rows <300 && cols<300)` then why it is entering inside the `if loop` – skm Feb 18 '14 at 21:22
0

More reference/similar question: c++ algorithm for running a command after detecting square

Ok, I used to have this problem too. Can't really remember how I solved it, but if your problem are as follows, then my solution is probably right.

You said:

But sometimes i am getting min_x = 149186927 and min_y = 149186937

Sometimes as in when there is more than one square? If one square, you do get returned the normal values right? If so, my suspicion is that you need to clear/empty your pt_reader after detection after each square. Hope that works.

Best of luck. If not, you can always refer to the similar question for more ideas to edit your code till it works normally. If it works, do let me know (:

Community
  • 1
  • 1
rockinfresh
  • 2,068
  • 4
  • 28
  • 46