Monday, March 16th, 2009, 1159 days ago

Angle recognition using openCV


Angle calculation using openCV from andol on Vimeo.

or-codes

In this post, i will present you the new progress of object recognition using openCV—angle recognition.

Actually, this is just a small step further than the last one which is rectangles’ recognition. From the four outlines of rectangle as well as the four corner points, we can get two relative points to calculat the angle between them. As inllustrated below, i chosed two points which are most left and right. Once the position of these two points are captured, the SINE value of the angle can be calculated like this

SIN (angle) = abs(p1.y-p2.y)/sqrt(
(p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))

So, that’s it.

or-angle


23 Responses to “Angle recognition using openCV”

  1. Jeff says:

    Do you have source code for the “Angle recognition using openCV” example?

  2. Toy says:

    it’s great

    can you send me source code?

    thanks

    • Andol says:

      sorry that due to historical evolution of codes, that source code was not kept. but you can refer to the function below which is one of the most important in angle detection:
      CvSeq* findSquares4( IplImage* img, CvMemStorage* storage )
      {
      double s = 0, t = 0;
      CvSeq* result;
      CvSeq* contours;
      CvSeq* squares = cvCreateSeq( 0, sizeof( CvSeq), sizeof( CvPoint), storage );
      cvFindContours( img, storage, &contours, sizeof( CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint( 0, 0) );
      while( contours )
      {
      result = cvApproxPoly( contours, sizeof( CvContour), storage, CV_POLY_APPROX_DP, cvContourPerimeter( contours)*0.02, 0 );
      if( result->total == 4 && fabs( cvContourArea( result, CV_WHOLE_SEQ)) > 1000 && fabs( cvContourArea( result, CV_WHOLE_SEQ)) height * img->width/2 ) && cvCheckContourConvexity( result) )
      {
      s = 0;
      for( int i = 0; i = 2 )
      {
      t = fabs( angle( ( CvPoint*)cvGetSeqElem( result, i ), ( CvPoint*)cvGetSeqElem( result, i-2 ), ( CvPoint*)cvGetSeqElem( result, i-1 )));
      s = s > t ? s : t;
      }
      }
      if( s < 0.5 )for( int i = 0; i h_next;
      }
      //cout << "Rectangles: "<total/4 <<endl;
      return squares;
      }

  3. taylor says:

    hi,Andol. i am beginner of Opencv ,
    could you please send me the code of this angle recognition?
    thanks !

  4. kutu says:

    hi,sir
    could you please send me the code of this angle recognition as it is useful in my trajectory finding

  5. Ethan says:

    hi,Andol.
    could you please send me the code of this rectangle‘s recognition?
    I am just working on the related area and find your work useful.
    Need your help. thanks !

  6. amit says:

    hiii
    i want to detect rectangular shape after doing colour detection …of image where all shapes are present.like circle,rectangle,triangle.all are of yellow colour..first i do hsv colour converson and threshholding…but how to detect rectangle after that……
    .need ur help urgently….
    thanks

    • Andol says:

      @Amit, check the function ‘CVRect locating (IplImage* img,IplImage* output)’, this is the function which does the rectangle recognition work and draws outlines on detection results. So actually the detection of rectangle after thresholds and HSV, this function is the next step to deal with the processed image.
      Hope this helps.

  7. amit says:

    hello…
    there is a function square.c in the sample of opencv…..
    in the function we can get the set of outline points…
    but wat i need is the corner points of the square….how to get it using square.c function…..
    need help

  8. jorjge says:

    Hi there, I’ve checked your page and is great, I’m a complete beginner in opencv, I was wondering what fixups must be done to your code to analyze an image instead of a video stream and how to identify triangles based on your square’s code, I really appreciate your help sharing your knowledge on ur site, have a nice day

    • Andol says:

      To change the detecting target from videos to images, the only thing different is to load images through ‘cvLoadImage()’. The other thing to detect triangle instead of rectangles, it is the same for the detection as long as you change conditions to judge detected lines. Cheers.

  9. welly says:

    I have error like this where compile locatingobject.cpp
    request for member ‘y’ in ‘p1’, which is of non-class type ‘CvPoint*’. How to fix it ?
    thx

  10. Nuwan says:

    hi. can you pls send me the source code.

  11. Ricardo says:

    Hi, I used your function FindSquare4, I´m thinking about change angle recognition, because a trapezoid can be a rectangle if I change perspective, so how can I change the angle recognition to find other angles between 25 and 135?

Leave a Reply

You can use these XHTML tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>