Image Matlab中的图像扭曲

Image Matlab中的图像扭曲,image,matlab,recursion,Image,Matlab,Recursion,所以我试图创建一个程序,通过移动图像的中心点来扭曲史蒂夫·乔布斯的照片。我必须按照我的教授给我的伪代码来做,我的进步很小。这是我到目前为止得到的代码,其中一些伪代码被我自己的代码替换,但是我得到了一个错误,因为我超过了内置的递归限制500次递归。我确信这不应该是个问题,所以我在某个地方有一个无限循环或递归: function HW7 I = imread('jobs_small.jpg','jpg'); % Loads the image figure(1); imshow(I);

所以我试图创建一个程序,通过移动图像的中心点来扭曲史蒂夫·乔布斯的照片。我必须按照我的教授给我的伪代码来做,我的进步很小。这是我到目前为止得到的代码,其中一些伪代码被我自己的代码替换,但是我得到了一个错误,因为我超过了内置的递归限制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)
功能HW7
I=imread('jobs_small.jpg','jpg');%加载图像
图(1);imshow(I);%显示图像
P_new=[194;257];%中心点偏移
D=扭曲(I,P_新);%显示蒙版以查看其是否正确
图(2);imshow(D);
imwrite(D,'jobs_small_warper.jpg','jpg');%保存结果
结束
函数D=扭曲(I,P_新)
%图像扭曲
%函数D=扭曲(I,P_新)
I=imread('jobs_small.jpg','jpg');
P_new=[194;257];
D=扭曲(I,P_新);
R=尺寸(I,1);
C=尺寸(I,2);
D=uint8(零(大小(I));
%将原始的4个三角形创建为4个矩阵Y1、Y2、Y3、Y4
Y1=[1 130 1
1173 R];
Y2=[1 130摄氏度
1 173 1];
Y3=[1 130 R
R 173 C];
Y4=[R 130摄氏度
C 173 1];
%将扭曲的4个三角形创建为4个矩阵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=创建掩码(R、C、X1、X2、X3、X4);
图(3);imshow(M/4);
A1=X1,Y1;
A2=X2,Y2;
A3=X3,Y3;
A4=x(X4,Y4);
%扭曲4个区域并将像素从I复制到D
%提示:可以在double for循环中使用if/else语句来扫描
%全矩阵
%注意:小心将坐标四舍五入,使其位于图像内部
%边界

%D递归发生在
Warp
函数的第五行。。。我不能帮你做其他事情对不起!@darthbith的意思是在
warp中调用
warp
这就是递归。另外,由于传入了
I
p\u new,
因此不需要(也不应该)在函数中重新定义它们。