2015-10-10 23 views
0
I = imread('data1.jpg') 
[p3, p4] = size(I); 
q1 = 50; % size of the crop box 
i3_start = floor((p3-q1)/2); % or round instead of floor; using neither gives warning 
i3_stop = i3_start + q1; 

i4_start = floor((p4-q1)/2); 
i4_stop = i4_start + q1; 

I = I(i3_start:i3_stop, i4_start:i4_stop, :); 
figure ,imshow(I); 

我已經運行該代碼,並得到這個錯誤「指數超過矩陣尺寸im matlab中的新手。所以我需要幫助。 IM嘗試做到這一點的編碼,但有此錯誤

錯誤==> croptry在10 I = I(i3_start:i3_stop ,i4_start:i4_stop, :);「

有人能幫我解決這個錯誤嗎?我想在中心裁剪圖像

回答

1

該錯誤可能是由於您稱爲函數size的方式。

如果在其中加載圖像的矩陣I是三維(N×M個X K),你必須調用size這種方式:

[p3, p4, p5] = size(I) 

即,是通過將一個額外的參數(在那種情況下「p5」)。

如果你打電話size爲:

[p3, p4] = size(I) 

P4將被設置爲你的矩陣I

更新代碼的第二個和第三個維度的產品

I = imread('pdb_img_1.jpg'); 
% Modified call to "size" 
% [p3, p4] = size(I) 
[p3, p4, p5] = size(I) 
% Increased the size of the "crop box" 
q1 = 150; % size of the crop box 
i3_start = floor((p3-q1)/2) % or round instead of floor; using neither gives warning 
i3_stop = i3_start + q1 

i4_start = floor((p4-q1)/2) 
i4_stop = i4_start + q1 

I = I(i3_start:i3_stop, i4_start:i4_stop, :); 
figure 
imshow(I) 

原始圖片

enter image description here

裁剪圖片 enter image description here 希望這有助於。

+0

非常感謝你,因爲幫助我解決了這個錯誤。有用。謝謝你 – user5405704

+0

不客氣!快樂我一直在使用你。也許您可能想要接受關閉問題的答案:-) –

+0

@ user5405704考慮接受答案爲有效;)https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-工作 –