ocr-text-extraction
ocr-text-extraction copied to clipboard
fixed 8 and B problem and migrate to python3
I watched your PR. The original routine, which was not fully python3 did this conversion from this source picture:
https://user-images.githubusercontent.com/3341558/175789293-f39ddfdb-6f3e-4598-8d16-80a1f4a88b36.jpg
Your version gives this picture:
You can see the big text on the top now gets the wrong inversions.
When I run the original matlab code in Octave I get a better result, however missing most of the dots of the characters, so this jasonlfunk-translation does not fully represent the original matlab-code:
function[out_bw] = kasar_binarize(im)
%--------------------------------------------------------------------------
% COLOR TEXT BINARIZATION
% Author: Thotreingam Kasar
% MILE, Department of Electrical Enginerring,
% Indian Institute of Science, Bangalore, INDIA.
% Last modified on: 08/11/2008
%--------------------------------------------------------------------------
% Ref: T Kasar, J Kumar and A G Ramakrishnan, "Font and background color
% independent text binarization", Proc. 2nd Intl. Workshop on Camera-Based
% Document Analysis and Recognition, pp. 3-9, 2007.
%--------------------------------------------------------------------------
pkg load image
im = imread('175789293-f39ddfdb-6f3e-4598-8d16-80a1f4a88b36.jpg');
%figure,imshow(im);
img = rgb2gray(im);
[m,n,o] = size(im);
% Color Edge Detection
er = edge(im(:,:,1),'canny',[0.2 0.3],1);
eg = edge(im(:,:,2),'canny',[0.2 0.3],1);
eb = edge(im(:,:,3),'canny',[0.2 0.3],1);
e = er|eg|eb;
% Connected Component Analysis
[L,num]= bwlabel(e,8);
stats = regionprops(L,'BoundingBox','Area');
bbox = cat(1, stats.BoundingBox);
barea = cat(1,stats.Area);
aspect = bbox(:,4)./bbox(:,3);
% Heuristic Filtering
valid = find(not(xor(bbox(:,4)<0.8*m,bbox(:,3)<0.8*n))&(barea>15)&...
(aspect>0.1 & aspect<10)&(bbox(:,3).*bbox(:,4)<...
m*n/5*ones(size(bbox,1),1)));
% Edge-Box Filtering starts------------------------------------------------
vbox = bbox(valid,:);
remov_ind = [];
for i = 1:size(vbox,1)
ref = vbox(i,:);
X = vbox(:,1);
Y = vbox(:,2);
W = vbox(:,3);
H = vbox(:,4);
subi = find((X>=ref(1)& X + W<ref(1)+ref(3))&...
(X>ref(1)& X + W<=ref(1)+ref(3))&...
(Y>=ref(2)& Y + H<ref(2)+ref(4))&...
(Y>ref(2)& Y + H<=ref(2)+ref(4)));
ff = isempty(subi);
if ff ~=1
if(length(subi)<3)
remov_ind = [remov_ind ;subi];
else
remov_ind = [remov_ind ;i];
end
end
end
vbox(remov_ind,:) = [];
%Edge-Box Filtering ends---------------------------------------------------
%figure,imshow(e);%Display the final set of CC considered for binarization
% hold on;
vbox = round(vbox);
out_bw = ones(size(img));
offset = 1; % parameter for selecting the background pixels
for i =1:size(vbox,1)
bx(:,:,i) = [vbox(i,1) vbox(i,2)
vbox(i,1) vbox(i,2) + vbox(i,4)
vbox(i,1) + vbox(i,3) vbox(i,2) + vbox(i,4)
vbox(i,1) + vbox(i,3) vbox(i,2)
vbox(i,1) vbox(i,2)]';
% line_handle = line(bx(1,:,i),bx(2,:,i));
% set(line_handle,'Color',[1 1 0],'linewidth',2);
% Estimate the foreground and background intensities for each CC
block = img(vbox(i,2):vbox(i,2) + vbox(i,4)-1,...
vbox(i,1):vbox(i,1) + vbox(i,3)-1);
blocke = e(vbox(i,2):vbox(i,2) + vbox(i,4)-1,...
vbox(i,1):vbox(i,1) + vbox(i,3)-1);
FG = block(blocke==1);
foregr = mean(FG); % foreground intensity
bg = [];
if (vbox(i,2)-offset>1)&(vbox(i,1)-offset>1)
bg = [bg img(vbox(i,2)-offset,vbox(i,1)-offset) img(vbox(i,2)-offset,vbox(i,1)) img(vbox(i,2),vbox(i,1)-offset) ];
end
if(vbox(i,2) + vbox(i,4) + offset<m)&(vbox(i,1)-offset>1 )
bg = [bg img(vbox(i,2) + vbox(i,4) + offset ,vbox(i,1) - offset) img(vbox(i,2) + vbox(i,4),vbox(i,1)-offset) img(vbox(i,2) + vbox(i,4)+offset,vbox(i,1))];
end
if (vbox(i,2) + vbox(i,4) + offset<m)&(vbox(i,1)+vbox(i,3)+offset<n)
bg = [bg img(vbox(i,2) + vbox(i,4) + offset ,vbox(i,1)+vbox(i,3)+offset) img(vbox(i,2) + vbox(i,4),vbox(i,1)+vbox(i,3)+offset) img(vbox(i,2) + vbox(i,4) + offset ,vbox(i,1)+vbox(i,3))];
end
if (vbox(i,2)-offset>1 )&(vbox(i,1)+vbox(i,3)+offset<n)
bg = [bg img(vbox(i,2)-offset,vbox(i,1)+vbox(i,3)+offset) img(vbox(i,2)-offset,vbox(i,1)+vbox(i,3)) img(vbox(i,2),vbox(i,1)+vbox(i,3)+offset)];
end
backgr = median(double(bg)); % Background intensity
bin_im = double(block)>(foregr);
if foregr>=backgr
out_bw(vbox(i,2):vbox(i,2) + vbox(i,4)-1,vbox(i,1):vbox(i,1) + vbox(i,3)-1) = not(bin_im);
elseif foregr<backgr
out_bw(vbox(i,2):vbox(i,2) + vbox(i,4)-1,vbox(i,1):vbox(i,1) + vbox(i,3)-1) = bin_im;
end
end
imwrite(out_bw,"Kasar2.png");