Extract Digital Numbers from Image
182 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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');
0 个评论
更多回答(2 个)
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.
0 个评论
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 个评论
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 Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!