|
比如有图像1,将其旋转n度得到图像2,问如何比较两张图像得到旋转的度数n。
算法思路参考logpolar变换:
1.从图像中心位置向四周引出射线。
2.计算每根射线所打到图像上的像素累计和,得到极坐标灰度曲线。
3.比较两张图的灰度曲线,得到最相关的偏移位置,即为两张图像的旋转角度。
原图:
旋转了10°的图像:
灰度曲线:
代码如下:
main.m
- clear all;
- close all;
- clc;
- img1 = imread('lena.jpg');
- img2 = imrotate(img1,15);
- imshow(img1)
- figure;
- imshow(img2)
- re1 = getCurve(img1);
- re2 = getCurve(img2);
- figure;
- plot(re1);
- hold on;
- plot(re2);
- su=zeros(length(re1),1);
- for i=1:length(re1)
- tmp = circshift(re2,i);
- su(i) =sum(tmp.*re1);
- end
- [ma,ind] = max(su);
- ind/10
复制代码
getCurve.m
- function re = getCurve(img)
- [m,n]=size(img);
- oy=m/2;
- ox=n/2;
- %求中心点到图像四个角的距离
- up_left=sqrt((oy-0)^2+(ox-0)^2);
- up_right=sqrt((oy-0)^2+(ox-n)^2);
- down_left=sqrt((oy-m)^2+(ox-0)^2);
- down_right=sqrt((oy-m)^2+(ox-n)^2);
- num=3600;
- %求中心点距离四角距离的最大值,作为变换后图像的高。
- %这个最大值也是极坐标变换的极径
- radius=round(max([up_left up_right down_left down_right]));
- re = zeros(num,1);
- for i=0:1:radius %纵坐标代表极径,不同情况不一样
- for j=1:num %横坐标代表极角,为3600
- %oy,ox作为极坐标变换中心坐标,需要作为偏移量相加
- ind = j/10;
- h=round(oy+i*sin(ind*pi/180));
- w=round(ox+i*cos(ind*pi/180));
-
- if h>0 && w> 0&& h<=m && w<=n %超出原图像的像素忽略
- re(j)= re(j) +double(img(h,w));
- end
- end
- end
- re = re/sum(re);
- end
复制代码
|
|