Ask Your Question

How to find angle between two images

asked 2015-11-23 07:02:47 -0500

ganeshchavan gravatar image

How to find angle between two images, ones is ref and another is sample with rotation. This C:\fakepath\Crop.jpg is sample image and This C:\fakepath\template.jpg is reference image. Thank You in Advance!

edit retag flag offensive close merge delete



In addition to the previous answers, a fast method to determine the object rotation would be to compute the histogram of gradients on the image and compute the correlation.

The angle between the images is the phase between the gradients.

kbarni gravatar imagekbarni ( 2015-11-25 03:51:54 -0500 )edit

Thank You, can you please explain.

ganeshchavan gravatar imageganeshchavan ( 2015-11-25 06:39:52 -0500 )edit

Create a 360 element vector (the gradient histogram, one element per °) for each image (H1 and H2). Calculate the gradient vector (Gx and Gy) in every pixel of the first image. Then get the angle A=fastAtan2(Gy,Gx) of the gradient. Increment the corresponding element (H1[A]++). This gives the gradient histogram. Repeat for the second image. Then calculate the correlation between the histograms: C(t)=sum(H1[x] H2[x+t]). The maximal value of C(t) will give you the angle difference t between the images.

kbarni gravatar imagekbarni ( 2015-11-25 08:04:09 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2015-11-23 11:11:17 -0500

dufferdev gravatar image

You can try finding the orientation of both the images using moments. Using the two orientation you can find the angle by taking the difference between them .

cv::Point3d findOrientation(const cv::Mat& src){
      cv::Moments m = cv::moments(src, true);
      double cen_x=m.m10/m.m00;
      double cen_y=m.m01/m.m00;
     double m_11= 2*m.m11-m.m00*(cen_x*cen_x+cen_y*cen_y);// m.mu11/m.m00;    
     double m_02=m.m02-m.m00*cen_y*cen_y;// m.mu02/m.m00;
     double m_20=m.m20-m.m00*cen_x*cen_x;//m.mu20/m.m00;    
     double theta = m_20==m_02?0:atan2(m_11, m_20-m_02)/2.0;
    //  theta = (theta / PI) * 180.0; //if you want in radians.(or vice versa, not sure)
    return cv::Point3d(cen_x,cen_y,theta);


I am returning the following: (x,y)(center of mass) and theta (orientation)

edit flag offensive delete link more


I am using opencv3.0 with netbeansjava and there is no any Moments class.

ganeshchavan gravatar imageganeshchavan ( 2015-11-23 23:10:06 -0500 )edit
Login/Signup to Answer

Question Tools



Asked: 2015-11-23 07:02:47 -0500

Seen: 3,024 times

Last updated: Nov 23 '15