Problems reading .RAW file
Ältere Kommentare anzeigen
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.

this is image with strange grey background

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 Kommentare
Xie Yiru
am 8 Jul. 2019
Bearbeitet: Walter Roberson
am 8 Jul. 2019
Walter Roberson
am 8 Jul. 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
am 9 Jul. 2019
Jan
am 9 Jul. 2019
@Xie Yiru: "which mode should I use to open the image file?" - See Guillaume's answer: Without the 't' in fopen.
Antworten (1)
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, []);
1 Kommentar
Walter Roberson
am 9 Jul. 2019
Oh, good catch!
Kategorien
Mehr zu Image Arithmetic finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!