Matlab学习 ---图像旋转(最邻近像素法)

function output = translate(input,angel);

H = 1;
W = 2;
%angel = 45;
img = imread(input);
%img = imread('C:\Users\anyka001\Desktop\Lena_1.png');
[row col] = size(img);
theta = angel/180 *pi;

rot = [cos(theta) -sin(theta) 0;sin(theta) cos(theta) 0;0 0 1];
inv_rot = inv(rot);
pix1 = [1 1 1]*rot;
pix2 = [1 col 1]*rot;
pix3 = [row 1 1]*rot;
pix4 = [row col 1]*rot;
Width =round(max([abs(pix1(W)-pix4(W)) abs(pix2(W)-pix3(W))]));
Hight =round(max([abs(pix1(H)-pix4(H)) abs(pix2(H)-pix3(H))]));


output = zeros(Hight,Width);

delte_x = abs(min([pix1(W) pix2(H) pix3(W) pix4(W)]));
delte_y = abs(min([pix1(H) pix2(H) pix3(H) pix4(H)]));

for y = 1-delte_y:Hight-delte_y
    for x = 1-delte_x:Width-delte_x
        pix=[y x 1]*inv_rot;

        if pix(H)>=0.5 && pix(W)>=0.5 && pix(H)<=row && pix(W)<=col
            output(y+delte_y,x+delte_x)=img(round(pix(H)),round(pix(W)));
        end
    end
end

output1= uint8(output);

imwrite(output1,'C:\Users\anyka001\Desktop\learn Matlcb\translate.png','png');

figure,imshow(output1);

函数实现图像任意角度反转。

你可能感兴趣的:(matlab)