2016-10-25 3 views
1

나는 이미지 프로세싱을 처음 사용하기 때문에 이미지를 뒤틀기위한 코드를 구현했으며 완벽하게 작동합니다. 필자는 내장 함수 (interp)를 사용하지 않고 선형 보간을 사용하여 이미지를 회전하여 코드를 개선하고 싶습니다.Matlab 이미지 회전

내가 기능 [imwarp][1] 구축 Matalb에 비교하여 구현을 확인 : 다음과 같은 솔루션

close all; 
clear all; 

img = 'woods.jpg'; 

input_image =double(imread(img))./255; 

H=size(input_image,1); 
W=size(input_image,2); 

th=pi/4; 

s0 = 2; 
s1 = 2; 

x0 = -W/2; 
x1 = -H/2; 

T=[1 0 x0 ; ... 
    0 1 x1 ; ... 
    0 0 1]; 

RST = [ (s0*cos(th)) (-s1*sin(th)) ((s0*x0*cos(th))-(s1*x1*sin(th))); ... 
     (s0*sin(th)) (s1*cos(th)) ((s0*x0*sin(th))+(s1*x1*cos(th))); ... 
     0 0 1]; 

M=inv(T)*R; 
N = inv(M); 


output_image=zeros(H,W,3); 

for i=1:W 
    for j=1:H 

     x = [i ; j ; 1]; 
     y = N * x; 

     a = y(1)/y(3); 
     b = y(2)/y(3); 

     a = round(a); 
     b = round(b); 

     if (a>0 && a<=W && b>0 && b<=H) 
      output_image(j,i,:)=input_image(b,a,:); 
     end 

    end 
end 

imgshow(output_image); 
+0

가능한 [Resi imresize없이 쌍 선형 보간을 사용하여 이미지 만들기] (http://stackoverflow.com/questions/26142288/resize-an-image-with-bilinear-interpolation-withoutimimize) – Anna1994

답변

1

확인 : 여기 내 코드입니다.

close all; 
clear all; 

img = 'peppers.png'; 

input_image =double(imread(img))./255; 

H=size(input_image,1); % height 
W=size(input_image,2); % width 

th=pi/4; 

s0 = 2; 
s1 = 2; 

x0 = -W/2; 
x1 = -H/2; 

T=[1 0 x0 ; ... 
    0 1 x1 ; ... 
    0 0 1]; 

RST = [ (s0*cos(th)) (-s1*sin(th)) ((s0*x0*cos(th))-(s1*x1*sin(th))); ... 
     (s0*sin(th)) (s1*cos(th)) ((s0*x0*sin(th))+(s1*x1*cos(th))); ... 
     0 0 1]; 

M=inv(T)*RST; 
N = inv(M); 

output_image=zeros(H,W,3); 

for i=1:W 
    for j=1:H 

     x = [i ; j ; 1]; 
     y = N * x; 

     a = y(1)/y(3); 
     b = y(2)/y(3); 

     %Nearest neighbor 
     %a = round(a); 
     %b = round(b); 


     x1 = floor(a); 
     y1 = floor(b); 
     x2 = x1 + 1; 
     y2 = y1 + 1; 

     %Bi-linear interpolation ilsutration: 
     %Image coordinates style (horizontal index first) 
     % 
     %(x1,y1) |   (x2,y1) 
     %   | 1-dy 
     % 1-dx |  dx 
     % ------(a,b)------------ 
     %   | 
     %   | 
     %   | 
     %   | dy 
     %   | 
     %   | 
     %(x1,y2) |   (x2,y2) 

     if ((x1 >= 1) && (y1 >= 1) && (x2 <= W) && (y2 <= H)) 
      %Load 2x2 pixels 
      i11 = input_image(y1, x1, :); %Top left pixel 
      i21 = input_image(y2, x1, :); %Bottom left pixel 
      i12 = input_image(y1, x2, :); %Top right pixel 
      i22 = input_image(y2, x2, :); %Bottom right pixel 

      %Interpolation wieghts 
      dx = x2 - a; 
      dy = y2 - b; 

      %Bi-lienar interpolation 
      output_image(j, i, :) = i11*dx*dy + i21*dx*(1-dy) + i12*(1-dx)*dy + i22*(1-dx)*(1-dy); 
     end 
    end 
end 

imshow(output_image); 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%Verify implementation by comparing with Matalb build in function imwarp: 
tform = affine2d(M'); 
ref_image = imwarp(input_image, tform, 'OutputView', imref2d(size(input_image)), 'Interp', 'linear'); 
figure;imshow(ref_image) 

figure;imshow(output_image - ref_image) 
max_diff = max(abs(output_image(:) - ref_image(:))); 
disp(['Maximum difference from imwarp = ', num2str(max_diff)]); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

결과 :

enter image description here


비고
난 (선형 보간이 더 설명된다) 선형 보간을 이용하여 영상의 크기를 조정 다음 execelnt 소식을 하회
Resize an image with bilinear interpolation without imresize