2011-04-05 48 views
3

我正在嘗試編寫一個簡單的程序來讀取灰度級中的bmp圖像。我有一套模式(除'I'以外的全部字母),我想匹配它們。我在Matlab中遇到問題。Matlab中的簡單文本閱讀器(OCR)

我走到這一步:

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']; 

現在我想比較模式與給定的載體載體,但它們具有不同的尺寸。

我該怎麼做?有一些特殊的比較功能嗎?我應該加0到最後,然後用pdist計算距離?

+0

Wha你可能想要做的是使用不同尺度的關聯(尺寸)。這就是存在的最簡單的OCR。看看函數normxcorr2,特別是matlab中的例子。 – ldog 2011-04-05 21:57:39

回答

4

的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'
  • 讀取文本的唯一horizo​​natal條
  • 空白空間檢測應改進