用浮点值写一个图像 [英] Writing an image with floating point values
问题描述
实际上可以适应64位的像素数据(即,表示一个像素数据所需要的位数)rel =, nofollow noreferrer>双精度浮点值)转换为某些图像格式,具体来说就是 PNG 。而且你甚至可以准确地恢复数据。关键是将所有内容编码为 uint16
的值,并使用 Truecolor RGB图像(16位数据的3个颜色平面)以及alpha透明度图(另外16位)。这是编码步骤:
pre $ data $ rand(4); %只是一些示例数据,一个小的4×4矩阵
hexData = num2hex(data(:)); %获取每个像素的16位十六进制代码
C = mat2cell(hexData,numel(data),[4 4 4 4]); %将十六进制代码拆分为4组4
C = cellfun(@(c){uint16(hex2dec(c))},C); %将每个十六进制值转换为16位整数
colorData = reshape([C {1:3}],[size(data)3]); %4×4×3 uint16颜色数据
alphaData = reshape(C {4},size(data)); %4-by-4 uint16 alpha数据
imwrite(colorData,'double_data.png','Alpha',alphaData); %保存图片
您现在拥有一个有效的图片文件,你看它。现在,你可以这样解码:
[imColor,〜,imAlpha] = imread('double_data.png'); %load image
imSize = size(imColor); %获取图像大小
imHex = [dec2hex(imColor(:,, 1))...%将每个uint16转换为十六进制并连接
dec2hex(imColor(:,, 2))。 。
dec2hex(imColor(:,:,3))...
dec2hex(imAlpha)];
imdata = reshape(hex2num(imHex),imSize(1:2)); %重现数据
以及相等检查:
>> isequal(imdata,data)
ans =
逻辑
1%这是相同的!
My code reads an RGB image, processing it to produce floating-point results. How can I write these values as an image, maintaining these floating-point values, in Matlab 7.6.0 (R2008a)?
It is actually possible to fit 64 bits of pixel data (i.e. the number of bits needed to represent a double-precision floating-point value) into certain image formats, specifically a PNG. And you can even recover the data exactly. The key is to encode everything as uint16
values and use a Truecolor RGB image (3 color planes of 16 bit data) along with an alpha transparency map (another 16 bits). Here's the encoding step:
data = rand(4); % Just some sample data, a small 4-by-4 matrix
hexData = num2hex(data(:)); % Get the 16 digit hex codes for each pixel
C = mat2cell(hexData, numel(data), [4 4 4 4]); % Split the hex codes into 4 groups of 4
C = cellfun(@(c) {uint16(hex2dec(c))}, C); % Convert each hex value into a 16 bit integer
colorData = reshape([C{1:3}], [size(data) 3]); % 4-by-4-by-3 uint16 color data
alphaData = reshape(C{4}, size(data)); % 4-by-4 uint16 alpha data
imwrite(colorData, 'double_data.png', 'Alpha', alphaData); % Save image
You now have a valid image file, although it'll probably looks like random garbage if you view it. Now, you can decode it like so:
[imColor, ~, imAlpha] = imread('double_data.png'); % Load image
imSize = size(imColor); % Get image size
imHex = [dec2hex(imColor(:, :, 1)) ... % Convert each uint16 to hex and concatenate
dec2hex(imColor(:, :, 2)) ...
dec2hex(imColor(:, :, 3)) ...
dec2hex(imAlpha)];
imdata = reshape(hex2num(imHex), imSize(1:2)); % Reproduce data
And an equality check:
>> isequal(imdata, data)
ans =
logical
1 % it's the same!
这篇关于用浮点值写一个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!