Angle recognition using openCV

Angle calculation using openCV from andol on Vimeo.


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(

So, that’s it.


Author: Andol Li

A HCI researcher, a digital media lecturer, an information product designer, and a python/php/java coder.

25 Comments On “ Angle recognition using openCV”

    • 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;

  1. 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 !

  2. hiii
    i want to detect rectangular shape after doing colour detection …of image where all shapes are 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….

    • @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.

  3. 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

  4. 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

    • 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.

  5. 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 ?

  6. 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?

  7. Hi sir , I have tried your code to recognize an angle of bounded box but i m getting errors on access point value,
    error C2228: left of ‘.y’ must have class/struct/union
    error C2664: ‘angle’ : cannot convert parameter 1 from ‘CvPoint’ to ‘CvPoint *’
    M using opencv 2.4.2 !! Help me to rectify this errors !!
    Thanks in advance !!

Leave a comment
Due to technical adjustments, the comment function is shortly closed and will be re-openning soon. Thanks.

Copyrights 2006-2018 © All rights reserved
Theme Tree2, re-designed by Andol Li, powered by WordPress and Bootsrap
WWW.ANDOL.ME | 浙ICP备15040508号-1
公安备案图标 浙公网安备33010602004018号
Back to top