Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

cartToPolar in OpenCV

📅 2014-Feb-26 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ atan2, carttopolar, opencv ⬩ 📚 Archive

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;