0
votes

I am trying to split a image into 3 channels

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')

And the error comes at the third line. usually this exception will happens to something like an array, right? Why it would happens here? And why the second line doesn't get the error?

1
If my answer helped you in solving your problem, please accept it. Thanks! - Tommaso Belluzzo

1 Answers

1
votes

It's probably due to the fact that the TIFF image you are reading is encoded in grayscale format or with indexed colors. In these cases, the image pixel data only contains one channel: grayscale gradient for grayscale format and palette indices for indexed colors format. For more informations concerning the latter, read this question: Single channel png displayed with colors, which also offers a solution.

This is why you are getting that error when trying to access the second channel of the image... because it doesn't exist. Open your image array and verify that.