Clear Filters
Clear Filters

Save an intensity image created with imagesc with true resolution

52 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 16 May 2017
imwrite( uint16(YourData), parula(5070), filename)
or
imwrite( ind2rgb(im2uint8(mat2gray(YourData)), parula(256)), filename)
  3 Comments
Simon Streit
Simon Streit on 16 May 2017
Your second line of code works! I just had no access to the image processing tool, but now it's all fine!
Thank you very much!

Sign in to comment.

More Answers (3)

Image Analyst
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.

Simon Streit
Simon Streit on 16 May 2017
Thank you for your answers!
I tried a lot of combinations, but none of them worked:
Just passing parula(256) doesn't get the colormap right for me, because the image gets just way to 'bright'.
Using uint16 conversion only helps if I use a .tif-file, which is not easy to handle. I don't know much about imagefile formats, so I am probably missing the point of my problem, but here are the results:
On the left is what my figure looks like and what i would like to have as an image, the second is picture is saved with parula(256) and the last one with parula(5070) (allwithout conversion to uint16 because it didn't change my output):
Thank you for your patience!
  4 Comments
Simon Streit
Simon Streit on 16 May 2017
Edited: Simon Streit on 16 May 2017
The class is double. Here should be the data:
Edit:I tried to download my own data but it looks currupted. Reupload did not fix the issue. Are you able to download it or is there any other way than attaching it to this post? If it doesn't work here is a dropbox link: data
Walter Roberson
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));

Sign in to comment.


DGM
DGM on 15 Jul 2024 at 8:14
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])

Community Treasure Hunt

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

Start Hunting!