% author: M.M.Haji % email : mehdi.haji@gmail.com % function y = remove_nontext4(x) % x: input image % y: text mask % remove non-text components using decision tree and dct-18 features. % example: % % x = imread('image031.jpg'); % y = remove_nontext4(x); % z = rgb2gray(x); % z(find(y==false)) = 255; % imshow(z); load text_dtree dct18_coefs = [4 5 6 12 13 14 20 21 22 44 45 46 52 53 54 60 61 62]; if ndims(x) == 3 x = rgb2gray(x); end [nr nc] = size(x); NR = ceil(nr/8) * 8; NC = ceil(nc/8) * 8; x = padarray(x,[NR-nr NC-nc],0,'post'); y = logical(zeros(size(x)/8)); for i1 = 1:8:NR for i2 = 1:8:NC J = dct2(x(i1:i1+7, i2:i2+7)); J = J'; J = J(:); tree_output = (treeval(text_dtree,J(dct18_coefs(1:18))')); if tree_output == 1 y(ceil(i1/8),ceil(i2/8)) = 1; else y(ceil(i1/8),ceil(i2/8)) = 0; end end end % apply rule-based smoothing: imwrite(y,'tmp.bmp'); dos('rbsmooth tmp.bmp tmp.bmp 1 10'); dos('rbsmooth tmp.bmp tmp.bmp 3 10'); y = imread('tmp.bmp'); y = imopen(y,strel('rect',[1 3])); imwrite(y,'tmp.bmp'); dos('rbsmooth tmp.bmp tmp.bmp 2 10'); y = imread('tmp.bmp'); y = imclose(y,strel('rect',[1 3])); y = imopen(y,strel('rect',[1 3])); y = imresize(y,8); y = y(1:nr,1:nc);