Matlab中的图像变形

时间:2013-11-17 20:26:46

标签: image matlab recursion

所以我试图通过移动图像的中心点来创建一个程序来扭曲史蒂夫乔布斯的这张照片。我必须遵循我的教授给我的伪代码,并且我的进展很少。这是我到目前为止所获得的代码,其中一些伪代码替换为我自己的代码,但我收到错误,因为我超过了500次递归的内置递归限制。我确定这不应该是一个问题所以我在某个地方有一个无限循环或递归:

function HW7
I = imread('jobs_small.jpg','jpg'); % Loads the image
figure(1); imshow(I);               % Displays the image

P_new = [194; 257];                 % Offset of the center point

D = Warp(I,P_new);                  % Displays mask to see if it's correct
figure(2); imshow(D);

imwrite(D, 'jobs_small_warper.jpg', 'jpg'); % saves the result
end


function D = Warp(I, P_new)
%image warping
%function D = Warp(I,P_new)
I = imread('jobs_small.jpg','jpg');
P_new = [194; 257];
D = Warp(I,P_new);
R = size(I,1);
C = size(I,2);
D = uint8(zeros(size(I)));

% create the original 4 triangles as 4 matrices Y1,Y2,Y3,Y4
Y1 = [1 130 1
      1 173 R];
Y2 = [1 130 C
      1 173 1];
Y3 = [1 130 R
      R 173 C];
Y4 = [R 130 C
      C 173 1];
% create the distorted 4 triangles as 4 matrices X1,X2,X3,X4
X1 = [[1;1] [P_new] [1;R]];
X2 = [[1;1] [P_new] [C;1]];
X3 = [[1;R] [P_new] [R;C]];
X4 = [[R;C] [P_new] [C;1]];

M = CreateMask(R,C,X1,X2,X3,X4);
figure(3); imshow(M./4);

A1 = SolveWarp(X1,Y1);
A2 = SolveWarp(X2,Y2);
A3 = SolveWarp(X3,Y3);
A4 = SolveWarp(X4,Y4);

% warp the 4 regions and copy the pixels from I to D
% Hint: you can use if/else statement within a double for loop to scan the
% whole matrix


% Note: be careful to round the coordinates to make them inside the image
% boundry
% D <- copy the corresponding pixels from I based on M,A1,A2,A3,A4
end


function A = SolveWarp(X,Y)
A = Y*inv([[X]; [1 1 1]]);
% following Eq(1) for the solution <- implement this!
end


function M = CreateMask(R,C,P1,P2,P3,P4)
% R -- number of Rows
% C -- number of columns
% P1,P2,P3,P4 are the 4 triangles
% M -- the mask of size (R,C)

M = zeros(R,C);

% x <- collection of x coordinates of all pixels
x = I(1:2:259,:); %VERY LIKELY INCORRECT
% y <- collection of y coordinates of all pixels
y = I(2:2:259,:); %VERY LIKELY INCORRECT

M1 = inpolygon(x,y,P1(1,:),P1(2,:));
M2 = inpolygon(x,y,P2(1,:),P2(2,:))*2;
M3 = inpolygon(x,y,P3(1,:),P3(2,:))*3;
M4 = inpolygon(x,y,P4(1,:),P4(2,:))*4;
M5 = max(max(max(M1,M2), M3), M4);

% M <- re-stack M5 to an RxC matrix
M5 = zeros(R,C);
% ^ pay attention to the order: row-by-row or column-by-column

end


% The center is shifted to (194,257)

所以你可以看到这几乎是一团糟。我特别感到困惑的是如何将像素从矩阵I(图像)复制到矩阵D(失真图像),掩模应该如何工作,以及M5在代码末尾附近的重新堆叠意味着什么。 我非常感谢任何指导和帮助,你们都可以给我。 感谢

0 个答案:

没有答案