Using imwrite() to create a .bmp file with custom colormap

I am trying to use the imwrite() function to create and save a .bmp file using a custom colormap, 'myMap'. The colormap contains values between 0 and 1 and is a variable of type double with size [51 3]. My image matrix is of type double and contains values 0 to 1. When I produce this image with imagesc() it comes out fine:
figure
imagesc(myImage)
axis equal
axis tight
colormap(myMap)
However, when I use imwrite to save it as a .bmp file the image is saved in all one dark grey color:
imwrite(myImage, myMap, 'grid.bmp')
I have tried converting myImage to uint8 format and scaling myMap into [256 3] size, but neither works. What am I doing wrong? Thanks.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Dez. 2016
The colormap for the imwrite must be in the range 0 to 1.
When you imwrite with a colormap the data must be integral. If it is type double or single then 1 is subtracted from it and the result is converted to uint8. In other words the values must already be colormap indices.
You need:
Numcolor = size(myMap, 1);
Imgmin = min(MyImage(:)) ;
Imgmax = max(MyImage(:)) ;
mappedImage = uint8( (MyImage-Imgmin)./(Imgmax-ImgMin).* (Numcolor-1) ) ;
Then imwrite(mappedImage, myMap,...)

Weitere Antworten (1)

Image Analyst
Image Analyst am 25 Dez. 2016
Bearbeitet: Image Analyst am 25 Dez. 2016
You can convert your image to RGB with ind2rgb():
rgbImage = ind2rgb(myImage, myMap);
imwrite(rgbImage, filename);

7 Kommentare

Using this solution I get the error:
Error using dither>parse_inputs (line 92)
DITHER(RGB,map): the RGB image has to be a three-dimensional array.
Error in dither (line 47)
[X,m,qm,qe] = parse_inputs(varargin{:});
Error in rgb2ind (line 106)
X = dither(RGB,map);
Try ind2rgb() instead of rgb2ind().
That will not work as the user's original data is not indices, it is data with an arbitrary range. Notice they used imagesc() to display it.
Right, you have to normalize like you did. This will work:
myImage = im2double(imread('cameraman.tif'));
% myImage is floating point in the range 0-1
myMap = hsv(256); % Whatever one you want.
% Convert to gray levels (indexes):
myImage = uint8(255 * myImage);
% Now convert to RGB color.
rgbImage = ind2rgb(myImage, myMap);
imshow(rgbImage);
If you want to scale the min to 0 and max to 255, you can use mat2gray():
myImage = uint8(255 * mat2gray(myImage));
They are working with data not an image so they need to normalize to 0 to 1 and then to integer indices like I coded
Why do you say it's non-image data when she called the variable "myImage", used imagesc() to display it, and tried to save it with imwrite()? As far as I can guess, it's a floating point (double) image. Anyway, my code also normalizes and converts to integer indices like yours, just using an alternate method.
Hi. I want ask a question.
How do I save the original image jpg from figure for colormap?
Here my codes:
>> I=imread('M_92_ar_2001743_st01_currentsize_200x200.jpg');
>> imshow(I);
>> imfinfo('M_92_ar_2001743_st01_currentsize_200x200.jpg');
>> I2=I(:,:,1);
>> imshow(I2);
>> map = [0, 0.3, 0
0, 0.4, 0
0, 0.5, 0
0, 0.6, 0
0, 0.8, 0
0, 1.0, 0];
>> imshow(I2);
>> colormap(map);
>> imwrite(I2,map,'cmalignant200.jpg');

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Color and Styling finden Sie in Hilfe-Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by