How can I read a RAW image in MATLAB?

I want to open and read an image .raw

in MATLAB. My file can be downloaded here . I have tried the following three code snippets but it does not give the expected results.

Snippet # 1

    row=576;  col=768;
    fin=fopen('m-001-1.raw','r');
    I=fread(fin,row*col,'uint8=>uint8'); 
    Z=reshape(I,row,col);
    Z=Z';
    k=imshow(Z);

      

It shows this image:

first

Snippet # 2

    f=fopen('m-001-1.raw');
    a=fread(f);
    input_img = reshape(a,768, 576, 3);
    input_img = imrotate(input_img, -90);
    imwrite(input_img, 'm-001-1.jpg'); 

      

Saves a blank (white only) image in .jpg

.

Snippet # 3

    id = fopen('m-001-1.raw', 'r');
    x = fread(id, [576,768], 'short');

      

When I use imshow(x)

this picture shows:

third

How to read this image correctly?

+3


source to share


2 answers


Peter's code (thanks btw!) Works assuming your image is grayscale. For color images, what you need to do is read in all bytes as one one-dimensional array as we cannot use fread

to read the data as a 3D matrix (at least as far as I know ...). After that, we will reformat it so that it becomes a 3D matrix. After we change the matrix, notice that this is the transposed result. So we'll either have to transpose each channel ourselves, or you can skillfully combine flipdim

and imrotate

to do the 3D transposition for us. I would rotate the image so that it is 90 degrees clockwise, but that would make the mirror of the image reflected in columns. Then I useflipdim

with the second parameter as 2

, as I want to flip the reflection across the columns to get the original image back.

So what you really need to do is:

row=576;  col=768;
fin=fopen('m-001-1.raw','r');
I=fread(fin, col*row*3,'uint8=>uint8'); %// Read in as a single byte stream
I = reshape(I, [col row 3]); %// Reshape so that it a 3D matrix - Note that this is column major
Ifinal = flipdim(imrotate(I, -90),2); % // The clever transpose
imshow(Ifinal);
fclose(fin); %// Close the file

      

I am getting this image:

enter image description here




Alternatively, you can use Peter's code, but you would use it so that you can recreate the image on the same color plane at the same time. In other words, you can do this:

row=576;  col=768;
fin=fopen('m-001-1.raw','r');
I1=fread(fin, [col row],'uint8=>uint8'); %// Red channel
I2=fread(fin, [col row],'uint8=>uint8'); %// Green channel
I3=fread(fin, [col row],'uint8=>uint8'); %// Blue channel
I1 = I1.'; I2 = I2.'; I3 = I3.'; %// Transpose each channel separately
Ifinal = cat(3, I1, I2, I3); %// Create 3D matrix
imshow(Ifinal);
fclose(fin);

      


You will get the same image as above.

+4


source


Your row / column numbers are reversed. Since MATLAB arrays are large in column size, and bitmaps are usually stored in rows, you need to read the image as a [col row] matrix and then transfer it.

row=576;  col=768;
fin=fopen('m-001-1.raw','r');
I=fread(fin, [col row],'uint8=>uint8'); 
Z=I';
k=imshow(Z)

      



Image replication happens because you have a short 768-576 = 192 pixels per row, so each row is gradually cut off from that amount. After 4 lines, you have determined the difference (4 * 192 = 768), so you have a replication with 4 images.

+6


source







All Articles