Unable to Extract Numbers Using OCR Despite Performing Image Processing
I have images from a screen recording from which I am trying extract certain text and numbers.
I then crop the regions of interest (ROI) and proceed to resize the image from each ROI. Finally I apply various processing techniques such as filtering, adaptive histogram equalization, masking, binarization and morphological operations as shown below.
My code is shared as under
function [final_frame,point_cloud_block_values] = block_extractor_v10(img)
%% Define Region of Interest
a0 = 25;
for dd = 1:25
a_n(dd) = a0+45*(dd-1);
end
roi_matrix = [360.*ones(25,1),a_n’,67.*ones(25,1),32.*ones(25,1)];
roi3 = [111.51,1099.51,172.98,43.98]; %Final frame name
point_cloud_block_values = zeros(25,1);
for gg = 1:25
I2 = imcrop(img,roi_matrix(gg,:));
I3 = imresize(I2,[33*63,68*68]);
txt1 = ocr(I3,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)||txt1.TextLineConfidences <0.5
txt1 = ocr(I2,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)||txt1.TextLineConfidences <0.5
I3 = imresize(I2,[33*70,68*80]);
txt1 = ocr(I3,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
end
if isempty(temp)||temp>12000
I3_average1 = rgb2gray(I3);
mask = I3_average1 < 60;
mask = imclose(mask, true(9, 5));
txt1 = ocr(mask,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)
txt1 = ocr(I2,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
end
if isempty(temp)||size(temp,1)>1
I3_average1 = rgb2gray(I3);
I3_filt = medfilt2(I3_average1);
I3_filt = adapthisteq(I3_filt);
se = strel(‘disk’,1);
I3_filt = imopen(I3_filt,se);
I3_filt = imbinarize(I3_filt);
txt1 = ocr(I3_filt,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
point_cloud_block_values(gg) = temp;
end
txt3 = ocr(img,roi3,CharacterSet="_aeFmr0123456789",LayoutAnalysis="line");
if length(point_cloud_block_values) ~=25
error(‘The length of each block must be 25’)
end
final_frame = txt3.Words;
final_frame = char(final_frame);
end
However, I am unable to get 100% accuracy in text detection as there are several instances where the text is incorrectly identified by OCR as shown below.
I have uploaded the entire image database on my my google drive. Please adviseI have images from a screen recording from which I am trying extract certain text and numbers.
I then crop the regions of interest (ROI) and proceed to resize the image from each ROI. Finally I apply various processing techniques such as filtering, adaptive histogram equalization, masking, binarization and morphological operations as shown below.
My code is shared as under
function [final_frame,point_cloud_block_values] = block_extractor_v10(img)
%% Define Region of Interest
a0 = 25;
for dd = 1:25
a_n(dd) = a0+45*(dd-1);
end
roi_matrix = [360.*ones(25,1),a_n’,67.*ones(25,1),32.*ones(25,1)];
roi3 = [111.51,1099.51,172.98,43.98]; %Final frame name
point_cloud_block_values = zeros(25,1);
for gg = 1:25
I2 = imcrop(img,roi_matrix(gg,:));
I3 = imresize(I2,[33*63,68*68]);
txt1 = ocr(I3,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)||txt1.TextLineConfidences <0.5
txt1 = ocr(I2,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)||txt1.TextLineConfidences <0.5
I3 = imresize(I2,[33*70,68*80]);
txt1 = ocr(I3,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
end
if isempty(temp)||temp>12000
I3_average1 = rgb2gray(I3);
mask = I3_average1 < 60;
mask = imclose(mask, true(9, 5));
txt1 = ocr(mask,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)
txt1 = ocr(I2,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
end
if isempty(temp)||size(temp,1)>1
I3_average1 = rgb2gray(I3);
I3_filt = medfilt2(I3_average1);
I3_filt = adapthisteq(I3_filt);
se = strel(‘disk’,1);
I3_filt = imopen(I3_filt,se);
I3_filt = imbinarize(I3_filt);
txt1 = ocr(I3_filt,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
point_cloud_block_values(gg) = temp;
end
txt3 = ocr(img,roi3,CharacterSet="_aeFmr0123456789",LayoutAnalysis="line");
if length(point_cloud_block_values) ~=25
error(‘The length of each block must be 25’)
end
final_frame = txt3.Words;
final_frame = char(final_frame);
end
However, I am unable to get 100% accuracy in text detection as there are several instances where the text is incorrectly identified by OCR as shown below.
I have uploaded the entire image database on my my google drive. Please advise I have images from a screen recording from which I am trying extract certain text and numbers.
I then crop the regions of interest (ROI) and proceed to resize the image from each ROI. Finally I apply various processing techniques such as filtering, adaptive histogram equalization, masking, binarization and morphological operations as shown below.
My code is shared as under
function [final_frame,point_cloud_block_values] = block_extractor_v10(img)
%% Define Region of Interest
a0 = 25;
for dd = 1:25
a_n(dd) = a0+45*(dd-1);
end
roi_matrix = [360.*ones(25,1),a_n’,67.*ones(25,1),32.*ones(25,1)];
roi3 = [111.51,1099.51,172.98,43.98]; %Final frame name
point_cloud_block_values = zeros(25,1);
for gg = 1:25
I2 = imcrop(img,roi_matrix(gg,:));
I3 = imresize(I2,[33*63,68*68]);
txt1 = ocr(I3,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)||txt1.TextLineConfidences <0.5
txt1 = ocr(I2,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)||txt1.TextLineConfidences <0.5
I3 = imresize(I2,[33*70,68*80]);
txt1 = ocr(I3,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
end
if isempty(temp)||temp>12000
I3_average1 = rgb2gray(I3);
mask = I3_average1 < 60;
mask = imclose(mask, true(9, 5));
txt1 = ocr(mask,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
if isempty(temp)
txt1 = ocr(I2,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
end
if isempty(temp)||size(temp,1)>1
I3_average1 = rgb2gray(I3);
I3_filt = medfilt2(I3_average1);
I3_filt = adapthisteq(I3_filt);
se = strel(‘disk’,1);
I3_filt = imopen(I3_filt,se);
I3_filt = imbinarize(I3_filt);
txt1 = ocr(I3_filt,CharacterSet="0123456789",LayoutAnalysis="line");
temp = str2double(txt1.Words);
end
point_cloud_block_values(gg) = temp;
end
txt3 = ocr(img,roi3,CharacterSet="_aeFmr0123456789",LayoutAnalysis="line");
if length(point_cloud_block_values) ~=25
error(‘The length of each block must be 25’)
end
final_frame = txt3.Words;
final_frame = char(final_frame);
end
However, I am unable to get 100% accuracy in text detection as there are several instances where the text is incorrectly identified by OCR as shown below.
I have uploaded the entire image database on my my google drive. Please advise ocr, image processing, textscan MATLAB Answers — New Questions