% [Image,Facit]=ReadNumberImage(P,N)
%
% Reads an image named N from directory P
% and returns the image in Image and the 
% numbers written shown on the image in the string Facit.
% If no file name is given a random file from the 
% directory P is returned. If the random-function is to be used
% the directory P must only contain valid images and no other files.
%
% Example: If you want to read file
% '966.437.470.896.483.243.047.617.200..bmp'
%          from directory 'C:\bilder\Bilder'
%          you type
%
% [Image,Facit]=ReadNumberImage('C:\bilder\Bilder','966.437.470.896.483.243.047.617.200..bmp')
%
%          If you just want a random image from the directory
%          'C:\bilder\Bilder' you type
%
% [Image,Facit]=ReadNumberImage('C:\bilder\Bilder')
%
% Your OCR function  should extract the same string as Facit
% from the image.
%
% Written 2004-01-23
% Fredrik Georgsson
%
function [Image,Facit]=ReadNumberImage(P,N)

if nargin==1
    D=dir(P);
    D=D(3:end);
    n=length(D);
    N=D(ceil(rand*n)).name; 
end

Image=imread([P '\' N]);
Facit=RemDots(RemFileType(N));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------------------------------------------
function S=RemDots(s)
S=[];
for i=1:length(s)
    if s(i)=='.'
        s(i)=' ';
    end
    if s(i)~='.'
        S=[S s(i)];
    end
end    
    
%------------------------------------------
function s=RemFileType(s)
for i=length(s):-1:1
    if s(i)=='.'
        s=s(1:i-1);
        return
    end
end

figure(1)
imshow(Image,[])