function imout = crop(inim)
%function to crop a binary image
%written for Malayalam OCR on 21/10/2008
%--usage
% outputimage=crop(inputimage);
%--inputs
% in = image binary data
%
%--output
% imout= the cropped image
if ~islogical(inim) %check if the image is binary ?
level=graythresh(inim); %if not find the threshold level
inim=im2bw(inim,level); %convert it to binary with the threshold level obtained
end
i=~inim; %invert the image ,
[x y]=find(i); %find indices of nonzero elemnts
xmin=min(x); %find lowest row index of nonzero element
xmax=max(x); %find highest row index of nonzero element
ymin=min(y); %find lowest column index of nonzero element
ymax=max(y); %find highest column index of nonzero element
imout=i(xmin:xmax,ymin:ymax); %crop out the white parts
imout=~imout; %invert the image