function foo=parse2std(arg) %assumes input from Kens parse function % Thin then thicken original image to remove difference is line thickness arg = bwmorph(arg,'thin',Inf); [xsize ysize] = size(arg); arg = bwmorph(arg,'dilate',round(max([xsize ysize])/50)); %if stuff is screwin up, its this code here % it spreads it way out in an attempt to make angled ones vertical [ydim xdim]=size(arg); %get current size after cropping if (ydim/xdim > 2.5) %almost certainly a one if this is true newxdim=ydim/.75; padsize=round((newxdim-xdim)/2); arg=[zeros(ydim,padsize) arg zeros(ydim,padsize)]; end %end potentially crappy code y=imresize(arg,[25 25],'nearest'); %resize it to a standard 25x25 image y=bwmorph(y,'thin',Inf); %thin out all extraneous thickness from fonts %the problem after thinning is that it leaves black edges that need %to be cropped, so we do that now [imgy imgx]=size(y); blackedge=1; %a variable to indicate that the current row/col is totally %black from thinning and needs to be cropped x=0; %start with the lefthand cols first while blackedge x=x+1; if any(y(:,x)) %any=1 if any element of () is nonzero blackedge=0; end end %by this point, x is set at the number of rows we crop y=imcrop(y,[x 1 imgx-1 imgy-1]); %now we crop the righthand cols [imgy imgx]=size(y); blackedge=1; x=imgx+1; while blackedge x=x-1; if any(y(:,x)) blackedge=0; end end y=imcrop(y,[1 1 x-1 imgy-1]); [imgy imgx]=size(y); blackedge=1; x=0; %start with the top rows first while blackedge x=x+1; if any(y(x,:)) blackedge=0; end end y=imcrop(y,[1 x imgx-1 imgy-1]); %now we crop the bottom rows [imgy imgx]=size(y); blackedge=1; x=imgy+1; while blackedge x=x-1; if any(y(x,:)) blackedge=0; end end y=imcrop(y,[1 1 imgx-1 x-1]); %here we address the problem of the handwritten one, which by this point %is very tall and very skinny - when it gets resized, it gets very fat, %looking more like a rectangle than a one. So we test the aspect ratio to %see if what we have is too skinny. If so, we fatten it up by padding it %on the left and right with zeros. This makes it phat. [ydim xdim]=size(y); %get current size after cropping if (ydim/xdim > 2.5) %almost certainly a one if this is true newxdim=ydim/2.5; padsize=round((newxdim-xdim)/2); y=[zeros(ydim,padsize) y zeros(ydim,padsize)]; end y=imresize(y,[25 25],'nearest'); %resize it to a standard 25x25 image y=bwmorph(y,'dilate'); %thicken the image a bit y=bwmorph(y,'dilate'); %dont know if we wanna do that twice, but... y=255*double(y); foo=y;