## 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( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))

So, that’s it.

###
25 Comments On *“
Angle recognition using openCV”*

Leave a comment

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

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

yes i do. leave your email and i ll share it to you.

it’s great

can you send me source code?

thanks

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;

}

hi,Andol. i am beginner of Opencv ,

could you please send me the code of this angle recognition?

thanks !

please refer here locating objects

cheers man!!!

hi , with your code i can get the angle of the BoundingRect (but not the object).

how can i get the object-Angle?

thanks!

Though it might help ‘

object angle recognition‘.Hi Andol,

your way to find angle is different form opencv .

But is your angle finding algorithm work for all angle direction like 0 t8 360 ???

Actually i have to detect hand direction . or any moving object direction .

hi,sir

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

@Kutu please check the comments above, the code link has been provided.

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 !

Please check this http://download.andol.me/locatingObjects.cpp

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

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

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

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.

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

hi. can you pls send me the source code.

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?

@Ricardo

If you know coordinates of four points of a square, then you can calculate any angle of this square.

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

you may not define a position structure before using it.

try defining a structure of position which contains x and y coordinates.

cheers