Save an intensity image created with imagesc with true resolution
52 views (last 30 days)
Show older comments
Hello,
I have a pixel array 2560*2160 with intensity values from 0 to 5070 and I want just an image file (with the true resolution of 2560*2160)(bmp,png,jpg, whatever) with a nice display of this array.
I can get a good image with:
imagesc(data,'CDataMapping','scaled');
but I fail to save the outputed image in true resolution. Saveas and print don't work because it gets really messy with dpi and papersize.
The only thing close to what I want is by using:
data=data/5070;
imwrite(data, 'filename.png')
but then my image is of course only in grayscale, because the colormap is wrong.
I think there is a way using parula and get the right colormap from the image created with imagesc, but I just cant figure out how to combine this with imwrite.
Thanks for your help!
0 Comments
Accepted Answer
Walter Roberson
on 16 May 2017
imwrite( uint16(YourData), parula(5070), filename)
or
imwrite( ind2rgb(im2uint8(mat2gray(YourData)), parula(256)), filename)
3 Comments
More Answers (3)
Image Analyst
on 15 May 2017
I think you didn't look at the help for imwrite(). In there it says:
imwrite(A,map,filename) writes the indexed image in A and its associated colormap, map, to the file specified by filename.
So, don't divide your image by 5070, just pass in parula(256) for the map input.
0 Comments
Simon Streit
on 16 May 2017
4 Comments
Walter Roberson
on 16 May 2017
In my test,
YourData = pixels;
filename = 'testmat.png';
imwrite( ind2rgb(im2uint8(mat2gray(YourData)), parula(256)), filename)
worked well, producing the same output as
imagesc(pixels);
colormap(parula(256));
DGM
on 15 Jul 2024
For maps of length 256 or 65536, the workaround using mat2gray()/ind2rgb() might be fine. There are things to consider. The quantization style is not the same as used by imagesc(). For these long maps, that won't be visually noticeable, but for shorter map lengths, it may be. Likewise, shorter maps would need a slightly different workaround, since the given examples rely on implicitly scaling the input data to a native integer scale equal in length to the colormap (e.g. uint8 for map length of 256).
Both of these complications can be avoided. MIMT has gray2pcolor(), which can do scaled colormapping of arbitrarily-scaled data, given a colormap and map extents (default is data extrema). The quantization style can be user-selected. While the version included with MIMT offers multiple style options, attached is a simplified copy which offers the two most relevant ones.
% arbitrarily-scaled data
S = load('data.mat'); % float, range [52 5070]
% apply a colormap using the same quant style used by imagesc()
% mapping spans the range of the data (imagesc() behavior)
CT = parula(256);
outpict = gray2pcolor(S.pixels,CT,'cdscale');
% show it
imshow(outpict)
It's easier to demonstrate the difference between the two if we use a shorter map:
% a simple linear ramp
inpict = repmat(linspace(52,5070,500),[50 1]);
% compare the two main quantization styles
CT = parula(16); % a short CT
A = gray2pcolor(inpict,CT,'default'); % as ind2rgb() would do it
B = gray2pcolor(inpict,CT,'cdscale'); % as imagesc() would do it
% show them together
imshow([A;B])
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!