How to open .raw files in matlab?
Show older comments
Hello,
How will I open my .raw files in matlab?
Accepted Answer
More Answers (3)
Image Analyst
on 18 Apr 2013
1 vote
Pooja, do you have any idea at all ...
- what the lateral size of the image is (number of rows and columns), and
- whether it's grayscale, RGB color or multispectral,
- if color how the red, green, and blue values are positioned (interleaved)
- how many bits (8 or 16 are typical) is the pixel written out
- if there is a header and how long it might be?
Or are you willing to guess at some typical values? You can pass in an array to the fread() function to get back a 2D array.
8 Comments
Pooja
on 19 Apr 2013
Image Analyst
on 19 Apr 2013
Are you sure #1 is the size of the image, and not the max number of gray levels? 256 might be a common size but not 255. You need to find out more, or start guessing.
Pooja
on 19 Apr 2013
Walter Roberson
on 19 Apr 2013
imshow() or image() -- but first you need to reshape() the data to the proper size, and so far you have not indicated how you know what the proper size is.
Pooja
on 21 Apr 2013
Image Analyst
on 21 Apr 2013
You don't get the size from a string (which is the filename)! 'mars.raw' is a 1 by 9 array of characters. You need to specify the size, which may mean guessing. See this snippet and I hope you'll then understand:
% Read in image2D image data
% Get original, full-sized 2-D slice.
% Note: fread() requires that x_size and y_size be doubles.
% Note: fread() will return a 2D array if you pass in a 2D array for the number of bytes, as in [x_size y_size].
if stHeader.BytesPerVoxel == 1
oneSlice = fread(fileHandle, [x_size y_size], '*uint8');
elseif stHeader.BytesPerVoxel == 2
oneSlice = fread(fileHandle, [x_size y_size], '*int16'); % It will be a 2D array after this.
else
error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
end
Pooja
on 21 Apr 2013
Image Analyst
on 21 Apr 2013
As you can see from the '*uint8' or '*uint16' in the fread() statement, it's either 1 or 2, for 8 bit (one byte) or 16 bit (2 bytes) respectively. x_size will be the number of columns, and y_size is the number of rows that you want in your 2D array (which I called oneSlice but you can call rawdata if you want).
Now, notice how your fread() doesn't look like mine because you didn't put in the desired output matrix size ([x_size y_size]) like I did. Why not?
Walter Roberson
on 18 Apr 2013
0 votes
There are different .raw file formats, different ones for different manufacturers.
2 Comments
Pooja
on 18 Apr 2013
Walter Roberson
on 18 Apr 2013
The most I can seem to find is the commercial product from ledin.com which is a Telemetry toolbox for MATLAB.
I have not checked to see if that handles DELTA-DOR files.
Jan
on 18 Apr 2013
0 votes
"Raw" means an individually defined binary format usually. Because there are many different formats named "raw", you have to ask the creator how this file has been created and how the format is defined. But it is not reliable to let the forum's users guess, which RAW-format it might be.
11 Comments
Pooja
on 18 Apr 2013
Walter Roberson
on 18 Apr 2013
fid = fopen('YourFile.raw');
rawdata = fread(fid, inf, '*uint8');
fclose(fid);
Now what?
Jan
on 18 Apr 2013
Of course there is a way. Of course you can read the file byte by byte, but this does not allow to interprete the data. Perhaps the format of the file looks like:
4 UINT8 values, 16 INT32 values, 1 UINT8 which defines the number of the following characters, then this number of characters, then 16'000 doubles.
Or:
32'000 UINT16 values
Or what ever. "RAW" means, that the data are not totally processed and the none of the standard formats are used. This is all we know. So the only way to get these data imported is asking the author for all necessary details. When he really claims, that there "is a way" and you do not get any useful information, delete the RAW files and start another project.
Pooja
on 19 Apr 2013
Pooja
on 21 Apr 2013
Jan
on 21 Apr 2013
@Pooja: I cannot suggest an improvement based on "imshow() is not working". So you specified, that you have images of different bit-depth. And now? How could this information be useful?
Surendra Maharjan
on 3 Feb 2018
Could you please tell me how many .raw files are there with some references?
Surendra Maharjan
on 3 Feb 2018
Why are the pixel values in the images are different before and after fread in MATLAB? I would like to fread .raw image 16 bits with pixel values range from 0 to 2pi. But the pixel values after fread are different. Can you please help me?
Image Analyst
on 3 Feb 2018
Attach your image and the code where you read in the raw image, and the values you think the first few pixels should have so we can compare those against what is read in with fread().
Surendra Maharjan
on 11 Oct 2018
clear; clc; width = 256; height = 256; fileID = fopen('Phase0004.raw','r'); A=fread(fileID,[width height]); fclose(fileID);
The output A is empty. I can not understand why?
zinah n
on 24 Sep 2019
clear; clc;
width = 256;
height = 256;
ccnt=width*height;
fileID = fopen('Phase0004.raw','r');
A=fread(fileID,ccnt,'uint8=>uint8');
fclose(fileID);
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!