Extract Digital Numbers from Image

143 views (last 30 days)
Hi All.
I have lots of scale images that I would like to extract the scale reading from. I have looked through many sources such as "ocr" but when applying that it doesn't read the numbers shown. Attached is an image after processing (cropping. brightining..etc). I appreciate if someone can help me find a way to extract the numbers out of the image.

Accepted Answer

Image Analyst
Image Analyst on 13 Jul 2021
I don't know - it just doesn't like the 2 in that font. It thinks it's an 8 for some reason. This is what I got so far. I'm getting a new computer tomorrow so I may not be able to work on it for a day or two.
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
grayImage = imread('Scale Reading.PNG');
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the red channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 1);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(3, 1, 1);
imshow(grayImage, []);
axis('on', 'image');
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig = gcf;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
% Binarize
mask = grayImage < 60;
% Connect nearby blobs with a closing.
mask = imclose(mask, true(9, 5));
% Display the image.
subplot(3, 1, 2);
imshow(mask, []);
axis('on', 'image');
title('Initial Mask', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Get rid of blobs touching border
mask = imclearborder(mask);
% Take the 7 largest blobs.
mask = bwareafilt(mask, 6);
mask = imrotate(mask, 2);
% Smooth out the numbers by blurring and thresholding.
windowSize = 15;
mask = conv2(double(mask), ones(windowSize)/windowSize^2, 'same') > 0.5;
% Display the image.
subplot(3, 1, 3);
imshow(mask, []);
axis('on', 'image');
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
% Do ocr on it
txt = ocr(mask, 'TextLayout', 'line', 'CharacterSet', '0123456789')
% txt = ocr(grayImage, 'TextLayout', 'line', 'CharacterSet', '0123456789.')
number = str2double(txt.Text)/1000
caption = sprintf('Final Mask. %.3f', number);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');

More Answers (2)

Image Analyst
Image Analyst on 3 Aug 2021
I tried it with my new computer and developed a solution for seven segment LED displays that uses masking to examine the 7 locations where a segment would be. From the pattern or lit or non-lit, I can turn that into a number. It works, as in the attached demo.
I got word from a developer on the Computer Vision team that the ocr() function was not trained for seven segment characters. Still it's surprising how bad it is for certain numbers like thinking a 2 was an 8 and the 8 was a 9.

ANKUR KUMAR
ANKUR KUMAR on 13 Jul 2021
Edited: ANKUR KUMAR on 13 Jul 2021
You can simply use ocr command to do that. Refer to this documentation for some example, which would be a good starting point for you to take it forward.
  3 Comments
Abdullah Azzam
Abdullah Azzam on 13 Jul 2021
I have already went through that documentation. But when running it on the image after doing some masking and filteration it doesn't recognize the numbers or get them wrongly. @Image Analyst Yes I have looked through your code brefily yesterday night, and will look more in details. For some reason when I read the orignial image and cropped to the form I have attached it does show the numbers yet it read 0.03. However, when using the attached image (which has already went cropping process) it show same images but read 187.13. I wil take a deeper look today and let you know what I find. Thanks for the code.
ANKUR KUMAR
ANKUR KUMAR on 13 Jul 2021
@Image Analyst OCR behaves wierd. I just tune the masking factor, and getting 137.313.
mask = conv2(double(mask), ones(windowSize)/windowSize^2, 'full') > 0.3;
I replaced 0.5 to 0.3. I played around masking thresholds and all, but not getting the exact value.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!