MATLAB 计算图像旋转角度

比如有图像1,将其旋转n度得到图像2,问如何比较两张图像得到旋转的度数n。

算法思路参考logpolar变换:

1.从图像中心位置向四周引出射线。

2.计算每根射线所打到图像上的像素累计和,得到极坐标灰度曲线。

3.比较两张图的灰度曲线,得到最相关的偏移位置,即为两张图像的旋转角度。

原图:

旋转了10°的图像:

灰度曲线:

代码如下:

main.m

 1 clear all;
 2 close all;
 3 clc;
 4 
 5 img1 = imread('lena.jpg');
 6 img2 = imrotate(img1,15);
 7 
 8 imshow(img1)
 9 figure;
10 imshow(img2)
11 
12 re1 = getCurve(img1);
13 re2 = getCurve(img2);
14 
15 figure;
16 plot(re1);
17 hold on;
18 plot(re2);
19 
20 su=zeros(length(re1),1);
21 for i=1:length(re1)
22     tmp = circshift(re2,i);
23     su(i) =sum(tmp.*re1);
24 end
25 
26 [ma,ind] = max(su);
27 ind/10

getCurve.m

 1 function re = getCurve(img)
 2 
 3 [m,n]=size(img);
 4 
 5 oy=m/2;
 6 ox=n/2;
 7 
 8 %求中心点到图像四个角的距离
 9 up_left=sqrt((oy-0)^2+(ox-0)^2);
10 up_right=sqrt((oy-0)^2+(ox-n)^2);
11 down_left=sqrt((oy-m)^2+(ox-0)^2);
12 down_right=sqrt((oy-m)^2+(ox-n)^2);
13 
14 num=3600;
15 %求中心点距离四角距离的最大值,作为变换后图像的高。
16 %这个最大值也是极坐标变换的极径
17 radius=round(max([up_left up_right down_left down_right]));
18 re = zeros(num,1);
19 
20 for i=0:1:radius          %纵坐标代表极径,不同情况不一样
21     for j=1:num       %横坐标代表极角,为3600
22         %oy,ox作为极坐标变换中心坐标,需要作为偏移量相加
23         ind = j/10;
24         h=round(oy+i*sin(ind*pi/180));
25         w=round(ox+i*cos(ind*pi/180));
26         
27         if h>0 && w> 0&& h<=m && w<=n       %超出原图像的像素忽略
28             re(j)= re(j) +double(img(h,w));
29         end
30     end
31 end
32 re = re/sum(re);
33 end
原文地址:https://www.cnblogs.com/ybqjymy/p/13645488.html