Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

How can I read in a RAW image in MATLAB?

I want to open and read a .raw image in MATLAB. My file can be downloaded here. I have tried the following three code snippets, but neither gives the expected results.

Code 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 picture:

first

Code 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'); 

This saves a blank (just white) image in .jpg format.

Code 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 do I read this picture correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your row/col sizes are reversed. Since MATLAB arrays are column-major, and raster images are usually stored row-major, you need to read the image as a [col row] matrix, then transpose it.

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

The image replication is happening because you're short 768-576 = 192 pixels per line, so each line is progressively off that amount. After 4 lines, you've made up the difference (4*192 = 768), so you have a 4-image replication.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...