MATLAB-索引超过矩阵维度误差



我试图将图像拆分为3个通道

img = imread('canoe.tif'); % Read image
red = img(:,:,1); % Red channel
green = img(:,:,2); % Green channel
blue = img(:,:,3); % Blue channel
a = zeros(size(img, 1), size(img, 2));
just_red = cat(3, red, a, a);
just_green = cat(3, a, green, a);
just_blue = cat(3, a, a, blue);
back_to_original_img = cat(3, red, green, blue);
figure, imshow(img), title('Original image')
figure, imshow(just_red), title('Red channel')
figure, imshow(just_green), title('Green channel')
figure, imshow(just_blue), title('Blue channel')
figure, imshow(back_to_original_img), title('Back to original image')

错误出现在第三行。通常,这种例外会发生在诸如数组之类的事情上,对吗?为什么会在这里发生?为什么第二行没有错误?

可能是由于您正在阅读的TIFF图像以灰度格式或索引颜色编码的事实。在这些情况下,图像像素数据仅包含一个通道:灰度格式的灰度梯度和索引颜色格式的调色板索引。有关后者的更多信息,请阅读以下问题:带有颜色的单个通道PNG,还提供了解决方案。

这就是为什么在尝试访问图像的第二个通道时会遇到错误的原因……因为它不存在。打开图像数组并验证。

最新更新