clc;
clear;
close all;
x=imread('D:\test\1 (11).jpg');
x=rgb2gray(x);
x=medfilt2(x,[3 3]);
% x=adapthisteq(x);
bw=im2bw(x,0.3);
figure; imshow(bw);
SE=ones(1,30); % the maximum space between characters
y=imclose(bw,SE); % A closing operation is performed on bi-nary edge image using this SE
figure; imshow(y);
% Applying an opening operation on the image with a vertical SE (whose
% height is taken as minimum character height) eliminates the
% regions whose height is less than the minimum character
% height.
SE=ones(1,45);
y_old=imopen(y,SE');
figure; imshow(y);
% First opening the image (Fig. 8) using a SE designed considering maximum license plate height.
SE=ones(1,170);
y=imopen(y_old,SE');
figure; imshow(y);
% By subtracting, this results in the elimination ofregions with height greater than
% the maximum license plate height. Thle resultant image containing
% the license plate region along with some small noise blobs
y_new=abs(y_old-y);
figure; imshow(y_new);
% Finally, an image opening operation with
% horizonital SE (SE width is less thani minimum license plate
% width) eliminiates the noise blobs whose width is less than
% minimum width of license plate.
SE=ones(1,200);
y=imopen(y_new,SE);
figure; imshow(y);
T=immultiply(bw,y);
figure; imshow(T);
u=T;
[a b]=size(u);
% Horizontal Smearing
for i=1:a
c=0;
for j=1:b-1
if u(i,j)==1
c=c+1;
if u(i,j+1)==0 || j==b-1
if c>600;
for k=j-c+1:j;
u(i,k)=0;
end
% else
% if c<20
% for k=j-c+1:j;
% u(i,k)=0;
% end
% end
end
else
end
end
end
end
% % Vertical Smearing
for i=1:b
c=0;
for j=1:a-1;
if u(j,i)==1
c=c+1;
if u(j+1,i)==0 || j==a-1
if c>220;
for k=j-c+1:j;
u(k,i)=0;
end
% else
% if c<20
% for k=j-c+1:j;
% u(k,i)=0;
% end
% end
end
else
end
end
end
end
figure; imshow(u);
u=imclearborder(u);
SE=ones(1,3);
u=imerode(u,SE);
figure; imshow(u);
edges=imfill(u,'holes');
edges=bwareaopen(edges,550);
figure; imshow(edges);
uu=abs(u-edges);
uu=imclearborder(uu,26);
figure; imshow(uu);
stats=regionprops(uu);
for index=1:length(stats)
% if stats(index).Area >= 90&& stats(index).Area < 950 && stats(index).BoundingBox(3) < 90 && stats(index).BoundingBox(3) >2
x = ceil(stats(index).BoundingBox(1));
y= ceil(stats(index).BoundingBox(2));
widthX = floor(stats(index).BoundingBox(3)-1);
widthY = floor(stats(index).BoundingBox(4)-1);
if y-20<1
w=1;
else
w=y-20;
end
if y+widthY+20>a
ww=a;
else
ww=y+widthY+20;
end
stats(index).Area
subimage(index) = {uu(w:ww,x:x+widthX,:)};
% g=cell2mat(subimage(index));
% o=o+1;
% name=num2str(o);
% imwrite(g,['C:\Documents and Settings\engmsma\My Documents\Downloads\New Folder (2)\' name],'jpg');
figure, imshow(subimage{index})
% end
end