Map grayscale to color using colormap
In matlab you can view a grayscale image with:
imshow(im)
Which for my image im
shows:
And you can also view this grayscale image using pseudocolors from a given colormap with something like:
imshow(im,'Colormap',jet(255))
Which shows:
But it’s not obvious how to use the colormap to actually retrieve the RGB values we see in the plot. Here’s a simple way to convert a grayscale image to a red, green, blue color image using a given colormap:
rgb = ind2rgb(gray2ind(im,255),jet(255));
Replace the 255
with the number of colors in your grayscale image. If you don’t know the number of colors in your grayscale image you can easily find out with:
n = size(unique(reshape(im,size(im,1)*size(im,2),size(im,3))),1);
It’s a little overly complicated to handle if im
is already a RGB image.
If you don’t mind if the rgb image comes out as a uint8 rather than double you can use the following which is an order of magnitude faster:
rgb = label2rgb(gray2ind(im,255),jet(255));
Then with your colormaped image stored in rgb
you can do anything you normally would with a rgb color image, like view it:
imshow(rgb);
which shows the same as above:
Possible function names include real2rgb, gray2rgb.
'Matlab ' 카테고리의 다른 글
Matlab: Running an m-file from command-line (0) | 2017.02.06 |
---|---|
start MATLAB from a DOS window running inside Windows (0) | 2017.02.06 |
GraycoProps의 Contrast가 계산되는 방법 (0) | 2017.02.03 |
graycomatrix가 [0,255] 사이의 이미지 입력 시 [0,8]으로 변환되는 이유 (0) | 2017.02.03 |
Running MATLAB function from Java (0) | 2017.02.01 |