Problems reading .RAW file

Hello
I have a strange problem when trying to read in a grayscale image of 12bit. there are always strange gray background when I am trying to load them in Matlab. I wish only black background and white cross.
q10.jpg
this is image with strange grey background
image.png
this is the image I wish
Do you know what may be the reason for such a problem ?
Thanks in advance
my code
fid=fopen('image.dat','rt');
q2=fread(fid,[3840 2160],'*ubit12');
q10=uint16(q2');
fclose(fid);

5 commentaires

Jan
Jan le 8 Juil 2019
Modifié(e) : Jan le 9 Juil 2019
Why do you open the file in text mode? '*ubit12' should reply a UINT16 already, so you do not need the conversion.
Can you provide an example file?
Xie Yiru
Xie Yiru le 8 Juil 2019
Modifié(e) : Walter Roberson le 8 Juil 2019
which mode should I use to open the image file? I see a example, they use this mode. But I don't know where come the gray noisy.
Yes I can give you the raw file
Walter Roberson
Walter Roberson le 8 Juil 2019
It is common to pack 12 bits into 2 bytes and leave the other 4 bits 0, which would be a uint16 format instead of a ubit12 format.
Xie Yiru
Xie Yiru le 9 Juil 2019
yes i have tried. But I still get the image like the first one with grey background. Can you maybe try one time and tell me the code. I have raw File linkes
Jan
Jan le 9 Juil 2019
@Xie Yiru: "which mode should I use to open the image file?" - See Guillaume's answer: Without the 't' in fopen.

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 9 Juil 2019
Modifié(e) : Jan le 9 Juil 2019

1 vote

Your image is read incorrectly because you specified 't' in the fopen call. 't' means text mode and will replace some bytes sequences (specifically \r\n) by a different one (\r), which is certainly not what you want.
Without it, your image is read properly
fid = fopen('image.dat'); %you don't even need to specify 'r' since it's the default
q2 = fread(fid, [3840, 2160], '*ubit12');
fclose(fid);
imshow(q2, []);

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by