2016-11-17 31 views
-2

在運行這段代碼我得到一個錯誤說:錯誤而執行圓形作物

指數超過矩陣尺寸。

錯誤在作物(第8行)

croppedImage(:,:,2)= I(:,:,2)。*掩模;

I = imread('cameraman.tif'); 
imageSize = size(I); 
ci = [100,100,20];  % center and radius of circle ([c_row, c_col, r]) 
[xx,yy] = ndgrid((1:imageSize(1))-ci(1),(1:imageSize(2))-ci(2)); 
mask = uint8((xx.^2 + yy.^2)<ci(3)^2); 
croppedImage = uint8(zeros(size(I))); 
croppedImage(:,:,1)=I(:,:,1).*mask; 
croppedImage(:,:,2)=I(:,:,2).*mask; 
croppedImage(:,:,3)=I(:,:,3).*mask; 
imshow(croppedImage); 

請幫助,我無法調試這個錯誤。

+2

看起來不像C代碼? – acraig5075

+2

matlab,可能嗎? –

+0

因爲.....它不是RGB圖像,而是灰度圖像?..... –

回答

1

更換

croppedImage(:,:,1)=I(:,:,1).*mask; 
croppedImage(:,:,2)=I(:,:,2).*mask; 
croppedImage(:,:,3)=I(:,:,3).*mask; 

通過

croppedImage=bsxfun(@times,I,mask); 

這將確保該應用掩碼I是灰度或RGB

+0

非常感謝您的幫助, – Shirley

+0

@Shirley考慮接受答案,如果它有幫助 –