How to recognize negative numbers with OCR?

9 ビュー (過去 30 日間)
sasad games
sasad games 2021 年 2 月 27 日
コメント済み: sasad games 2021 年 2 月 27 日
I am using the following code to recognize numbers in images and transform it to values. However, it only recognizes positive values. Can you please help me to modify the code in order to be able to identify positive and negative values? Thanks in advance.
clc
clear all
close all
%%
I=imread('N1.png');
I2=I(:,:,3);
I3=ind2rgb(I2,gray);
I4=imcomplement(I3);
N=Im_text(I4);
function [val] = Im_text(Original)
% Increase image size by 3x
my_image = imresize(Original, 3);
% Localize words
BW = imbinarize(rgb2gray(my_image));
BW1 = imdilate(BW,strel('disk',6));
s = regionprops(BW1,'BoundingBox');
bboxes = vertcat(s(:).BoundingBox);
% Sort boxes by image height
[~,ord] = sort(bboxes(:,2));
bboxes = bboxes(ord,:);
% Pre-process image to make letters thicker
BW = imdilate(BW,strel('disk',1));
% Call OCR and pass in location of words. Also, set TextLayout to 'word'
ocrResults = ocr(BW,bboxes,'CharacterSet','.0123456789','TextLayout','word');
words = {ocrResults(:).Text}';
words = deblank(words);
val=str2double(words);
end

採用された回答

Image Analyst
Image Analyst 2021 年 2 月 27 日
編集済み: Image Analyst 2021 年 2 月 27 日
Can't you simply add a - to the CharacterSet? Then if the first character is a -, it's a negative number.
This works fine:
grayImage = imread('N1.png');
if ndims(grayImage) == 3
grayImage = rgb2gray(grayImage);
end
imshow(grayImage, 'InitialMagnification', 400);
axis('on', 'image');
N = Im_text(grayImage)
caption = sprintf('The number in here is %f', N);
title(caption, 'FontSize', 20);
%==================================================================================
function [val] = Im_text(Original)
% Increase image size by 3x
my_image = imresize(Original, 3);
ocrResults = ocr(my_image, 'CharacterSet', '-.0123456789', 'TextLayout', 'line')
words = {ocrResults(:).Text}';
words = deblank(words);
val = str2double(words);
end
  1 件のコメント
sasad games
sasad games 2021 年 2 月 27 日
Thank you very much

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeComputer Vision Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by