Save an intensity image created with imagesc with true resolution

52 vues (au cours des 30 derniers jours)
Simon Streit
Simon Streit le 15 Mai 2017
Réponse apportée : DGM le 15 Juil 2024
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!

Réponse acceptée

Walter Roberson
Walter Roberson le 16 Mai 2017
imwrite( uint16(YourData), parula(5070), filename)
or
imwrite( ind2rgb(im2uint8(mat2gray(YourData)), parula(256)), filename)
  3 commentaires
Simon Streit
Simon Streit le 16 Mai 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!

Connectez-vous pour commenter.

Plus de réponses (3)

Image Analyst
Image Analyst le 15 Mai 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 le 16 Mai 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 commentaires
Simon Streit
Simon Streit le 16 Mai 2017
Modifié(e) : Simon Streit le 16 Mai 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 le 16 Mai 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));

Connectez-vous pour commenter.


DGM
DGM le 15 Juil 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])

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by