Info

This question is closed. Reopen it to edit or answer.

I am doing a project on OCR. I am getting an error in this code.Invalid file identifier. Use fopen to generate a valid file identifier. Error in ocr (line 59) fprintf(fid,'Number Plate:-%s\​nDate:-%s\​n',word,da​te);%Write 'word' in text file (up

1 view (last 30 days)
if true
img = imread('image1.jpg');
img = rgb2gray(img);
level = graythresh(img);
imagen = im2bw(img,level);
imagen = ~imagen;
imagen = bwareaopen(imagen,70);
imagen = ~imagen;
%Try with images:heavy_metal.bmp, scanner.bmp
imshow(imagen);title('INPUT IMAGE WITH NOISE')
%*-*-*Filter Image Noise*-*-*-*
if length(size(imagen))==3 %RGB image
imagen=rgb2gray(imagen);
end
imagen = medfilt2(imagen);
[f c]=size(imagen);
imagen (1,1)=255;
imagen (f,1)=255;
imagen (1,c)=255;
imagen (f,c)=255;
%*-*-*END Filter Image Noise*-*-*-*
word=[];%Storage matrix word from image
re=imagen;
fid = fopen('text.txt', 'at');%Opens text.txt as file for write
while 1
[fl re]=lines(re);%Fcn 'lines' separate lines in text
imgn=~fl;
%*-*Uncomment line below to see lines one by one*-*-*-*
% imshow(fl);pause(1)
%*-*--*-*-*-*-*-*-
%*-*-*-*-*-Calculating connected components*-*-*-*-*-
%Code from:
%http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=8031&objectType=FILE
L = bwlabel(imgn);
mx=max(max(L));
BW = edge(double(imgn),'sobel');
[imx,imy]=size(BW);
for n=1:mx
[r,c] = find(L==n);
rc = [r c];
[sx sy]=size(rc);
n1=zeros(imx,imy);
for i=1:sx
x1=rc(i,1);
y1=rc(i,2);
n1(x1,y1)=255;
end
%*-*-*-*-*-END Calculating connected components*-*-*-*-*
n1=~n1;
n1=~clip(n1);
img_r=same_dim(n1);%Transf. to size 42 X 24
%*-*Uncomment line below to see letters one by one*-*-*-*
% imshow(img_r);pause(1)
%*-*-*-*-*-*-*-*
letter=read_letter(img_r);%img to text
word=[word letter];
end
%fprintf(fid,'%s\n',lower(word));%Write 'word' in text file (lower)
fprintf(fid,'Number Plate:-%s\nDate:-%s\n',word,date);%Write 'word' in text file (upper)
word=[];%Clear 'word' variable
%*-*-*When the sentences finish, breaks the loop*-*-*-*
if isempty(re) %See variable 're' in Fcn 'lines'
break
end
%*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
end
fclose(fid);
end
* *ERROR:-
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in ocr (line 59) fprintf(fid,'Number Plate:-%s\nDate:-%s\n',word,date);%Write 'word' in text file (upper)*

Answers (1)

Image Analyst
Image Analyst on 1 Apr 2014
What is the value of fid right after you call fopen()? Did you look at the help for fopen()? Especially the part that says "If fopen cannot open the file, then fileID is -1." The help documentation is a good place to start (way better than the Answers forum as a first step).
  2 Comments
shanu
shanu on 1 Apr 2014
Edited: shanu on 1 Apr 2014
Sir,i have checked documentation but nothing is there related to this error. value of fid=-1 right after when i call fopen().
Image Analyst
Image Analyst on 1 Apr 2014
Exactly. So you can't open the file. Could be already open and locked by some other process. Maybe it doesn't exist, etc.

Community Treasure Hunt

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

Start Hunting!