执行圆形裁剪时出错

时间:2016-11-17 10:34:36

标签: matlab

在运行此代码时,我收到错误消息:

  
    

指数超出矩阵维度。

  
     

裁剪错误(第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);

请帮助,因为我无法调试此错误。

1 个答案:

答案 0 :(得分:1)

替换

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

通过

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

这将确保应用蒙版,无论I是灰度还是RGB