Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

algorithm - Simple text reader (OCR) in Matlab

I am trying to write a simple program to read a bmp image in gray scale. I have a set of patterns (the whole alphabet except for 'I') and I want to match them. I have problems doing this in Matlab.

What I got so far:

clear
clc

%set of patterns
BW1 = imread('alphabet.bmp');
patterns = bwlabel(~BW1);
patternStats = regionprops(patterns,'all');

patternNumber = size(patternStats);
imagePatternArray = cell(patternNumber);

%make cell array of pattern vectors
for i = 1:1:patternNumber
  imageMatrix = patternStats(i).Image;
  imageVector = imageMatrix(:);
  imagePatternArray{i} = imageVector;
end  

%set of chars
BW2 = imread('text.bmp');
text = bwlabel(~BW2);
textStats = regionprops(text,'all');

letterNumber = size(textStats);
imageLetterArray = cell(letterNumber);

%make cell array of text vectors
for i = 1:1:letterNumber
  imageMatrix = textStats(i).Image;
  imageVector = imageMatrix(:);
  imageLetterArray{i} = imageVector;
end

%lookup table
charSet =['A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

Now I would like to compare the pattern vectors with the given vector, but they have different sizes.

How can I do this? Is there some special compare function? Should I add 0s to the end and then calculate the distance with pdist?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The soultion

clear
clc

%set of patterns
BW1 = imread('alphabet.bmp');
patterns = bwlabel(~BW1);
patternStats = regionprops(patterns,'all');

patternNumber = size(patternStats);
imagePatternArray = cell(patternNumber);

%make cell array of pattern Matrices
for i = 1:1:patternNumber
  imageMatrix = patternStats(i).Image;
  imageMatrix = imresize(imageMatrix, [25 20]);
  imagePatternArray{i} = imageMatrix;
end  

%set of chars
BW2 = imread('kol_2.bmp');
BW2Gray = rgb2gray(BW2); %convert text to grayscale bmp - 0 OR 1
text = bwlabel(~BW2Gray);
textStats = regionprops(text,'all'); 

letterNumber = size(textStats);
imageLetterArray = cell(letterNumber);

%make cell array of text Matrices
for i = 1:1:letterNumber
  imageMatrix = textStats(i).Image;
  imageMatrix = imresize(imageMatrix, [25 20]);
  imageLetterArray{i} = imageMatrix;
end

%white spaces
whiteSpacesIndexes = [];

for i = 1:letterNumber - 1
  firstLetterBox = textStats(i).BoundingBox;
  positionFirstVector = [firstLetterBox(1), firstLetterBox(2)];  

  secondLetterBox = textStats(i+1).BoundingBox;
  positionSecondVector = [secondLetterBox(1), secondLetterBox(2)];

  distanceVector = positionSecondVector - positionFirstVector;
  distance = norm(distanceVector)
  % if the distance between is bigger that letter width plus 1/3 of width, it is a whitespace
  bothLettersSize = firstLetterBox(3)  + secondLetterBox(3);
  noSpaceDistance =  bothLettersSize - bothLettersSize * 0.25; % - 25 per cent (heuristic value)

  if (distance > noSpaceDistance) %&& (abs(distanceVector(2)) > 1.0)
      whiteSpacesIndexes = [whiteSpacesIndexes, i + 1];
  end 
end

compareVector = size(patternNumber);
indexArray = size(letterNumber);

for i = 1:1:letterNumber
    for j = 1:1:patternNumber        
           correlationMatrix = normxcorr2(imagePatternArray{j},imageLetterArray{i});           
          compareVector(j) = max(abs(correlationMatrix(:)));
    end    
        [correlationMax,correlationIndex] = max(compareVector);
       indexArray(i) = correlationIndex;
end

%lookup table
charSet = ['A','B','C','D','E','F','G','H','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];

%outPut stream
outPut = size(letterNumber);
for i = 1:1:letterNumber
   outPut(i) = charSet(indexArray(i));
end

whiteSpaceNumber = size(whiteSpacesIndexes,2);

whiteSpacesIndexes = whiteSpacesIndexes + (0:numel(whiteSpacesIndexes)-1)
nFinal = numel(outPut)+numel(whiteSpacesIndexes );      %# New length of result with blanks
newstr = blanks(nFinal);                                %# Initialize the result as blanks
newstr(setdiff(1:nFinal,whiteSpacesIndexes )) = outPut

I fairly simple and have some drawback like

  • does not read 'I'
  • reads only horizonatal strip of text
  • white space detecting should be improved

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...