MATLAB implements image rotation code based on adjacent interpolation

Image rotation calculation formula:
i´= i*cosθ-j*sinθ
j´= i*sinθ+j*cosθ
Step 1: The canvas is expanded, and the expansion range is determined by the endpoint value
Step 2: A new image is obtained from the image rotation formula
Step 3 : Hole filling (adjacent interpolation method, mean interpolation method) uses the adjacent interpolation method.

function [im] = rot_interpolation(I,ang)
% I = [11 12 13; 21 22 23; 31 32 33];
% ang = pi/6;
[m,n,s] = size(I);
a = sin(ang)
b = cos(ang)
i_min = round(min(min(min(min(m*b-n*a,m*b-a),b-n*a),b-a)))
i_max = round(max(max(max(max(m*b-n*a,m*b-a),b-n*a),b-a)))
j_min = round(min(min(min(min(m*a+n*b,m*a+b),a+n*b),a+b)))
j_max = round(max(max(max(max(m*a+n*b,m*a+b),a+n*b),a+b)))
y_m = 1 - i_min             %行坐标的偏移量
y_n = 1 - j_min             %列坐标的偏移量
m1 = abs(i_max - i_min)+1
n1 = abs(j_max - j_min)+1
B = -1*ones(m1,n1,1);       %生成一个彩色的-1矩阵
B = -1*ones(m1,n1,2);
B = -1*ones(m1,n1,3);
for i=1:m                       %图像旋转
    for j=1:n
        i1 = round(i*b-j*a);
        j1 = round(i*a+j*b);
        B(i1+y_m,j1+y_n,:) = I(i,j,:);
    end
end
%邻近插值空穴填充
for i=1:m1                      %C(i,1,:)记录图像的左边界
    for j=1:n1
        if(B(i,j,:)~=-1)
            C(i,1,:) = j;
            break;
        end
    end
end
for i=1:m1                      %C(i,2,:)记录图像的右边界
    for j=n1:-1:1
        if(B(i,j,:)~=-1)
            C(i,2,:) = j;
            break;
        end
    end
end
for i=1:m1                      %空值填充为 它的前一个像素值
    start = C(i,1,:)+1;
    send = C(i,2,:)-1;
    for j=start:send
        if(B(i,j,:)==-1)
            B(i,j,:) = B(i,j-1,:);
        end
    end
end
im = uint8(B);

Example function call:
I =imread('peppers.png');
delta_ang = pi/3;[im]= rot_interpolation (I,delta_ang) ; imshow(I);figure, imshow(im);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324803441&siteId=291194637