Find angle and rotation of point

asked 2015-08-26 08:24:32 -0600

Deepak Kumar gravatar image

hi , i want to find the angle. i find the following code can anyone explain me its meaning. here why p0,p1,p2 are being findout. what is the meaning of (uxvx + uyvy) / sqrt((uxux + uyuy)(vxvx + vyvy) and (uxvy - vx*uy) here.

double angle(std::vector<cv::Point>& contour, int pt, int r)
{
    int size = contour.size();
    cv::Point p0 = (pt>0) ? contour[pt%size] : contour[size - 1 + pt];
    cv::Point p1 = contour[(pt + r) % size];
    cv::Point p2 = (pt>r) ? contour[pt - r] : contour[size - 1 - r];

    double ux = p0.x - p1.x;
    double uy = p0.y - p1.y;
    double vx = p0.x - p2.x;
    double vy = p0.y - p2.y;
    return (ux*vx + uy*vy) / sqrt((ux*ux + uy*uy)*(vx*vx + vy*vy));
}

int rotation(std::vector<cv::Point>& contour, int pt, int r)
{
    int size = contour.size();
    cv::Point p0 = (pt>0) ? contour[pt%size] : contour[size - 1 + pt];
    cv::Point p1 = contour[(pt + r) % size];
    cv::Point p2 = (pt>r) ? contour[pt - r] : contour[size - 1 - r];

    double ux = p0.x - p1.x;
    double uy = p0.y - p1.y;
    double vx = p0.x - p2.x;
    double vy = p0.y - p2.y;
    return (ux*vy - vx*uy);
}

thanks!!

edit retag flag offensive close merge delete

Comments

Angle and rotation of a point? I do not understand can you give an example

LBerger gravatar imageLBerger ( 2015-08-26 08:34:28 -0600 )edit
1

to compute an angle you need three point. i suggest you to use the function below. you can find example usage here

// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
static double angle( Point pt1, Point pt2, Point pt0 )
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
sturkmen gravatar imagesturkmen ( 2015-08-26 08:51:19 -0600 )edit

can you explain me why the following is done : what is its meaning

cv::Point p0 = (pt>0) ? contour[pt%size] : contour[size - 1 + pt];
    cv::Point p1 = contour[(pt + r) % size];
    cv::Point p2 = (pt>r) ? contour[pt - r] : contour[size - 1 - r];
Deepak Kumar gravatar imageDeepak Kumar ( 2015-08-27 01:19:09 -0600 )edit
1

operator % and ? Your question is not about OpenCV. You should read a book about C++ and geometry

LBerger gravatar imageLBerger ( 2015-08-27 01:30:06 -0600 )edit

Dear LBerger, i want to know conceptually why this is being done to find 3 points. with a difference of 16. i know the meaning of operator %.

Deepak Kumar gravatar imageDeepak Kumar ( 2015-08-27 01:34:13 -0600 )edit

As you don't answer to my first question what is angle and rotation point ? and you don't answer too @sturkmen and you can give some lines outside of context. I can imagine many things about your problem. People who try to answer to question want only to help people.

Now with your last question a contour is a set of order points from i to size-1 in your example. distance from Point i to (i+1)%size could be only 1 if it's a 4-connex contour( 8-connex 1 or square rooth of 2) and angle relative to x axis 0,90,180 or -90 degree. Now If you want to have an estimation what is your contour orientation around i point it's better to take a point far from i (but not far 16 and -16 are good values).

Hence we've got two vectors and ...(more)

LBerger gravatar imageLBerger ( 2015-08-27 01:51:49 -0600 )edit

the % makes it wrap around the end, and start at the beginning again

berak gravatar imageberak ( 2015-08-27 07:55:51 -0600 )edit