matlab練習程序(計算圖像旋轉角度)


比如有圖像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

這里參考了我過去一篇博文:https://www.cnblogs.com/tiandsp/archive/2013/06/09/3129198.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM