ocr not recognizing some numbers

23 views (last 30 days)
Trying to use function ocr to extract some numbers from the attached image.
Expected to have 9 numbers and the results have 8 numbers only.
On the other hand, some of the numbers are not correctly detected.
Any recommendation or suggestion to improve the detection? Or any additional pre-processing on the image needs to be done before using function ocr?
I = imread('Sample.png');
imshow(I,[]);
txt = ocr(I, 'CharacterSet', '0123456789.');
data = str2double(txt.Words);
data(~isnan(data))
ans = 8×1
971.5000 459.2000 7.0000 174.0000 416.0000 174.0000 417.0000 416.0000

Accepted Answer

Walter Roberson
Walter Roberson on 30 Apr 2023
You can use trainOCR to train on sample images, and pass the trained model to ocr
You might want to use ocrTrainingData to help create the data to train on.
Your current model is failing completely on 8's, and is weak on 7's.
  2 Comments
Image Analyst
Image Analyst on 30 Apr 2023
Cool. THat might help. I didn't know about that one. It was introduced just last month in R2023a.
Simon Chan
Simon Chan on 1 May 2023
Great, will definitely train a new model.

Sign in to comment.

More Answers (2)

albara
albara on 30 Apr 2023
Edited: albara on 30 Apr 2023
There are several possible ways to improve the detection, which may include image pre-processing techniques, adjusting OCR parameters, or using other image processing tools.
Here are some recommendations:
  1. Convert to grayscale and apply a threshold: If the image is in color, you might want to convert it to grayscale to simplify the analysis. You can also apply a binary threshold to make the text stand out more clearly.
I_gray = rgb2gray(I);
I_binary = imbinarize(I_gray);
imshow(I_binary);
2- Noise reduction: Removing noise from the image can help to improve OCR accuracy. You can use the 'medfilt2' function to apply a median filter for noise reduction.
I_denoised = medfilt2(I_binary);
imshow(I_denoised);
3- Morphological operations: You can use morphological operations like dilation or erosion to improve the text's visibility.
se = strel('square', 2); % Structuring element
I_dilated = imdilate(I_denoised, se);
imshow(I_dilated);
4-Adjust OCR parameters: You can experiment with the 'TextLayout' and 'AnalysisLevels' parameters to improve the OCR results.
txt = ocr(I_dilated, 'CharacterSet', '0123456789.', 'TextLayout', 'Block', 'AnalysisLevels', 2);
5- If the numbers are expected to be arranged in a specific pattern or grid, you can use image segmentation techniques to isolate each number and then apply OCR on individual segments.
After applying the above recommendations, update the code with the processed image and try again. Remember to experiment with different combinations of the above suggestions for optimal results. Keep in mind that OCR accuracy might not be perfect, and some manual validation or correction might still be necessary.
Waiting for your feedback
Important: There may be some mistakes in this answer Experts can tell if there are any mistakes

Image Analyst
Image Analyst on 30 Apr 2023
Edited: Image Analyst on 30 Apr 2023
ocr needs the image of the letters or numbers to be at least 20 pixels high. I don't think yours are.
If your digits are always the same size (image magnification does not change) then you might have a template of numbers and try normalized cross correlation. See attached demo.

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!