clear ;
close all;
%Step1 Get images Load the color image to be processed and display the original image
Scolor = imread('3.jpg');%imread Function to read the image file
% Convert color images to black and white and display
Sgray = rgb2gray(Scolor);%rgb2gray Convert to grayscale
figure,imshow(Scolor),title(' Original color image ');%figure Command to display two images at the same time
figure,imshow(Sgray),title(' Original black and white image ');
%Step2 Image preprocessing Yes Sgray The original black-and-white image is opened to obtain the image background
s=strel('disk',13);%strei function
Bgray=imopen(Sgray,s);% open sgray s Images
figure,imshow(Bgray);title(' background image ');% Output background image
% Subtract the original image from the background image , Enhance the image
Egray=imsubtract(Sgray,Bgray);% Subtract the two pictures
figure,imshow(Egray);title(' Enhance black and white images ');% Output black and white images
%Step3 Get the best threshold , Binarize the image
fmax1=double(max(max(Egray)));%egray And output double precision
fmin1=double(min(min(Egray)));%egray And output double precision
level=(fmax1-(fmax1-fmin1)/3)/255;% Get the best threshold
bw22=im2bw(Egray,level);% Convert image to binary image
bw2=double(bw22);
%Step4 The binary image is filtered by opening and closing operation
figure,imshow(bw2);title(' Image binarization ');% Get a binary image
grd=edge(bw2,'canny')% use canny The operator recognizes the boundary in the intensity image
figure,imshow(grd);title(' Image edge extraction ');% Output image edge
bg1=imclose(grd,strel('rectangle',[5,19]));% Take the closed operation of the rectangular box
figure,imshow(bg1);title(' Image closed operation [5,19]');% Output the image of closed operation
bg3=imopen(bg1,strel('rectangle',[5,19]));% Open operation of rectangular box
figure,imshow(bg3);title(' Image open operation [5,19]');% Output the image of open operation
bg2=imopen(bg3,strel('rectangle',[19,1]));% Open operation of rectangular box
figure,imshow(bg2);title(' Image open operation [19,1]');% Output the image of open operation
%Step5 Region extraction of binary image , And calculate the regional characteristic parameters . Compare the regional characteristic parameters , Extract the license plate area
[L,num] = bwlabel(bg2,8);% Label the connected part of the binary image
Feastats = regionprops(L,'basic');% Calculate the feature size of the image area
Area=[Feastats.Area];% Area
BoundingBox=[Feastats.BoundingBox];%[x y width height] The frame size of the license plate
RGB = label2rgb(L, 'spring', 'k', 'shuffle'); % Logo image to RGB Image conversion
figure,imshow(RGB);title(' Image color marking ');% Output the color image of the frame
lx=0;
for l=1:num
width=BoundingBox((l-1)*4+3);% Calculation of frame width
hight=BoundingBox((l-1)*4+4);% Calculation of frame height
if (width>98 & width<160 & hight>25 & hight<50)% The width and height of the frame
lx=lx+1;
Getok(lx)=l;
end
end
for k= 1:lx
l=Getok(k);
startcol=BoundingBox((l-1)*4+1)-2;% Start the column
startrow=BoundingBox((l-1)*4+2)-2;% Go ahead
width=BoundingBox((l-1)*4+3)+8;% The license plate is wide
hight=BoundingBox((l-1)*4+4)+2;% License plate height
rato=width/hight;% Calculate the aspect ratio of the license plate
if rato>2 & rato<4
break;
end
end
sbw1=bw2(startrow:startrow+hight,startcol:startcol+width-1); % Get the binary subgraph of the license plate
subcol1=Sgray(startrow:startrow+hight,startcol:startcol+width-1);% Obtain the gray sub image of the license plate
figure,subplot(2,1,1),imshow(subcol1);title(' Gray level subgraph of license plate ');% Output grayscale image
subplot(2,1,2),imshow(sbw1);title(' License plate binary subgraph ');% Output the binary diagram of the license plate
%Step6 Calculate the horizontal projection of the license plate , The peak valley analysis of horizontal projection
histcol1=sum(sbw1); % Calculate the vertical projection
histrow=sum(sbw1'); % Calculate the horizontal projection
figure,subplot(2,1,1),bar(histcol1);title(' Vertical projection ( With border )');% Output vertical projection
subplot(2,1,2),bar(histrow); title(' Horizontal projection ( With border )');% Output horizontal projection
figure,subplot(2,1,1),bar(histrow); title(' Horizontal projection ( With border )');% Output horizontal projection
subplot(2,1,2),imshow(sbw1);title(' License plate binary subgraph ');% Output binary graph
% Peak valley analysis of horizontal projection
meanrow=mean(histrow);% Find the average of the horizontal projection
minrow=min(histrow);% Find the minimum value of horizontal projection
levelrow=(meanrow+minrow)/2;% Find the average of the horizontal projection
count1=0;
l=1;
for k=1:hight
if histrow(k)<=levelrow
count1=count1+1;
else
if count1>=1
markrow(l)=k;% Rising point
markrow1(l)=count1;% Valley width ( From the falling point to the next rising point )
l=l+1;
end
count1=0;
end
end
markrow2=diff(markrow);% Peak distance ( Rising point to the next rising point )
[m1,n1]=size(markrow2);
n1=n1+1;
markrow(l)=hight;
markrow1(l)=count1;
markrow2(n1)=markrow(l)-markrow(l-1);
l=0;
for k=1:n1
markrow3(k)=markrow(k+1)-markrow1(k+1);% Drop point
markrow4(k)=markrow3(k)-markrow(k);% Peak width ( Rising point to falling point )
markrow5(k)=markrow3(k)-double(uint16(markrow4(k)/2));% Peak center position
end
%Step7 Calculate the license plate rotation angle
%(1) Find the first one from the rising point to the falling point as 1 The point of
[m2,n2]=size(sbw1);%sbw1 The image size of
[m1,n1]=size(markrow4);%markrow4 Size
maxw=max(markrow4);% The maximum width is characters
if markrow4(1) ~= maxw% Check the upper
ysite=1;
k1=1;
for l=1:n2
for k=1:markrow3(ysite)% Scan from the top edge to the falling point of the first peak
if sbw1(k,l)==1
xdata(k1)=l;
ydata(k1)=k;
k1=k1+1;
break;
end
end
end
else % Check the bottom
ysite=n1;
if markrow4(n1) ==0
if markrow4(n1-1) ==maxw
ysite= 0; % No bottom
else
ysite= n1-1;
end
end
if ysite ~=0
k1=1;
for l=1:n2
k=m2;
while k>=markrow(ysite) % Scan from the bottom to the rising point of the last peak
if sbw1(k,l)==1
xdata(k1)=l;
ydata(k1)=k;
k1=k1+1;
break;
end
k=k-1;
end
end
end
end
%(2) Linear fitting , Calculation and x Angle
fresult = fit(xdata',ydata','poly1'); %poly1 Y = p1*x+p2
p1=fresult.p1;
angle=atan(fresult.p1)*180/pi; % Change radians into degrees ,360/2pi, pi=3.14
%(3) Rotate the license plate image
subcol = imrotate(subcol1,angle,'bilinear','crop'); % Rotate the license plate image
sbw = imrotate(sbw1,angle,'bilinear','crop');% Rotated image
figure,subplot(2,1,1),imshow(subcol);title(' Gray level subgraph of license plate ');% Output the gray image title after license plate rotation, and display the gray sub image of license plate
subplot(2,1,2),imshow(sbw);title('');% Output the gray image of the license plate after rotation
title([' License plate rotation angle : ',num2str(angle),' degree '] ,'Color','r');% Display the rotation angle of the license plate
%Step8 After rotating the license plate, recalculate the horizontal projection of the license plate , Remove the horizontal border of the license plate , Gets the character height
histcol1=sum(sbw); % Calculate the vertical projection
histrow=sum(sbw'); % Calculate the horizontal projection
figure,subplot(2,1,1),bar(histcol1);title(' Vertical projection ( After rotation )');
subplot(2,1,2),bar(histrow); title(' Horizontal projection ( After rotation )');
figure,subplot(2,1,1),bar(histrow); title(' Horizontal projection ( After rotation )');
subplot(2,1,2),imshow(sbw);title(' License plate binary subgraph ( After rotation )');
% Go to the level ( Up and down ) Frame , Gets the character height
maxhight=max(markrow2);
findc=find(markrow2==maxhight);
rowtop=markrow(findc);
rowbot=markrow(findc+1)-markrow1(findc+1);
sbw2=sbw(rowtop:rowbot,:); % The subgraph is (rowbot-rowtop+1) That's ok
maxhight=rowbot-rowtop+1; % character height (rowbot-rowtop+1)
%Step9 Calculate the vertical projection of the license plate , Remove the vertical border of the license plate , Get the average width of license plate and characters
histcol=sum(sbw2); % Calculate the vertical projection
figure,subplot(2,1,1),bar(histcol);title(' Vertical projection ( After removing the horizontal border )');% Output the vertical projection image of the license plate
subplot(2,1,2),imshow(sbw2); % Output vertical projection image
title([' License plate character height : ',int2str(maxhight)],'Color','r');% Output license plate character height
% Peak valley analysis of vertical projection
meancol=mean(histcol);% Find the average of the vertical projection
mincol=min(histcol);% Find the average of the vertical projection
levelcol=(meancol+mincol)/4;% Find the of vertical projection 1/4
count1=0;
l=1;
for k=1:width
if histcol(k)<=levelcol
count1=count1+1;
else
if count1>=1
markcol(l)=k; % Character rising point
markcol1(l)=count1; % Valley width ( From the falling point to the next rising point )
l=l+1;
end
count1=0;
end
end
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.