查看: 258|回复: 8|关注: 0

[已答复] 如何提取图片中的文字保存成文本

[复制链接]

新手

5 麦片

财富积分


050


11

主题

22

帖子

0

最佳答案
发表于 2020-8-10 09:57:49 | 显示全部楼层 |阅读模式
如何提取图片中的文字保存成文本 ,输入一张图片如何把其中的文字提取出来,然后存到文档当中呢?

回复主题 已获打赏: 0 积分

举报

新手

10 麦片

财富积分


050


0

主题

7

帖子

1

最佳答案
发表于 2020-8-11 16:00:19 | 显示全部楼层
回复此楼 已获打赏: 0 积分

举报

新手

5 麦片

财富积分


050


11

主题

22

帖子

0

最佳答案
 楼主| 发表于 2020-8-11 16:16:32 | 显示全部楼层
本帖最后由 WHXCURRY 于 2020-8-11 16:26 编辑

您好,我们这个图中有许多并排文本框,能不能把每一个文本框都先截取出来,然后在进行识别呢?
回复此楼 已获打赏: 0 积分

举报

新手

10 麦片

财富积分


050


0

主题

7

帖子

1

最佳答案
发表于 2020-8-11 19:00:07 | 显示全部楼层
WHXCURRY 发表于 2020-8-11 16:16
您好,我们这个图中有许多并排文本框,能不能把每一个文本框都先截取出来,然后在进行识别呢? ...

这个例子里面识别电话按键的部分应该是你想要的效果?
https://ww2.mathworks.cn/help/vi ... ecognition-ocr.html
回复此楼 已获打赏: 0 积分

举报

新手

5 麦片

财富积分


050


11

主题

22

帖子

0

最佳答案
 楼主| 发表于 2020-8-12 09:44:00 | 显示全部楼层
环宇huanyu 发表于 2020-8-11 19:00
这个例子里面识别电话按键的部分应该是你想要的效果?
https://ww2.mathworks.cn/help/vision/examples/re ...

我的图片太大,该怎么缩小呢?谢谢啦,很感谢
回复此楼 已获打赏: 0 积分

举报

新手

10 麦片

财富积分


050


0

主题

7

帖子

1

最佳答案
发表于 2020-8-12 09:47:59 | 显示全部楼层
WHXCURRY 发表于 2020-8-12 09:44
我的图片太大,该怎么缩小呢?谢谢啦,很感谢

imresize, 你应该多去看看image processing 和 computer vision的帮助文档
回复此楼 已获打赏: 0 积分

举报

新手

5 麦片

财富积分


050


11

主题

22

帖子

0

最佳答案
 楼主| 发表于 2020-8-12 09:55:12 | 显示全部楼层
环宇huanyu 发表于 2020-8-12 09:47
imresize, 你应该多去看看image processing 和 computer vision的帮助文档

刚开始学,还不是很懂,谢谢啦
回复此楼 已获打赏: 0 积分

举报

新手

5 麦片

财富积分


050


11

主题

22

帖子

0

最佳答案
 楼主| 发表于 2020-8-13 10:21:49 | 显示全部楼层
环宇huanyu 发表于 2020-8-12 09:47
imresize, 你应该多去看看image processing 和 computer vision的帮助文档

这两个例子我试过了,好像提取不能行,你看看还有什么问题呢?I = imread('555.jpg');
I = rgb2gray(I);

figure;
imshow(I)
results = ocr(I);

results.Text;
% Set 'TextLayout' to 'Block' to instruct ocr to assume the image
% contains just one block of text.
results = ocr(I,'TextLayout','Block');

results.Text;
BW = imbinarize(I);

figure;
imshowpair(I,BW,'montage');
% Remove keypad background.
Icorrected = imtophat(I,strel('disk',15));

BW1 = imbinarize(Icorrected);

figure;
imshowpair(Icorrected,BW1,'montage');
% Perform morphological reconstruction and show binarized image.
marker = imerode(Icorrected, strel('line',10,0));
Iclean = imreconstruct(marker, Icorrected);

BW2 = imbinarize(Iclean);

figure;
imshowpair(Iclean,BW2,'montage');
results = ocr(BW2,'TextLayout','Block');

results.Text;
% The regular expression, '\d', matches the location of any digit in the
% recognized text and ignores all non-digit characters.
regularExpr = '\d';

% Get bounding boxes around text that matches the regular expression
bboxes = locateText(results,regularExpr,'UseRegexp',true);

digits = regexp(results.Text,regularExpr,'match');

% draw boxes around the digits
Idigits = insertObjectAnnotation(I,'rectangle',bboxes,digits);

figure;
imshow(Idigits);
% Use the 'CharacterSet' parameter to constrain OCR
results = ocr(BW2, 'CharacterSet','0123456789','TextLayout','Block');

results.Text;
% Sort the character confidences.
[sortedConf, sortedIndex] = sort(results.CharacterConfidences, 'descend');

% Keep indices associated with non-NaN confidences values.
indexesNaNsRemoved = sortedIndex( ~isnan(sortedConf) );

% Get the top ten indexes.
topTenIndexes = indexesNaNsRemoved(1:10);

% Select the top ten results.
digits = num2cell(results.Text(topTenIndexes));
bboxes = results.CharacterBoundingBoxes(topTenIndexes, :);

Idigits = insertObjectAnnotation(I,'rectangle',bboxes,digits);

figure;
imshow(Idigits);
% Initialize the blob analysis System object(TM).
blobAnalyzer = vision.BlobAnalysis('MaximumCount',500);

% Run the blob analyzer to find connected components and their statistics.
[area,centroids,roi] = step(blobAnalyzer,BW1);

% Show all the connected regions.
img = insertShape(I,'rectangle',roi);
figure;
imshow(img);
areaConstraint = area > 300;

% Keep regions that meet the area constraint.
roi = double(roi(areaConstraint, :));

% Show remaining blobs after applying the area constraint.
img = insertShape(I,'rectangle',roi);
figure;
imshow(img);
% Compute the aspect ratio.
width  = roi(:,3);
height = roi(:,4);
aspectRatio = width ./ height;

% An aspect ratio between 0.25 and 1 is typical for individual characters
% as they are usually not very short and wide or very tall and skinny.
roi = roi( aspectRatio > 0.25 & aspectRatio < 1 ,:);

% Show regions after applying the area and aspect ratio constraints.
img = insertShape(I,'rectangle',roi);
figure;
imshow(img);
roi(:,1:2) = roi(:,1:2) - 4;
roi(:,3:4) = roi(:,3:4) + 8;
results = ocr(BW1, roi,'TextLayout','Block');
text = deblank( {results.Text} );
img  = insertObjectAnnotation(I,'rectangle',roi,text);
text = deblank( {results.Text} );
img  = insertObjectAnnotation(I,'rectangle',roi,text);

figure;
imshow(img)
这是那个识别键盘的例子

回复此楼 已获打赏: 0 积分

举报

新手

5 麦片

财富积分


050


11

主题

22

帖子

0

最佳答案
 楼主| 发表于 2020-8-13 10:24:31 | 显示全部楼层
WHXCURRY 发表于 2020-8-11 16:16
您好,我们这个图中有许多并排文本框,能不能把每一个文本框都先截取出来,然后在进行识别呢? ...

好像识别不出来colorImage = imread('0.jpg');
I = rgb2gray(colorImage);

% Detect MSER regions.
[mserRegions, mserConnComp] = detectMSERFeatures(I, ...
    'RegionAreaRange',[200 8000],'ThresholdDelta',4);

figure
imshow(I)
hold on
plot(mserRegions, 'showPixelList', true,'showEllipses',false)
title('MSER regions')
hold off
% Use regionprops to measure MSER properties
mserStats = regionprops(mserConnComp, 'BoundingBox', 'Eccentricity', ...
    'Solidity', 'Extent', 'Euler', 'Image');

% Compute the aspect ratio using bounding box data.
bbox = vertcat(mserStats.BoundingBox);
w = bbox(:,3);
h = bbox(:,4);
aspectRatio = w./h;

% Threshold the data to determine which regions to remove. These thresholds
% may need to be tuned for other images.
filterIdx = aspectRatio' > 3;
filterIdx = filterIdx | [mserStats.Eccentricity] > .995 ;
filterIdx = filterIdx | [mserStats.Solidity] < .3;
filterIdx = filterIdx | [mserStats.Extent] < 0.2 | [mserStats.Extent] > 0.9;
filterIdx = filterIdx | [mserStats.EulerNumber] < -4;

% Remove regions
mserStats(filterIdx) = [];
mserRegions(filterIdx) = [];

% Show remaining regions
figure
imshow(I)
hold on
plot(mserRegions, 'showPixelList', true,'showEllipses',false)
title('After Removing Non-Text Regions Based On Geometric Properties')
hold off
% Get a binary image of the a region, and pad it to avoid boundary effects
% during the stroke width computation.
regionImage = mserStats(6).Image;
regionImage = padarray(regionImage, [1 1]);

% Compute the stroke width image.
distanceImage = bwdist(~regionImage);
skeletonImage = bwmorph(regionImage, 'thin', inf);

strokeWidthImage = distanceImage;
strokeWidthImage(~skeletonImage) = 0;

% Show the region image alongside the stroke width image.
figure
subplot(1,2,1)
imagesc(regionImage)
title('Region Image')

subplot(1,2,2)
imagesc(strokeWidthImage)
title('Stroke Width Image')
% Compute the stroke width variation metric
strokeWidthValues = distanceImage(skeletonImage);   
strokeWidthMetric = std(strokeWidthValues)/mean(strokeWidthValues);
% Threshold the stroke width variation metric
strokeWidthThreshold = 0.4;
strokeWidthFilterIdx = strokeWidthMetric > strokeWidthThreshold;
% Process the remaining regions
for j = 1:numel(mserStats)
   
    regionImage = mserStats(j).Image;
    regionImage = padarray(regionImage, [1 1], 0);
   
    distanceImage = bwdist(~regionImage);
    skeletonImage = bwmorph(regionImage, 'thin', inf);
   
    strokeWidthValues = distanceImage(skeletonImage);
   
    strokeWidthMetric = std(strokeWidthValues)/mean(strokeWidthValues);
   
    strokeWidthFilterIdx(j) = strokeWidthMetric > strokeWidthThreshold;
   
end

% Remove regions based on the stroke width variation
mserRegions(strokeWidthFilterIdx) = [];
mserStats(strokeWidthFilterIdx) = [];

% Show remaining regions
figure
imshow(I)
hold on
plot(mserRegions, 'showPixelList', true,'showEllipses',false)
title('After Removing Non-Text Regions Based On Stroke Width Variation')
hold off
% Get bounding boxes for all the regions
bboxes = vertcat(mserStats.BoundingBox);

% Convert from the [x y width height] bounding box format to the [xmin ymin
% xmax ymax] format for convenience.
xmin = bboxes(:,1);
ymin = bboxes(:,2);
xmax = xmin + bboxes(:,3) - 1;
ymax = ymin + bboxes(:,4) - 1;

% Expand the bounding boxes by a small amount.
expansionAmount = 0.02;
xmin = (1-expansionAmount) * xmin;
ymin = (1-expansionAmount) * ymin;
xmax = (1+expansionAmount) * xmax;
ymax = (1+expansionAmount) * ymax;

% Clip the bounding boxes to be within the image bounds
xmin = max(xmin, 1);
ymin = max(ymin, 1);
xmax = min(xmax, size(I,2));
ymax = min(ymax, size(I,1));

% Show the expanded bounding boxes
expandedBBoxes = [xmin ymin xmax-xmin+1 ymax-ymin+1];
IExpandedBBoxes = insertShape(colorImage,'Rectangle',expandedBBoxes,'LineWidth',3);

figure
imshow(IExpandedBBoxes)
title('Expanded Bounding Boxes Text')
% Compute the overlap ratio
overlapRatio = bboxOverlapRatio(expandedBBoxes, expandedBBoxes);

% Set the overlap ratio between a bounding box and itself to zero to
% simplify the graph representation.
n = size(overlapRatio,1);
overlapRatio(1:n+1:n^2) = 0;

% Create the graph
g = graph(overlapRatio);

% Find the connected text regions within the graph
componentIndices = conncomp(g);
% Merge the boxes based on the minimum and maximum dimensions.
xmin = accumarray(componentIndices', xmin, [], @min);
ymin = accumarray(componentIndices', ymin, [], @min);
xmax = accumarray(componentIndices', xmax, [], @max);
ymax = accumarray(componentIndices', ymax, [], @max);

% Compose the merged bounding boxes using the [x y width height] format.
textBBoxes = [xmin ymin xmax-xmin+1 ymax-ymin+1];
% Remove bounding boxes that only contain one text region
numRegionsInGroup = histcounts(componentIndices);
textBBoxes(numRegionsInGroup == 1, :) = [];

% Show the final text detection result.
ITextRegion = insertShape(colorImage, 'Rectangle', textBBoxes,'LineWidth',3);

figure
imshow(ITextRegion)
title('Detected Text')
ocrtxt = ocr(I, textBBoxes);
[ocrtxt.Text]
能看看哪里有问题吗?
>> Untitled7

ans =

  空的 0×0 char 数组

>> 这是得到的结果为空数组
回复此楼 已获打赏: 0 积分

举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

快速回复 返回顶部 返回列表