error in template matching while doing character recognition
2 views (last 30 days)
Show older comments
I am using template matching in character extraction and getting error output. Appologises for poor presentation of coding i cannot attach the function code as file since my attachment count reaches the limit. Hope reader can tolerate this difficulty. Refer the link for input image. https://www.mathworks.com/matlabcentral/answers/uploaded_files/101640/2222.jpg
function [strPlate] = Recognition(Objects,ImgChar)
I=imread('2222.jpg');
[ImgPlate] = LocationPlate(I);
[Objects,ImgChar]=Segmentation(ImgPlate);
N=struct('Image',{});
numbers={'q0','q1','sign','0','1','2','3','4','5'};
N(1).Image=imresize(im2bw(uint8(imread('untitled0.bmp'))),[100 100]);
N(2).Image=imresize(im2bw(uint8(imread('untitled1.bmp'))),[100 100]);
N(3).Image=imresize(im2bw(uint8(imread('untitled2.bmp'))),[100 100]);
N(4).Image=imresize(im2bw(uint8(imread('untitled3.bmp'))),[100 100]);
N(5).Image=imresize(im2bw(uint8(imread('untitled4.bmp'))),[100 100]);
N(6).Image=imresize(im2bw(uint8(imread('untitled5.bmp'))),[100 100]);
N(7).Image=imresize(im2bw(uint8(imread('sign.bmp'))),[100 100]);
if Objects==9 % Number of characters segmented from the plate
strPlate=[];
for i=1:Objects
char=ImgChar(:,:,i);
list_corr=[];
for j=1:7
corr=corr2(N(j).Image,char);
list_corr=[list_corr corr];
end
f=max(list_corr);
maxcorr=find(list_corr==f);
strPlate=[strPlate numbers(maxcorr)];
end
end
Code for Locationplate:
function [ImgPlate] = LocationPlate(I)
%%Cutting and resizing the original image %%
% I=imread('2222.jpg');
% figure,imshow(I);
[rows columns]=size(I);
columns=columns/3;
xmin=round(0.20*rows);
ymin=round(0.20*columns);
width=round((0.85*columns)-(0.10*columns));
height=round((0.85*rows)-(0.15*rows));
Io=imcrop(I,[xmin ymin width height]);
Io=imresize(Io,[480 640]);
Io=rgb2gray(Io);
Io=imadjust(Io);
%%Image processing to focus the area of number plate %%
%%Smooth edges and contours to delete characters.
%%Subtracting the original image to obtain the information
%%previously deleted and thus stay with the characters.
%%Select the elements with a higher level of 85.
%%Pass a mask to the image to remove excess information common to
%%all images.
se=strel('rectangle',[6 30]);
Ic=imclose(Io,se);
Ic=imadjust(Ic);
tophat=Ic-Io;
Ibw=(tophat>85);
% Ibw=Ibw1 & im2bw(imread('marco.bmp'));
% Ibw=Ibw1&&im2bw(I);r
%%Remove the related elements with fewer than 70 pixels %%
%%Remove objects that are not plate %%
plate= bwlabel(Ibw,4);
obj= max(max(plate));
dim1 = regionprops(plate, 'area')';
dim=[dim1.Area];
dim(find(dim<70))=0;
for i=1:obj
index=find(plate==i);
if dim(i)==0
plate(index)=0;
else
plate(index)=1;
end
end
CC=bwconncomp(plate);
P=regionprops(CC,'all');
[rp cp]=size(plate);
for i=1:CC.NumObjects
if P(i).MajorAxisLength>(2*cp/3)
plate(P(i).PixelIdxList(:,1))=0;
end
end
%%Remove objects that are not candidates for plate %%
se3=strel('rectangle',[30 70]);
r2=imclose(plate,se3);
se2=strel('rectangle',[5 30]);
r=imdilate(r2,se2);
CC=bwconncomp(r);
P=regionprops(CC,'all');
for i=1:CC.NumObjects
if P(i).MajorAxisLength>(2*cp/3)
r(P(i).PixelIdxList(:,1))=0;
end
end
%%select the largest connected component after preprocessing, the
%%plate
plate1= bwlabel(r,4);
dim2= regionprops(plate1, 'area')';
dim1=[dim2.Area];
f=max(dim1);
indMax=find(dim1==f);
plate1(find(plate1~=indMax))=0;
%%cutting of original image %%
[cuty, cutx] = find( plate1 > 0);
up = min(cuty);
down = max(cuty);
left = min(cutx);
right = max(cutx);
img_cut_v = Io(up:down,:,:);
img_cut_h = img_cut_v(:,left:right,:);
ImgPlate = img_cut_h;
%%different mask for location plate %%
[r c]=size(ImgPlate);
if r<25 || r>65
[rows columns]=size(I);
columns=columns/3;
xmin=round(0.20*rows);
ymin=round(0.20*columns);
width=round((0.85*columns)-(0.10*columns));
height=round((0.85*rows)-(0.15*rows));
Io=imcrop(I,[xmin ymin width height]);
Io=imresize(Io,[480 640]);
Io=rgb2gray(Io);
Io=imadjust(Io);
se=strel('rectangle',[6 30]);
Ic=imclose(Io,se);
Ic=imadjust(Ic);
tophat=Ic-Io;
Ibw1=(tophat>85);
mask=zeros(480,640);
for i=40:370
for j=40:575
mask(i,j)=1;
end
end
Ibw=Ibw1 & im2bw(mask);
plate= bwlabel(Ibw,4);
obj= max(max(plate));
dim1 = regionprops(plate, 'area')';
dim=[dim1.Area];
dim(find(dim<70))=0;
for i=1:obj
index=find(plate==i);
if dim(i)==0
plate(index)=0;
else
plate(index)=1;
end
end
CC=bwconncomp(plate);
P=regionprops(CC,'all');
[rp cp]=size(plate);
for i=1:CC.NumObjects
if P(i).MajorAxisLength>(cp/3)
plate(P(i).PixelIdxList(:,1))=0;
end
end
se3=strel('rectangle',[30 70])
r2=imclose(plate,se3);
se2=strel('rectangle',[5 30]);
r=imdilate(r2,se2);
plate1= bwlabel(r,4);
dim2= regionprops(plate1, 'area')';
dim1=[dim2.Area];
f=max(dim1);
indMax=find(dim1==f);
plate1(find(plate1~=indMax))=0;
[cuty, cutx] = find( plate1 > 0);
up = min(cuty);
down = max(cuty);
left = min(cutx);
right = max(cutx);
img_cut_v = Io(up:down,:,:);
img_cut_h = img_cut_v(:,left:right,:);
ImgPlate = img_cut_h;
end
%%Representation %%
% figure(1);
% imshow(I);
% subplot(2,2,1);imshow(I);
% subplot(2,2,2);imshow(Ic);% subplot(2,2,3);imshow(plate);
% subplot(2,2,4);imshow(plate1);
figure(2); imshow(img_cut_h);title('output location plate');
end
%code for segmentation
function [Objects,ImgChar]=Segmentation(ImgPlate)
%%Binarize the image %%
% ImgPlate=imread('ImgPlate.jpg')
level = graythresh(ImgPlate);
Ibw =(im2bw(ImgPlate,level));
%%Select the orientation of the largest object in the image.
%%Turn this angle at the picture.
%%Plate cutting to segment the characters that compose %%
Fl=bwlabel(Ibw);
Statsbf=regionprops(Fl,'all');
Flmax=find([Statsbf.Area]==max([Statsbf.Area]));
angle=Statsbf(Flmax).Orientation;
F2=imrotate(Fl,-angle);
L=bwlabel(F2);
Statsbf=regionprops(L,'all');
maxi=find([Statsbf.Area]==max([Statsbf.Area]));
BB=Statsbf(maxi).BoundingBox;
F2=imcrop(F2,[BB(1,1) BB(1,2) BB(1,3) BB(1,4)]);
% figure,imshow(F2);
%%First three and last three rows to zero.
%%First two and last two columns to zero.
%%So remove connectivity between characters and background %%
%%Remove small impurities %%
L4=not(F2);
[r c]=size(L4);
L4(1,:)=0;
L4(2,:)=0;
L4(3,:)=0;
L4(r,:)=0;
L4(r-1,:)=0;
L4(r-2,:)=0;
L4(:,1)=0;
L4(:,2)=0;
L4(:,c)=0;
L4(:,c-1)=0;
L4b=bwlabel(L4);
Stats3=regionprops(L4b,'all');
sarea3=[Stats3.Area];
G=find(sarea3<70)
for cv=1:length(G)
G1=find(L4b==G(cv));
L4(G1)=0;
end
[r c]=size(L4);
CC=bwconncomp(L4);
L=bwlabel(L4);
ind2=max(L(:,c-2));
P=regionprops(CC,'all');
%%Remove objects smaller and larger than a character %%
i=1;
if (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))<(c/13)
L4(CC.PixelIdxList{1,i})=0;
end
for i=1:CC.NumObjects
if (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))>(2*c/3)
L4(CC.PixelIdxList{1,i})=0;
end
if (max(P(i,1).PixelList(:,2))-min(P(i,1).PixelList(:,2)))<(r/3)
L4(CC.PixelIdxList{1,i})=0;
end
if (max(P(i,1).PixelList(:,1))-min(P(i,1).PixelList(:,1)))<(c/8)
L4(find(L==ind2))=0;
end
end
L4=imclose(L4,strel('disk',1));
L4=imopen(L4,strel('disk',1));
% figure(4);
imshow(L4);
L4b=bwlabel(L4);
Stats3b=regionprops(L4b,'all');
N=length(Stats3b);
while N>8
L4=imdilate(L4,strel('disk',1));
L4b=bwlabel(L4);
Stats3b=regionprops(L4b,'all');
N=length(Stats3b);
end
L4b=bwlabel(L4);
Stats3b=regionprops(L4b,'all');
ImgChar=zeros(100,100,N);
%%Dividing characters which are connected %%
%%Remove objects that have been listed as characters but are not%
%%Show every character in the correct position %%
cont=0;
cont1=0;
for i=1:N
[r1 c1]=size(Stats3b(i,1).Image);
if c1>round(c/6)
cont1=cont;
Stats3b(i,1).Image(:,round(c1/2))=0;
L5=Stats3b(i,1).Image;
CC=bwconncomp(L5);
CC1=regionprops(CC,'all');
for j=1:CC.NumObjects
[r2 c2]=size(CC1(j,1).Image);
if c2>round(c/7)
CC1(j,1).Image(:,round(c2/2))=0;
L6=CC1(j,1).Image;
LL=bwconncomp(L6);
CC2=regionprops(LL,'all');
for k=1:LL.NumObjects
CC2(k).Image=imresize(CC2(k).Image, [100 100]);
figure;imshow((CC2(k).Image))
ImgChar(:,:,i+cont1)=not(CC2(k).Image);
cont1=cont1+1;
end
cont=cont+1;
else
CC1(j).Image=imresize(CC1(j).Image, [100 100]);
figure;imshow((CC1(j).Image))
ImgChar(:,:,i+cont1)=not(CC1(j).Image);
cont1=cont1+1;
end
end
cont=cont+1;
else
Stats3b(i).Image=imresize(Stats3b(i).Image, [100 100]);
figure;imshow((Stats3b(i).Image))
if cont~=0
ImgChar(:,:,i+cont)=not(Stats3b(i).Image);
else
ImgChar(:,:,i)=not(Stats3b(i).Image);
end
end
end
%%Remove spurious %%
[x, y, Objects]=size(ImgChar);
disp(Objects);
end
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!