[BMPfile, pathname] = uigetfile('*.bmp;*.jpg',...
'HE KE : Please select the image as you like');
cd (pathname);
file_name=sprintf('%s\\%s',pathname,BMPfile);
BMP=imread(file_name);
BW=im2bw(BMP,graythresh(BMP));
BWO=~BW;
[L,N] = bwlabel(BWO,4);
for i=1:N
[r,c] = find(L == i);
area=size(r,1); % area of the object
r2=unique(r);
Qr=histc(r,r2);
HLength=max(Qr); % Horizontal length of the object
c2=unique(c);
Qc=histc(c,c2);
VHeight=max(Qc); % Vertical height of the object
if area > 10000 || HLength > 35 || VHeight > 35 % Threshold can be adjusted to define the size of the object which is wanted to be removed.
BMP(r,c,1:3)=150;
BW(r,c)=1;
end
clear r
clear c
end
figure(1)
imshow(BMP);
title('removed the large objects');
%save large square removed image
[~,sz]=size(file_name);
new=file_name(1:sz-4);
new(sz-3:sz+4)='_new.bmp';
imwrite(BMP,new);
figure(2)
imshow(BW);
title('binary image after removed large square');