How to open a 12-bit .raw image file from Matlab

Hello guys,
Firt timer here asking a question. I have a 12-bit .raw image file save from our experiment - this normall consist of up to 10,000 frames.
I am trying to us matlab to read this .raw file and reconstruct it back into frame images, e.g. Tiff ones. But, as you may expect already, I kept on failing. I have attached a single frame example here.
What I tried is to reconstruct this single frame first.
file=fopen('Example.raw','r');
ImgFile=fread(file,'*ubit12');
Just to read this what I got is a 2621440x1 single array unit 16. My image is 1280x1024, so for some reason I dont know, the element count doubled. I tried some method to pick 1310720 elements out and use reshape to get an image, but never got what I need.
Some assistance here would be greatly appreciated. cheers

6 Comments

Are you sure that's how the data is formatted?
% given file size in bytes,
% this is how many 12-bit words are expected
hex2dec('3C0000')/1.5
ans = 2621440
% given file size in bytes,
% this is how many bytes are available for representing each pixel
hex2dec('3C0000')/(1280*1024)
ans = 3
So the data returned by fread() for a ubit12 format assumption is the correct length. As there are a total of 24 bits per pixel, this either means that the image is a dual-channel image at 12bit per sample, or it's a 3-channel image at 8bit per sample.
What is the object content of the image supposed to look like? I haven't come up with any rearrangement that doesn't just look like sensor noise.
if use
clc; close all; clear all
file=fopen('Example.raw','r');
ImgFile=fread(file,'*ubit12');
fclose(file);
a = reshape(ImgFile, 1024, 1280, 2);
figure; imshow(a(:,:,1), [])
get the figure,is it your image?
Thanks for answering.
I double checked and realised the original file, which I though is a single frame raw file, could be containing two frames. Maybe that explains while we have 2x the elements?
Nevertheless, I still couldn't extract any image out of these raw files. Here attached are the raw file I just saved and the corresponding TIFF images.
Hopefully this helps.
Cheers
Does the equipment have any documentation? There appear to be 8 blocks of data in the file, probably because this is striped data from the sensor. The bit order doesn't even appear to be consistent, and I'm not even sure what order the blocks are supposed to be in without just guessing.
For example:
fid = fopen('RAW.raw','r');
D = fread(fid,'*ubit12','b');
fclose(fid);
A = D(1:2:end);
A = reshape(A,1280,[]);
imshow(A,[])
fid = fopen('RAW.raw','r');
D = fread(fid,'*ubit12','l'); % opposite bit order??
fclose(fid);
B = D(2:2:end);
B = reshape(B,1280,[]);
imshow(B,[])
Thank you for the efforts DGM.
Good news is that at least we are seeing the images, though in several blocks. I am still confused on how to reconstructing them into one and there seem like no documentation illustrating how the datas are packed.
How would you know the number of frames in the .raw image? I have a .raw image that contains many frames but I need to know how many are they. Any idea ?
Thanks

Sign in to comment.

Answers (0)

Products

Release

R2019b

Asked:

on 21 Dec 2021

Moved:

DGM
on 6 Jul 2024

Community Treasure Hunt

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

Start Hunting!