% characterise - Function to decompose text picture into characters & intensity % in a data structure % - Part of setUpCmatscii.m % Usage: % [data,intensity,textWdHgt]=characterise(textpic,estimation) % % Arguments: % textpic - image containing chinese characters without white border % estimation - data structure containing % estimated textsize, spacesize, and estimation data % % Output: % data - a structure containing individual character and its avg intensity % intensity - mean intensity of characters in original 2D space % textWdHgt - [textWidth textHeight] % Author: % Tzu Yen Wong % wongt AT csse DOT uwa DOT edu DOT au % Department of Computer Science & Software Engineering % The University of Western Australia % % Mar 2004 %1234567890123456789012345678901234567890123456789012345678901234567890123456789 function [data,intensity,textWdHgt]=characterise(textpic,estimation) %MMMMMM initialisation MMMMMM margin = estimation{7}{1}; textWidth = estimation{1}{1}+2*margin; textHeight = estimation{2}{1}+2*margin; HSpace = estimation{3}{1}-2*margin; VSpace = estimation{4}{1}-2*margin; MS_H = estimation{8}{1}; MS_V = estimation{9}{1}; textWdHgt = [textWidth,textHeight]; x = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; x = ~x; if any(textWdHgt~=[13,13]), x=imresize(x,[textHeight textWidth]); end %MMMMMM End initialisation MMMMMM %MMMMMM checking correct textsize picture correspndance MMMMMM [r,c]=size(textpic); nrow = (r+VSpace)/(textHeight+VSpace); ncol = (c+HSpace)/(textWidth+HSpace); if nrow~=fix(nrow) || ncol~=fix(ncol), warning(sprintf('nrow=%.1f, ncol=%.1f, rounded down.',nrow,ncol)); nrow=fix(nrow); ncol=fix(ncol); end %disp(sprintf('nrow=%d, ncol=%d.',nrow,ncol)) %MMMMMM End checking correct textsize picture correspndance MMMMMM %MMMMMM adjusting nrow and ncol error due to irregular spacings MMMMMM osV = 0; % Vertical offset for ii=1:nrow, osH = 0; % Horizontal offset for jj=1:ncol, eval('osH = osH + ( MS_H(2*jj)+MS_H(2*jj+1) ) - (textWidth +HSpace);','ncol=ncol-1;') % if osH~=0, disp(sprintf('osH = %d, jj = %d',osH,jj)); end end eval('osV = osV + ( MS_V(2*ii)+MS_V(2*ii+1) ) - (textHeight+VSpace);','nrow=nrow-1;') % if osV~=0, disp(sprintf('osV = %d, ii = %d',osV,ii)); end end %MMMMMM End adjusting nrow and ncol error due to irregular spacings MMMMMM data = cell(nrow,ncol); intensity = zeros(nrow,ncol); picture = ~ones(nrow*textHeight,ncol*textWidth); %showprogress(1,[1 nrow],2,2); %MMMMMM Compositing Data structure MMMMMM osV = 0; % Vertical offset to handle possible irregular verticle spacings for ii=1:nrow, osH = 0; % Horizontal offset to handle possible irregular horizontal spacings for jj=1:ncol, eval(sprintf('%s%s%s','data{ii,jj}{1}=textpic(',... '(ii-1)*(textHeight+VSpace)+1+osV : ii*(textHeight+VSpace)-VSpace+osV ,',... '(jj-1)*(textWidth+HSpace) +1+osH : jj*(textWidth+HSpace) -HSpace+osH);'... ),'disp(sprintf(''something wrong at ii=%d,jj=%d'',ii,jj));') data{ii,jj}{2}=255*mean(mean(data{ii,jj}{1}(2:end-1,2:end-1))); if (sum(sum(intensity==data{ii,jj}{2}))>0),%&&(data{ii,jj}{2}~=255), list =find(intensity==data{ii,jj}{2}); for L=1:length(list), if isequal(data{ii,jj}{1},data{list(L)}{1}) %show([data{ii,jj}{1},data{list(L)}{1}],1); pause(.03); data{ii,jj}{1}=x; data{ii,jj}{2}=nan; end end end intensity(ii,jj)=data{ii,jj}{2}; %disp(sprintf('%d:%d,%d:%d',(ii-1)*textHeight+1,ii*textHeight,(jj-1)*textWidth+1,jj*textWidth)); %disp(size(data{ii,jj}{1})) picture((ii-1)*textHeight+1:ii*textHeight,... (jj-1)*textWidth+1:jj*textWidth) = data{ii,jj}{1}; osH = osH + ( MS_H(2*jj)+MS_H(2*jj+1) ) - (textWidth +HSpace); end % showprogress(2,[1 nrow],ii,2); osV = osV + ( MS_V(2*ii)+MS_V(2*ii+1) ) - (textHeight+VSpace); end %MMMMMM End Compositing Data structure MMMMMM %showprogress(3,[1 nrow],ii,2); show(picture,2) show(imresize(intensity,[textHeight*nrow,textWidth*ncol],'nearest'),3)