필터 지우기
필터 지우기

fopen gives -1. How to open??

조회 수: 28 (최근 30일)
Brenden
Brenden 2012년 6월 28일
My current work involves image processing. Just recently I have been looking at archived files from the early 2000's. The problem is that these images are stored as *.FILT, *.filt or *.im.... To my knowledge these filetypes where created by a past colleague inorder to save the image data with a text header (information about the image). My problem now is that these images cannot be opened as the software is far out of date.
I have written a small code to convert these files into *.bmp as follows:
clear all
close all
% Prompt for image file
[image_file image_file_path image_file_filterindex] = uigetfile({'*.FILT;*.filt;*.im;*'}, 'Select image for conversion to *.bmp','MultiSelect','On');
if iscell(image_file) ==1
for n=1:length(image_file)
[pathstr, name, ext] = fileparts(image_file{n});
file_id = fopen(image_file{n})
if file_id ~= -1
header = fread(file_id,8192,'char');
image = fread(file_id,[1024,1024],'uint16',0,'b');
% image = fread(file_id,[1024,1024],'int16',0,'n');
image = image./max(image(:));
fclose('all');
imwrite(image, [image_file_path name '_2_uint16_b' '.bmp'], 'bmp');
end
end
elseif iscell(image_file) ==0
[pathstr, name, ext] = fileparts(image_file);
file_id = fopen(image_file)
if file_id ~= -1
header = fread(file_id,8192,'char');
image = fread(file_id,[1024,1024],'uint16',0,'b');
% image = fread(file_id,[1024,1024],'int16',0,'n');
image = image./max(image(:));
fclose('all');
imwrite(image, [image_file_path name '_2_uint16_b' '.bmp'],'bmp');
end
end
My problem is that fopen gives -1 most of the time and so I cannot even open the file. However I have just recently found by running the program a few times on the same file that sometimes fopen gives -1 and sometimes it gives a valid id and the conversion goes through... Is there something wrong with fopen?? Any ideas??

채택된 답변

Jan
Jan 2012년 6월 28일
fopen replies -1 when it does not find the file. You can test this:
file_id = fopen(image_file{n});
if file_id == -1
if exist(image_file{n}, 'file')
error('Cannot open existing file: %s', image_file{n});
else
error('Missing file: %s', image_file{n});
end
end
Unfortunately this does not give the correct warning, the you try to open a folder, because exist(S, 'file') checks for folders also. Anyhow, it should help to find the problem.
  댓글 수: 4
Brenden
Brenden 2012년 6월 28일
Thank you Jan. You where right it was not reading the file path. I changed the code as follows:
file_id = fopen([image_file_path '/' image_file{n}]);
Brenden
Brenden 2012년 6월 28일
I have one more question. In the above code I read the data with
image = fread(file_id,[1024,1024],'uint16',0,'b');
where i specify the size of the image as [1024,1024]. Is there anyway to have matlab calculate what size the image is from the file?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by