2013-04-26 415 views
1

我正在使用MATLAB R2012a,我試圖讓用戶在不使用內置函數的情況下裁剪圖像。

這裏是我的代碼:
MATLAB幫助裁剪圖像

[x, y] = ginput(2); 
m1 = [x(1), y(1)]; 
m2 = [x(2), y(2)]; 
m1 = int16(m1); 
m2 = int16(m2); 
[m, n] = size(manip); 
s1 = (m2(1) - m1(1))+1; 
s2 = (m2(2) - m2(2))+1; 
temp = zeros([s1, s2],('uint8')); 
p1 = 0; 
p2 = 0; 
for c1 = 1:m 
    if ((c1 <= m1(2)) && (c1 >= m2(2))) 
     for c2 = 1:n 
      if ((c2 <= m1(1)) && (c2 >= m2(1))) 
       temp(p1, p2) = manip(c1, c2); 
      end 
      p2 = p2 + 1; 
     end 
    end 
    p1 = p1 + 1; 
end 
out = temp; 



這裏是我的結果:
results of the cropping

的我做錯了什麼任何想法,我可以的似乎能看見。謝謝。

回答

1

我會想象你的錯誤在這裏:s2 = (m2(2) - m2(2))+1;這不應該是s2 = (m2(2) - m1(2))+1;

但是你並不需要這個循環在所有:

Iold = rand(300); 
%crop 10 pixels off each side 
Inew = Iold(11:end - 10, 11: end - 10); 

,或者如果您需要在裁剪位圖像大小相同,但以零:

Inew = zeros(size(Iold)); 
Inew(11:end - 10, 11: end - 10) = Iold(11:end - 10, 11: end - 10);  

或一概而論它:

Inew(xmin:xmax, ymin:ymax) = Iold(xmin:xmax, ymin:ymax);