필터 지우기
필터 지우기

Extract Digital Numbers from Image

조회 수: 178 (최근 30일)
Abdullah Azzam
Abdullah Azzam 2021년 7월 13일
답변: Image Analyst 2021년 8월 3일
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.

채택된 답변

Image Analyst
Image Analyst 2021년 7월 13일
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');

추가 답변 (2개)

Image Analyst
Image Analyst 2021년 8월 3일
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 2021년 7월 13일
편집: ANKUR KUMAR 2021년 7월 13일
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
Abdullah Azzam
Abdullah Azzam 2021년 7월 13일
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 2021년 7월 13일
@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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by