Converting grayscale image to indexed
조회 수: 5 (최근 30일)
이전 댓글 표시
I have a grayscale image with 3 random values signified by integers 91, 123, 255. When I use gray2ind(I, 3) I get only 2 indices: 1 and 2. But when I use gray2ind(I, 2) I get 0 and 1 as expected. How come? I would expect gray2ind(I, 3) to give me 0, 1 and 2.
I do have a colormap array which I am planning to later use, which has 3 individual colors, such that after I would properly convert this to 0, 1 and 2 indices I'd just do imshow(gray2ind(I, 3), colors).
Thanks
댓글 수: 0
채택된 답변
Image Analyst
2018년 8월 19일
Don't worry about it. You can use a grayscale image as an indexed image as-is. Just set up the colormap based on the grayscale and set the map for whatever colors you want for the special values.
grayImage = imread('pout.tif');
% Initialize a normal colormap for a normal gray scale image.
cmap = gray(256);
% Set up special colors for 91, 123, 255.
cmap(92, :) = [1, 1, 0]; % Gray scale 91 will show up as yellow.
cmap(124, :) = [1, 0, 1]; % Gray scale 123 will show up as magenta.
cmap(256, :) = [1, 0, 0]; % Gray scale 255 will show up as red.
imshow(grayImage, cmap);
colorbar
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
However, note that the index (row) of the colormap you want to use is one more than the gray level, because row 1 controls the color of gray level 0, row 2 controls the color of gray level 1, and so on until row 256 controls the color of gray level 255.
댓글 수: 4
Image Analyst
2018년 8월 19일
Try this:
grayImage = imread('pout.tif');
% Quantize into 3 gray levels.
qImage = imquantize(grayImage, [91, 123, 255]);
unique(qImage)
% Set up a color map for color 1 valid for GL <=91,
% color 2 valid for GL >= 92 up to GL <= 123,
% and color 3 valid for GL >= 124 up to GL <= 255.
cmap = [1, 1, 0;
1, 0, 1;
1, 0, 0];
imshow(qImage, cmap);
colorbar
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!