Save an intensity image created with imagesc with true resolution
57 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2017-5-16
imwrite( uint16(YourData), parula(5070), filename)
or
imwrite( ind2rgb(im2uint8(mat2gray(YourData)), parula(256)), filename)
更多回答(3 个)
Image Analyst
2017-5-15
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 个评论
Simon Streit
2017-5-16
4 个评论
Walter Roberson
2017-5-16
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
2024-7-15
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 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!