How to open .raw files in matlab?

 Accepted Answer

Even after the large number of questions for clarifications and a lot of comments, the actual problem does not get clearer. It seems, like our messages do no reach you, Pooja:
It is your turn to find out the details of the format.
We cannot help you, when we have to guess, what you need.

14 Comments

fid = fopen('jeen.raw')
rawdata = fread(fid, inf, '*uint8')
fclose(fid);
rawdata i got a value as 71424*1 uint8
is this helpful to do the reshape process and show the image.
Why not shape it AS you do the fread like I showed you: fread(fileHandle, [x_size y_size], '*uint8');??? Then to show it, use imshow(yourImage, []);
@Pooja: Did you explain the file format already anywhere? The file contains of 71424 bytes, but this could be a [2 x 35712] or a [17856 x 4] matrix. How could we or you know this?
@Jan i got 714424*1 uint8 from the workspace.Actually my problem is i am doing with project on image compression .So i need to give an uncompressed input image.
@Image Analyst :-
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
St.hEADER.bYTES PER VOXEL,file handle are all undefined functions.
What should functions should i give instead of these functions for my program
@Pooja: I do not see a relation between my question about the definition of the shape of the data and your comment. Even an uncompressed image needs a definition of its dimensions.
As I've said already: You do not offer enough information to allow an interpretation of your file. It is not helpful at all to import the data as bytes, when you do not reveal any details about the format the data are saved in. You can even read a JPEG-file as a vector of bytes. So ask the one, who has created the image about the correct method to read them.
The author has told only "There is a way to open raw files by assigning binary array and then read in from files".How can i share that file with you to solve my problem.
Pooja: Just replace the header with some known values, like
BytesPerPixel = 1; % Let's assume we're using 8 bit images.
% BytesPerPixel = 2; % Use 2 if you know you have 16 bit images.
x_size = 320; % You need to figure this number out - we don't know it.
y_size = 240; % You need to figure this number out - we don't know it.
if BytesPerPixel == 1
yourImage = fread(fid, [x_size y_size], '*uint8');
elseif BytesPerPixel == 2
yourImage = fread(fid, [x_size y_size], '*int16');
else
error('Unsupported BytesPerPixel %d', BytesPerPixel );
end
% yourImage will be a 2D array after this
% with x_size columns and y_size rows.
And I also changed the name of fileHandle to fid (what you used) since you didn't seem to know to do that. But again, like we've all been telling you over several messages, you need to figure out, or guess, on the number of rows (y_size) and number of columns (x_size). If you guess, you might get something that looks almost normal but skewed (sheared). Then you can just keep guessing until it looks right. Or better yet, just ask the person who created your image.
@Image Analyst :-Thank you so much for your patience.I did some guessing regarding the rows and columns but as you have said image was sheared.I Asked the author about it and the reply was "There is a way to open raw files by assigning binary array and then read in from files,consult your matlab expert".He used those raw files in C language .
Go to that page, and click on the link "Go back to list". There is a README.doc file at that level. Open it and you will find that it tells you the width and height and bits per pixel for each file (they are not all the same.)
Thank you so much.Got the o/p
if true
% row=1024; col=1024;
fin=fopen('b1.raw','r');
I=fread(fin,row*col,'uint8=>uint8');
Z=reshape(I,row,col);
Z=Z';
k=imshow(Z)
end
If the question is finished please Accept one of the Answers.

Sign in to comment.

More Answers (3)

Image Analyst
Image Analyst on 18 Apr 2013
Pooja, do you have any idea at all ...
  1. what the lateral size of the image is (number of rows and columns), and
  2. whether it's grayscale, RGB color or multispectral,
  3. if color how the red, green, and blue values are positioned (interleaved)
  4. how many bits (8 or 16 are typical) is the pixel written out
  5. 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

1.max of 255.
2.grayscale.
3.8bits(16 bits image is also available.).
4.Doesnt know about the header.
I need to see the figure also.
Help please.
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.
how to show the image?
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.
fid = fopen('mars.raw');
rawdata = fread(fid, inf, '*uint8');
fclose(fid);
[x,y]=size('mars.raw')
i got x=1 and y=9
?? how to reshape and display the image?
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
sorry, instead of stHeader.BytesPerVoxel == 1 what should i give?
fid = fopen('mars.raw');
rawdata = fread(fid, inf, '*uint8');
fclose(fid);
stHeader.BytesPerVoxel is an undefined function.
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?

Sign in to comment.

Walter Roberson
Walter Roberson on 18 Apr 2013

2 Comments

Thanks for the reply.I am having .raw files of consultative committee for space data standards (CCSDS) .I am not able to open it.
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.

Sign in to comment.

Jan
Jan on 18 Apr 2013
"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

"There is a way to open raw files by assigning binary array and then read in from files." This is the reply given by the the author.How to give this in binary array?
fid = fopen('YourFile.raw');
rawdata = fread(fid, inf, '*uint8');
fclose(fid);
Now what?
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.
how can i see the image imshow() is not working.
i am having images with 8 bits,10,12,16 bits.
@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?
Could you please tell me how many .raw files are there with some references?
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?
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().
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?
clear; clc;
width = 256;
height = 256;
ccnt=width*height;
fileID = fopen('Phase0004.raw','r');
A=fread(fileID,ccnt,'uint8=>uint8');
fclose(fileID);

Sign in to comment.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Tags

Asked:

on 18 Apr 2013

Commented:

on 24 Sep 2019

Community Treasure Hunt

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

Start Hunting!