cartToPolar in OpenCV

cartToPolar is a function in OpenCV that can be used to compute the magnitude and angle of 2D vectors. You provide a matrix of 2D vectors as two Mat structures, one holding the X component of the vectors and the other for the Y component. cartToPolar returns two Mat structures, one holding the magnitude and the other the angle of the vectors.

Note that the angle returned is in the range 0 to 360 degrees. This is different from the typical output of the atan2 function, which is in the range of -180 to +180 degrees. To match the result of atan2 to the angle output of cartToPolar, you would need to do this:

float ang = atan2(y, x) * 180 / PI;
if (ang < 0)
    ang = 360 + ang;
  • The documentation of cartToPolar is here.

  • The source code of cartToPolar can be seen in modules/core/src/mathfuncs.cpp, which can be seen here.

  • The computation of the angle can be seen in the FastAtan2_32f function in modules/core/src/mathfuncs.cpp, which can be seen here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.