2016-11-17 32 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考虑接受答案,如果它有帮助 –