OCR on binary image
8 ビュー (過去 30 日間)
古いコメントを表示
Hello, I have an image and I've pre-processed it to be as clear as possible to get a binary image - for OCR.
However, the OCR function does not recognize any character - how can it be improved for my case?
This is what I am doing:
img_for_ocr = imread("img.png");
ocrResults = ocr(img_for_ocr(:,:,1));
figure, imshow(img_for_ocr(:,:,1))
fprintf("Recognized text: %s \t Detection confidence: %0.4f \n",cell2mat(ocrResults.Words),ocrResults.WordConfidences)
0 件のコメント
回答 (1 件)
Image Analyst
2023 年 10 月 6 日
Here's an improvement, though I don't have time to finish it now.
% 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 = 16;
markerSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = [];
baseFileName = 'ocr1.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% 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.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update 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)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get mask by thresholding at 128.
lowThreshold = 128;
highThreshold = 255;
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage)
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
caption = sprintf('Histogram of Image. Threshold at %d', lowThreshold);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Display initial mask.
subplot(2, 2, 2);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------------------
% Clean up the initial mask -- get rid of small interior holes in the image.
holeMask = imclearborder(~mask);
%--------------------------------------------------------------------------------------------------------
% Optional: find areas of blobs so we can filter out small ones.
props = regionprops(holeMask, 'Area');
allAreas = sort([props.Area])
% Get mask of just the small holes.
holeMask = bwareafilt(holeMask, [1, 600]);
%--------------------------------------------------------------------------------------------------------
% Display hole mask.
subplot(2, 2, 3);
imshow(holeMask);
impixelinfo;
axis('on', 'image');
title('Hole Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Fill in those holes in the original mask
mask(holeMask) = true;
% Get rid of blobs smaller than a letter.
% props = regionprops(mask, 'Area');
% allAreas = sort([props.Area])
mask = bwareaopen(mask, 1000);
%--------------------------------------------------------------------------------------------------------
% Display the final mask
subplot(2, 2, 4);
imshow(mask);
impixelinfo;
axis('on', 'image');
title('Final, Filled Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
txt = ocr(mask)
fprintf('The text is %s.\n', txt.Words{end})
3 件のコメント
Image Analyst
2023 年 10 月 6 日
Yeah, we know the OCR algorithm they use is not the best. A few years ago they put it on the list of functions that needed another looking at but I guess tehy haven't gotten around to it yet. Maybe you can try deep learning. Look for their MNIST demo where they're using a deep learning model to predict numbers and (possibly) alphabetic characters.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
