
how to add a custom colormap to 16 bit grayscale image without using imshow
7 views (last 30 days)
Show older comments
I have a 16 bit image and want to create a colormap based on pixel intensities.
I am interested in a narrow band betwen 0 and 6000 pixel intensity, and want to have a colormap on the image for further analysis on the picture such as adding annotations etc.
The only way I found that works, is by using imshow. The problem is that I don't want to display the image, I want to do further analysis.
Is there a way to do what imshow does, without actually showing the image?
imshow(image, 'DisplayRange',[0 6000],'Colormap',customColormap);
0 Comments
Accepted Answer
Image Analyst
on 19 Aug 2020
Try this. I made a 400 row by 600 column uint16 image with values from 1 to 6000. Then I took a range of values from 3000 to 3805 and pseudocolored them with the "jet" colormap. I used 3805 just to make it bigger so you can see it in the image, but feel free to use 3005 as long as you have enough pixels in that range to see a difference. Of course you could colorize outside that range with a different colormap if you want or even a colormap that's all black to blacken everything outside the desired range. Let me know if this meets your needs.
% Create sample data.
data = linspace(1, 6000, 400*600);
data = reshape(uint16(data), 400, 600);
% Show image in it's original gray scale.
subplot(2, 1, 1);
imshow(data, []);
colormap(gray(256));
colorbar;
title('6000 gray levels in gray scale', 'FontSize', 15);
% Show image where a specified range is in pseudocolor.
subplot(2, 1, 2);
cmap = gray(6000);
row1 = 3000; % Whatever.
row2 = 3805; % or 3005 or whatever you want.
% Make the jet colormap with the proper number of rows.
cmap2 = imresize(jet(row2-row1+1), [row2-row1+1, 3]);
% Paste that over the appropriate rows of the gray scale colormap.
cmap(row1:row2, :) = cmap2;
imshow(data, [], 'Colormap', cmap);
colorbar;
caption = sprintf('6000 gray levels with %d - %d in pseudocolor', row1, row2);
title(caption, 'FontSize', 15);

3 Comments
Walter Roberson
on 20 Aug 2020
rgb = ind2rgb(data, cmap);
title_x = 100; title_y = 350;
rgbtitled = insertText(rgb, [title_x, title_y], 'The rain in Spain');
Image Analyst
on 20 Aug 2020
insertText() burns the text into the image, changing the pixel values. text() places the text into the overlay, not altering the pixel values. Use whichever you want.
More Answers (1)
Image Analyst
on 14 Aug 2020
Yes, you can use ind2rgb() to make the rgb image. Then you don't have to display it if you don't want to, or you could display it with imagesc() or image() instead of imshow().
8 Comments
Walter Roberson
on 19 Aug 2020
I want to visualize the difference between these using a colormap that can show difference between, for example, 3000 and 3005, and afterwards add text to the colormap using insertText.
Generally speaking, you can use ind2rgb() or other forms of lookup table in order to create a color array representing the data. Then you can use Computer Vision insertText() to draw text into that array if desired.
The question at this time would be how you want to represent the color scheme.
See for example https://www.mathworks.com/matlabcentral/fileexchange/69470-custom-colormap which permits you to define relative positions (on the range 0 to 1) and corresponding colors, and will interpolate between them to the number of locations you specify. If you specified 6001 then you could use ind2rgb() with the map you had created. Otherwise you could use three interp1 calls.
Indeed, if you have a list of positions and rgb components and are willing to interpolate between them, then you can use three interp1() calls directly without using the FEX contribution. The first parameter would be the positions of change; the second parameter would be the color component value at those locations; the third parameter would be the image value.
See Also
Categories
Find more on White in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!