How can I write an image of datatype uint16 to a file in MATLAB? I try to write to the file using the following command, but it gives me an error
imwrite(pimg, 'h44', 'jpg')
Error using writejpg>set_jpeg_props (line 183)
UINT16 image data requires bitdepth specifically set to
either 12 or 16.
Error in writejpg (line 49)
props = set_jpeg_props(data,varargin{:});
Error in imwrite (line 472)
feval(fmt_s.write, data, map, filename,
paramPairs{:});
Error in image16bit (line 666)
imwrite(imgnew1,'h44','jpg' );
解决方案
You must specify the BitDepth option when saving JPEG images with more than 8-bits:
% note that 16-bit only accepts grayscale images
img = imread('peppers.png');
imwrite(im2uint16(img), '12bit.jpg', 'BitDepth',12);
imwrite(rgb2gray(im2uint16(img)), '16bit.jpg', 'BitDepth',16);
Unfortunately, not many programs have support for 12-bit/16-bit JPEG images, so you might not be able to open it externally. You could use the JPEG2000 format instead:
imwrite(im2uint16(img), 'out.jp2');