Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed 8 and B problem and migrate to python3 #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sstefanov
Copy link

No description provided.

@rmast
Copy link

rmast commented Jan 27, 2024

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

output

Your version gives this picture:
output (1)

You can see the big text on the top now gets the wrong inversions.

@rmast
Copy link

rmast commented Jan 27, 2024

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");

Kasar2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants