How to read JPEG image in 12 bit or 16 bit format??

8 Ansichten (letzte 30 Tage)
saurabh
saurabh am 29 Jul. 2013
Bearbeitet: DGM am 6 Jul. 2024
I am new to matlab as well as to community?
I want to read any JPEG image in 12 bit or 16 bit format.... I am not sure if it's possible or not.
I am trying X = imread('filname.jpg');
Thanks, Saurabh

Antworten (2)

Walter Roberson
Walter Roberson am 29 Jul. 2013
Yes, imread() supports 12 bit lossy format, and 12 and 16 bit lossless format.
When I look around, I do not see at the moment whether in 12 bit format it would be the leading or trailing 4 bits (of 16) that would go unused. I would tend to suspect it would be the trailing 4 bits (of 16)

DGM
DGM am 6 Jul. 2024
Bearbeitet: DGM am 6 Jul. 2024
I've tested this in older versions, so it should have been fine in 2013. See
Note that the available bitdepths depend on the number of image channels. Reading or writing 16b JPG is supported only for single-channel images, and is only supported for lossless encoding.
% an RGB image (uint8)
inpict = imread('peppers.png');
% create a test 12b JPG
% input must be unit-scale float
fname = 'test.jpg';
imwrite(im2double(inpict),fname,'bitdepth',12) % lossy, downsampled
%imwrite(im2double(inpict),fname,'bitdepth',12,'mode','lossless') % lossless
% read it back
recovered = imread(fname);
% show the image
imshow(recovered)
% the class and range of the data
class(recovered)
ans = 'uint16'
getrangefromclass(recovered)
ans = 1x2
0 65535
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[min(recovered(:)) max(recovered(:))] % uint12-scale
ans = 1x2
0 4095
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
It's almost impossible to see against the white page background, but there's an image there. It's just dark. Why is it dark? Well, the data is uint12-scale, and it's stored in uint16. The data maxes out at 4095, but "white" is at 65535 in uint16.
There is no native 12b integer class, so any representation of integer data in the scale [0 4095] will be improperly-scaled for its class, and you'll have to deal with all the problems that entails.

Community Treasure Hunt

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

Start Hunting!

Translated by