gpt4 book ai didi

algorithm - Matlab 中的简单文本阅读器 (OCR)

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:12:41 24 4
gpt4 key购买 nike

我正在尝试编写一个简单的程序来读取灰度 bmp 图像。我有一组模式(除了“I”之外的整个字母表),我想匹配它们。我在 Matlab 中执行此操作时遇到问题。

到目前为止我得到了什么:

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 计算距离?

最佳答案

灵魂

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

我很简单,但有一些缺点,比如

  • 不读“我”
  • 只阅读水平 strip 的文本
  • 应改进空白检测

关于algorithm - Matlab 中的简单文本阅读器 (OCR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5558005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com