% author: M.M.Haji % email : mehdi.haji@gmail.com % function y = remove_nontext1(x_in) % remove non-text components from the image x, using wavelet transform % see "Fast Text Segmentation Using Wavelet for Document Processing" by % S. Deng and S. Latif for more details. % % example: % % x = imread('image031.jpg'); % imshow(remove_nontext1(x)) if ndims(x_in) == 3 x_in = rgb2gray(x_in); end [nr nc] = size(x_in); [C,S] = wavedec2(x_in,2,'haar'); % cA2 = appcoef2(C,S,'haar',2); % cH2 = detcoef2('h',C,S,2); % cV2 = detcoef2('v',C,S,2); cD2 = detcoef2('d',C,S,2); fun = inline('sum(abs(x(:)))'); E1 = blkproc(cD2,[3 3],fun); [IDX C]= kmeans([E1(:)],2,'rep',4,'EmptyAction','drop','Display','off'); if( norm(C(1,:)) > norm(C(2,:)) ) IDX(find(IDX==1)) = 1; IDX(find(IDX==2)) = 0; else IDX(find(IDX==1)) = 0; IDX(find(IDX==2)) = 1; end IDX = logical(reshape(IDX,size(E1))); imwrite(IDX,'tmp.bmp'); % post processing: dos('rbsmooth tmp.bmp tmp.bmp 1 10'); L = imread('tmp.bmp'); L = imclose(L,strel('rect',[1 3])); L = imopen(L,strel('rect',[3 1])); L = imopen(L,strel('rect',[1 3])); L = imdilate(L,strel('rect',[3 3])); L = imdilate(L,strel('rect',[1 3])); % [L2 NUM] = bwlabel(L,8); % B = imfeature(L2,'BoundingBox'); % for i1 = 1:NUM % tmp = B(i1).BoundingBox; % sx = ceil(tmp(1)); % sy = ceil(tmp(2)); % w = floor(tmp(3)); % h = floor(tmp(4)); % % L(sy:sy+h,sx:sx+w) = 1; % end y = imresize(L,12); y = y(1:nr,1:nc);