Now, before finding the parameters of the LP (i.e. aspect ratio, orientation angle, and Euler's number) on the image under study to continue the CCA phase, it is important to identify the limits of these parameters (maximum and minimum required values to which the LP should be restricted). Generally, this should be done on a pool of images (like 50 plus images), but seeing that I was restricted to a pool of 5 images (my professor back then applied that restriction), that was what I did: use 5 images.
Note: The code for identifying the limits of each parameter is run on a separate m-file.
The code for
detecting the LP aspect ratio limits is:
% Detecting Aspect Ratios of LP
% This program detects the aspect ratio of each of the five studied license
% plates. First, the objects in each image is labelled with bwlabel
% (classical approach of connected component labeling with 8-connectivity),
% then the label(s) identifying the boarders of the license plates are
% used to extract the position coordinates. The aspect ratio is then
% calculated according to the equation: (cmax - cmin + 1)/(rmax - rmin + 1).
clear all
clc
ob_cnt = 0;
% Generating Gaussian mask to be used for blurring thresholded picture
% before performing object labeling
% sig = fix(randn*10); % Generating gradient of Gaussian masks for filtering
% % along x-axis (fx) and y-axis (fy)
% while (sig == 0)||(mod(sig,2)==0)||(abs(sig)>5)
% sig = fix(randn*10); % Ensure that masks formed are odd matrices
% end % each of whose size is no greater than 15x15
% sig = abs(sig); % Sigma should be positive
% After generating several Gaussian masks, best result obtained for sig = 1
sig = 1;
n = 3*sig;
for i=1:n
for j=1:n
g(i,j) = exp(-(i^2+j^2)/2*sig^2);
end
end
figure(ob_cnt+1)
I1 = imread('image 1.jpg'); % Importing picture of license plate 1
I1 = rgb2gray(I1);
imshow(I1,[]); % Show grayscale picture
[M1,N1] = size(I1);
figure(ob_cnt+2)
I_bin1 = edge(I1,'canny'); % Performing edge detection with Canny method
imshow(I_bin1,[]); % Show edge detection
figure(ob_cnt+3)
f1 = conv2(double(I_bin1),double(g));
f1 = f1(1:M1,1:N1);
[W1,num1] = bwlabel(f1,8); % Labeling with Matlab command bwlabel
imshow(W1,[]);
figure(ob_cnt+4)
% Extracting LP from middle section where expected to be (using a cropping algorithm which is not shown
% here)
imshow(W1(600:910,990:1500),[]);
figure(ob_cnt+5)
% Tracking label of LP boarders at middle section where the LP is expected to be. This is done by conducting
% a frequency count on each object label found in the extracted LP zone; since the LP is the largest object in
% the restricted zone, the object label of the second highest frequency count (the first corresponding to the
% image background) corresponds to the LP itself. This process is not shown here; instead, the object label
% found i.e. 342 is used.
L1 = zeros(M1,N1);
[r1,c1,v1] = find(W1==342); % Final result
label1 = vertcat(r1',c1');
for i = 1:length(label1)
L1(label1(1,i),label1(2,i)) = W1(label1(1,i),label1(2,i));
end
imshow(L1,[]);
% clear L1 % Used for clearing image L1 upon label tracking until correct
% label is found
r1_max = max(label1(1,:))
r1_min = min(label1(1,:))
c1_max = max(label1(2,:))
c1_min = min(label1(2,:))
a1 = (c1_max - c1_min + 1)/(r1_max - r1_min + 1)
%--------------------------------------------------------------------------
ob_cnt = ob_cnt + 5;
figure(ob_cnt+1)
I2 = imread('image 2.jpg'); % Importing picture of license plate 2
I2 = rgb2gray(I2);
imshow(I2,[]); % Show grayscale picture
[M2,N2] = size(I2);
figure(ob_cnt+2)
I_bin2 = edge(I2,'canny'); % Performing edge detection with Canny method
imshow(I_bin2,[]); % Show edge detection
figure(ob_cnt+3)
f2 = conv2(double(I_bin2),double(g));
f2 = f2(1:M2,1:N2);
[W2,num2] = bwlabel(f2,8); % Labeling with Matlab command bwlabel
imshow(W2,[]);
figure(ob_cnt+4)
% Extracting LP from middle section where expected to be
imshow(W2(750:1100,900:1650),[]);
figure(ob_cnt+5)
% Tracking label of LP boarders at middle section where the LP is expected to be
L2 = zeros(M2,N2);
[r2,c2,v2] = find(W2==364); % Final result
label2 = vertcat(r2',c2');
for i = 1:length(label2)
L2(label2(1,i),label2(2,i)) = W2(label2(1,i),label2(2,i));
end
imshow(L2,[]);
% clear L2 % Used for clearing image L2 upon label tracking until correct
% % label is found
r2_max = max(label2(1,:))
r2_min = min(label2(1,:))
c2_max = max(label2(2,:))
c2_min = min(label2(2,:))
a2 = (c2_max - c2_min + 1)/(r2_max - r2_min + 1)
%--------------------------------------------------------------------------
ob_cnt = ob_cnt + 5;
figure(ob_cnt+1)
I3 = imread('image 3.jpg'); % Importing picture of license plate 3
I3 = rgb2gray(I3);
imshow(I3,[]); % Show grayscale picture
[M3,N3] = size(I3);
figure(ob_cnt+2)
I_bin3 = edge(I3,'canny'); % Performing edge detection with Canny method
imshow(I_bin3,[]); % Show edge detection
figure(ob_cnt+3)
f3 = conv2(double(I_bin3),double(g));
f3 = f3(1:M3,1:N3);
[W3,num3] = bwlabel(f3,8); % Labeling with Matlab command bwlabel
imshow(W3,[]);
figure(ob_cnt+4)
% Extracting LP from middle section where expected to be
imshow(W3(700:1100,1100:1700),[]);
figure(ob_cnt+5)
% Tracking label of LP boarders at middle section where the LP is expected to be
L3 = zeros(M3,N3);
[r3,c3,v3] = find(W3==369); % Final result
label3 = vertcat(r3',c3');
for i = 1:length(label3)
L3(label3(1,i),label3(2,i)) = W3(label3(1,i),label3(2,i));
end
imshow(L3,[]);
% clear L3 % Used for clearing image L3 upon label tracking until correct
% % label is found
r3_max = max(label3(1,:))
r3_min = min(label3(1,:))
c3_max = max(label3(2,:))
c3_min = min(label3(2,:))
a3 = (c3_max - c3_min + 1)/(r3_max - r3_min + 1)
%--------------------------------------------------------------------------
ob_cnt = ob_cnt + 5;
figure(ob_cnt+1)
I4 = imread('image 4.jpg'); % Importing picture of license plate 4
I4 = rgb2gray(I4);
imshow(I4,[]); % Show grayscale picture
[M4,N4] = size(I4);
figure(ob_cnt+2)
I_bin4 = edge(I4,'canny'); % Performing edge detection with Canny method
imshow(I_bin4,[]); % Show edge detection
figure(ob_cnt+3)
f4 = conv2(double(I_bin4),double(g));
f4 = f4(1:M4,1:N4);
[W4,num4] = bwlabel(f4,8); % Labeling with Matlab command bwlabel
imshow(W4,[]);
figure(ob_cnt+4)
% Extracting LP from middle section where expected to be
imshow(W4(620:1020,1000:1650),[]);
figure(ob_cnt+5)
% Tracking label of LP boarders at middle section where the LP is expected to be
L4 = zeros(M4,N4);
[r4,c4,v4] = find(W4==8); % Final result
label4 = vertcat(r4',c4');
for i = 1:length(label4)
L4(label4(1,i),label4(2,i)) = W4(label4(1,i),label4(2,i));
end
imshow(L4,[]);
% clear L4 % Used for clearing image L4 upon label tracking until correct
% % label is found
r4_max = max(label4(1,:))
r4_min = min(label4(1,:))
c4_max = max(label4(2,:))
c4_min = min(label4(2,:))
a4 = (c4_max - c4_min + 1)/(r4_max - r4_min + 1)
%--------------------------------------------------------------------------
ob_cnt = ob_cnt + 5;
figure(ob_cnt+1)
I5 = imread('image 5.jpg'); % Importing picture of license plate 5
I5 = rgb2gray(I5);
imshow(I5,[]); % Show grayscale picture
[M5,N5] = size(I5);
figure(ob_cnt+2)
I_bin5 = edge(I5,'canny'); % Performing edge detection with Canny method
imshow(I_bin5,[]); % Show edge detection
figure(ob_cnt+3)
f5 = conv2(double(I_bin5),double(g));
f5 = f5(1:M5,1:N5);
[W5,num5] = bwlabel(f5,8); % Labeling with Matlab command bwlabel
imshow(W5,[]);
figure(ob_cnt+4)
% Extracting LP from middle section where expected to be
imshow(W5(600:1100,710:1850),[]);
figure(ob_cnt+5)
% Tracking label of LP boarders at middle section where the LP is expected to be
L5 = zeros(M5,N5);
[r5,c5,v5] = find(W5==124); % Final result
label5 = vertcat(r5',c5');
for i = 1:length(label5)
L5(label5(1,i),label5(2,i)) = W5(label5(1,i),label5(2,i));
end
imshow(L5,[]);
% clear L5 % Used for clearing image L5 upon label tracking until correct
% % label is found
r5_max = max(label5(1,:))
r5_min = min(label5(1,:))
c5_max = max(label5(2,:))
c5_min = min(label5(2,:))
a5 = (c5_max - c5_min + 1)/(r5_max - r5_min + 1)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The code for
detecting the LP orientation angle limits is:
% Detecting Orientation Angles of LP
% This program detects the orientation angle of each of the five studied
% license plates. First, the objects in each image is labelled with bwlabel
% (classical approach of connected component labeling with 8-connectivity),
% then the labeled matrix is used for calculating the orientation angle,
% which is calculated according to the equation:
% tan(phi_i) = 2[r_sum(c_sum(rcWi(r,c))]/[r_sum(c_sum(r^2Wi(r,c)) -
% r_sum(c_sum(c^2Wi(r,c))]
clear all
clc
ob_cnt = 0;
% Generating Gaussian mask to be used for blurring thresholded picture
% before performing object labeling
% sig = fix(randn*10); % Generating gradient of Gaussian masks for filtering
% % along x-axis (fx) and y-axis (fy)
% while (sig == 0)||(mod(sig,2)==0)||(abs(sig)>5)
% sig = fix(randn*10); % Ensure that masks formed are odd matrices
% end % each of whose size is no greater than 15x15
% sig = abs(sig); % Sigma should be positive
% After generating several Gaussian masks, best result obtained for sig = 1
sig = 1;
n = 3*sig;
for i=1:n
for j=1:n
g(i,j) = exp(-(i^2+j^2)/2*sig^2);
end
end
figure(ob_cnt+1)
I1 = imread('image 1.jpg'); % Importing picture of license plate 1
I1 = rgb2gray(I1);
imshow(I1,[]); % Show grayscale picture
[M1,N1] = size(I1);
figure(ob_cnt+2)
I_bin1 = edge(I1,'canny'); % Performing edge detection with Canny method
imshow(I_bin1,[]); % Show edge detection
figure(ob_cnt+3)
f1 = conv2(double(I_bin1),double(g));
f1 = f1(1:M1,1:N1);
[W1,num1] = bwlabel(f1,8); % Labeling with Matlab command bwlabel
imshow(W1,[]);
figure(ob_cnt+4)
L1 = zeros(M1,N1);
[r1,c1,v1] = find(W1==342);
label1 = vertcat(r1',c1');
for i = 1:length(label1)
L1(label1(1,i),label1(2,i)) = W1(label1(1,i),label1(2,i));
end
imshow(L1,[]);
for i = 1:M1
for j = 1:N1
rc_component1(i,j) = i*j*L1(i,j);
end
end
for i = 1:M1
for j = 1:N1
r2_component1(i,j) = i^2*L1(i,j);
end
end
for i = 1:M1
for j = 1:N1
c2_component1(i,j) = j^2*L1(i,j);
end
end
phi1 = atand((2*sum(sum(rc_component1)))/...
(sum(sum(r2_component1))-sum(sum(c2_component1))))
%--------------------------------------------------------------------------
ob_cnt = ob_cnt + 4;
figure(ob_cnt+1)
I2 = imread('image 2.jpg'); % Importing picture of license plate 2
I2 = rgb2gray(I2);
imshow(I2,[]); % Show grayscale picture
[M2,N2] = size(I2);
figure(ob_cnt+2)
I_bin2 = edge(I2,'canny'); % Performing edge detection with Canny method
imshow(I_bin2,[]); % Show edge detection
figure(ob_cnt+3)
f2 = conv2(double(I_bin2),double(g));
f2 = f2(1:M2,1:N2);
[W2,num2] = bwlabel(f2,8); % Labeling with Matlab command bwlabel
imshow(W2,[]);
figure(ob_cnt+4)
L2 = zeros(M2,N2);
[r2,c2,v2] = find(W2==364);
label2 = vertcat(r2',c2');
for i = 1:length(label2)
L2(label2(1,i),label2(2,i)) = W2(label2(1,i),label2(2,i));
end
imshow(L2,[]);
for i = 1:M2
for j = 1:N2
rc_component2(i,j) = i*j*L2(i,j);
end
end
for i = 1:M2
for j = 1:N2
r2_component2(i,j) = i^2*L2(i,j);
end
end
for i = 1:M2
for j = 1:N2
c2_component2(i,j) = j^2*L2(i,j);
end
end
phi2 = atand((2*sum(sum(rc_component2)))/...
(sum(sum(r2_component2))-sum(sum(c2_component2))))
%--------------------------------------------------------------------------
ob_cnt = ob_cnt + 4;
figure(ob_cnt+1)
I3 = imread('image 3.jpg'); % Importing picture of license plate 3
I3 = rgb2gray(I3);
imshow(I3,[]); % Show grayscale picture
[M3,N3] = size(I3);
figure(ob_cnt+2)
I_bin3 = edge(I3,'canny'); % Performing edge detection with Canny method
imshow(I_bin3,[]); % Show edge detection
figure(ob_cnt+3)
f3 = conv2(double(I_bin3),double(g));
f3 = f3(1:M3,1:N3);
[W3,num3] = bwlabel(f3,8); % Labeling with Matlab command bwlabel
imshow(W3,[]);
figure(ob_cnt+4)
L3 = zeros(M3,N3);
[r3,c3,v3] = find(W3==369);
label3 = vertcat(r3',c3');
for i = 1:length(label3)
L3(label3(1,i),label3(2,i)) = W3(label3(1,i),label3(2,i));
end
imshow(L3,[]);
for i = 1:M3
for j = 1:N3
rc_component3(i,j) = i*j*L3(i,j);
end
end
for i = 1:M3
for j = 1:N3
r2_component3(i,j) = i^2*L3(i,j);
end
end
for i = 1:M3
for j = 1:N3
c2_component3(i,j) = j^2*L3(i,j);
end
end
phi3 = atand((2*sum(sum(rc_component3)))/...
(sum(sum(r2_component3))-sum(sum(c2_component3))))
%--------------------------------------------------------------------------
ob_cnt = ob_cnt + 4;
figure(ob_cnt+1)
I4 = imread('image 4.jpg'); % Importing picture of license plate 4
I4 = rgb2gray(I4);
imshow(I4,[]); % Show grayscale picture
[M4,N4] = size(I4);
figure(ob_cnt+2)
I_bin4 = edge(I4,'canny'); % Performing edge detection with Canny method
imshow(I_bin4,[]); % Show edge detection
figure(ob_cnt+3)
f4 = conv2(double(I_bin4),double(g));
f4 = f4(1:M4,1:N4);
[W4,num4] = bwlabel(f4,8); % Labeling with Matlab command bwlabel
imshow(W4,[]);
figure(ob_cnt+4)
L4 = zeros(M4,N4);
[r4,c4,v4] = find(W4==8);
label4 = vertcat(r4',c4');
for i = 1:length(label4)
L4(label4(1,i),label4(2,i)) = W4(label4(1,i),label4(2,i));
end
imshow(L4,[]);
for i = 1:M4
for j = 1:N4
rc_component4(i,j) = i*j*L4(i,j);
end
end
for i = 1:M4
for j = 1:N4
r2_component4(i,j) = i^2*L4(i,j);
end
end
for i = 1:M4
for j = 1:N4
c2_component4(i,j) = j^2*L4(i,j);
end
end
phi4 = atand((2*sum(sum(rc_component4)))/...
(sum(sum(r2_component4))-sum(sum(c2_component4))))
%--------------------------------------------------------------------------
ob_cnt = ob_cnt + 4;
figure(ob_cnt+1)
I5 = imread('image 5.jpg'); % Importing picture of license plate 5
I5 = rgb2gray(I5);
imshow(I5,[]); % Show grayscale picture
[M5,N5] = size(I5);
figure(ob_cnt+2)
I_bin5 = edge(I5,'canny'); % Performing edge detection with Canny method
imshow(I_bin5,[]); % Show edge detection
figure(ob_cnt+3)
f5 = conv2(double(I_bin5),double(g));
f5 = f5(1:M5,1:N5);
[W5,num5] = bwlabel(f5,8); % Labeling with Matlab command bwlabel
imshow(W5,[]);
figure(ob_cnt+4)
L5 = zeros(M5,N5);
[r5,c5,v5] = find(W5==124);
label5 = vertcat(r5',c5');
for i = 1:length(label5)
L5(label5(1,i),label5(2,i)) = W5(label5(1,i),label5(2,i));
end
imshow(L5,[]);
for i = 1:M5
for j = 1:N5
rc_component5(i,j) = i*j*L5(i,j);
end
end
for i = 1:M5
for j = 1:N5
r2_component5(i,j) = i^2*L5(i,j);
end
end
for i = 1:M5
for j = 1:N5
c2_component5(i,j) = j^2*L5(i,j);
end
end
phi5 = atand((2*sum(sum(rc_component5)))/...
(sum(sum(r2_component5))-sum(sum(c2_component5))))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
As for the
Euler number, the limit is always 3, because in each LP there should be at least three characters. Hence, no code needed to identify limit.
The extracted LP zone in one of the pool images (which is also the studied image) is:
The image resulting from the LP's label tracking (not necessary to perform, but it would give you a good idea about the accuracy of your algorithm concerning aspect ratio) for the same pool image is:
The limit values of the aspect ratio are 1.5 and 5.5, while those of the orientation angle are 105 and 140 (offset by pi or 180 degrees, no reason for the offset except that I wanted to maintain a positive value for the angle).