why do i need to use fopen twice for reading an image????

5 views (last 30 days)
I am trying to open an image using fopen command. I have to use this command twice for getting it worked. Why is it so??? Here is my code
fid = fopen(filename, 'r');
opened = fopen(fid);
figure,imshow(opened);

Answers (2)

Walter Roberson
Walter Roberson on 9 Jan 2014
There is no way that code is going to do anything useful. You have not read any data in from the file.
What format is the data in in the file?
fid = fopen(filename, 'r');
imdata = fread(fid); %assumes binary
fclose(fid);
imdata = reshape(imdata, 512, 768, 3); %assumes data represented a 512 x 768 RGB image
imshow(imdata);
This code makes assumptions that probably do not fit your situation exactly, but it gives an outline of how data would be read from a binary file.

Image Analyst
Image Analyst on 9 Jan 2014
Why not just do
rgbImage = imread(filename);
imshow(rgbImage);
I don't know why you say you HAVE TO use fopen(). Why? Do you have some custom, raw format? If so, you're going to have to write your own reader, which can be as simple as what Walter showed, or can be more complicated if you have some header that you need to extract.

Community Treasure Hunt

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

Start Hunting!