MATLAB: Question about rgb image

colorizeimage processingImage Processing Toolbox

Hi, i have question regarding to imshow a rgb image:
rect =[ ...];
i=image_newC+abs(min(min(image_newC))); % i is a grayscale image
i=imcrop(i,rect);
n = size(unique(reshape(i,size(i,1)*size(i,2),size(i,3))),1);
rgb = ind2rgb(gray2ind(i,n),jet(n));
imshow(rgb,[]); *???why output image is entirely red?*
The purpose is to convert grayscale image to rgb image, i don't know why outcome is not expected. One more thing, is i tried to show it in this way:
imshow(i,[]);
colormap(jet(255));
but i don't know how to access the rgb image if i want to further perform some operations on it. Any way to store it in a variable? Sorry, i am new to Matlab.
Thanks a lot for your help.

Best Answer

Wow! That's about the most complicated, convoluted way of converting a gray scale image to RGB I've ever seen. If image_newC is a unint8 gray scale image, you can simply do
rgbImage = cat(3, image_newC, image_newC, image_newC);
If you want an RGB image with some colormap applied, just as jet(256), do this:
rgbImage = ind2rgb(image_newC, jet(256));
Please see my attached demos if you want to colorize a portion of the image but not the whole image.
Related Question