为什么MATLABs“imwrite”缩放12位图像以及如何规避这个? [英] Why does MATLABs "imwrite" scale 12-bit images and how to circumvent this?

查看:437
本文介绍了为什么MATLABs“imwrite”缩放12位图像以及如何规避这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图像是以 N x M - 12位数据的矩阵,我想使用 imwrite 将图像保存为 .pgm 文件。

I have an image given as an N x M - matrix with 12-bit data and I want to use imwrite to save the image as a .pgm file.

为什么MATLAB将图像缩放到16位?我怎么能绕过这个呢?

Why does MATLAB scale the image to 16bit? How can I circumvent this?

使用'MaxValue'参数似乎也会改变图像,因为之后无法正确显示例如在IrfanView。

Using the 'MaxValue' argument also seems to alter the image, as it can't be displayed properly afterwards e.g. in IrfanView.

推荐答案

MaxValue 参数有点直观。它确实指定写入标记有特定深度的最大值的PGM(例如,此处为12位),但它也告诉 iwwrite 重新缩放数据。重新缩放发生在 writepnm> remap_pixel_values

The MaxValue parameter is a little counter intuitive. It does specify to write the PGM tagged with a max value of a certain depth (e.g. 12-bit here), but it also tells iwwrite to rescale the data. The rescaling happens in writepnm>remap_pixel_values:

function newdata = remap_pixel_values(data, maxval)
%REMAP_PIXEL_VALUES Remap pixel values in array of pixel values.
%
%   NEWDATA = REMAP_PIXEL_VALUES(DATA, MAXVAL) remaps the pixel values in
%   DATA as follows
%
%   Class of DATA   Input                    Output
%   -------------   -----                    ------
%   uint8           The set {0,1,...,255}    The set {0,1,...,maxval}
%   uint16          The set {0,1,...,65535}  The set {0,1,...,maxval}
%   double          The interval [0,1]       The set {0,1,...,maxval}

所以 uint16 数据,它将通过比特移位 bitshift应用 65535 / maxval 的比例来重新调整数据。数据,-4); 。您不希望它重新缩放数据,但您还希望它将文件写为12位(这发生在 writepnm> write_raw_data 中。解决方法是应用在调用 imwrite 之前的相反比例:

So with uint16 data, it will rescale the data by applying a scale of 65535/maxval via the bit shift bitshift(data,-4);. You don't want it to rescale the data, but you also want it to write the file as 12-bit (this happens in writepnm>write_raw_data. The workaround is to apply the opposite scale before calling imwrite:

Iscaled = uint16(double(I)*(2^16-1)/(2^12-1))
imwrite(Iscaled,'test.pgm','MaxValue',2^12-1)

请注意,您可以使用在[0,1]之间缩放的双精度值,根据上面代码注释中的表格。

Note that you could use double values scaled between [0,1] too, according to the table in the above code comments.

对于读取 12位PGM / PPM,请参阅这里

For reading 12-bit PGM/PPM, see here.

这篇关于为什么MATLABs“imwrite”缩放12位图像以及如何规避这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆