How to read a raw format image data into a matrix??

조회 수: 1 (최근 30일)
Huan-Sheng Wang
Huan-Sheng Wang 2015년 12월 2일
댓글: Huan-Sheng Wang 2015년 12월 2일
I have the following old program for reading image into a matrix. But it is not working with new version Matlab.
How to modify this code?? Thanks!
x=zeros(512,512);
fid=fopen('d:\research\usc512\lenna','rb');
if(fid==-1)
error('Error!');
end
for i=1:512
x(i,:)=fscanf(fid,'%c',512);
end
fclose(fid);

답변 (1개)

Walter Roberson
Walter Roberson 2015년 12월 2일
That code is valid, if a bit wasteful. It does, though, look for a file d:\research\usc512\lenna that has no file extension, which is unusual in MS Windows. Windows Explorer by default hides file extensions but you can ask to reveal them. I suspect that you will find that the file does have an extension; if so then it needs to be specified in the fopen()
[filename, filepath] = uigetfile('d:\research\usc512', 'Select a file');
fullname = fullfile(filepath, filename);
fid = fopen(fullname, 'rb');
x = fread(fid, [512, 512], 'uint8=>double').' ; %careful, it goes down columns first
fclose(fid);
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 12월 2일
편집: Walter Roberson 2015년 12월 2일
I read in lena.jpg and wrote out lenna (no file extension) from it using fwrite(). I then used the code you indicate to read it back in, changing only the path to the file. It works for me and produces an input identical to the original.
Which MATLAB version are you using, and which version of MS Windows?
Did you experiment with the code I posted?
Huan-Sheng Wang
Huan-Sheng Wang 2015년 12월 2일
The MATLAB version is R1024a. The MS Windows I am using is WIN7 64 bits. If I run the old code under R0124a, I got the message "Subscripted assignment dimension mismatch." when i=380. I will try your code later! Thank you very much!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by