-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberPlateExtraction1.m
243 lines (219 loc) · 9.36 KB
/
numberPlateExtraction1.m
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
function [noPlate,VehicleClass,Amount] = numberPlateExtraction1(img,InOut)
% This fuction - 'numberPlateExtraction' extracts the characters from the input number plate image.
% when number plate image is closer with some part of the car if it fails
% then run numberPlateExtraction2
%% Input Image
f=imread('print2.jpg');
%f=img;
figure(1)
subplot(231);imagesc(f);
title('original image');
%% Preprocessing
% Resizing the image keeping aspect ratio same.
f=imresize(f,[400 NaN]);
% Converting the RGB (color) image to gray (intensity).
g=rgb2gray(f);
colormap(gray)
subplot(232);imagesc(g);
title('grayscale-resized');
% Median filtering to remove noise.
g=medfilt2(g,[3 3]);
%% Morphological processing
se=strel('disk',1);
% Dilation
gi=imdilate(g,se);
gi=imdilate(gi,se);
%gi=imdilate(gi,se);
subplot(233);imagesc(gi);
title('dilated image');
% Erosion
ge=g;
%ge=imerode(ge,se);
subplot(234);imagesc(ge);
title('eroded image');
% Morphological Gradient for edges enhancement.
gdiff=imsubtract(gi,ge);
% Converting the matrix to double image.
gdiff=mat2gray(gdiff);
% Convolution of double image for brightening edges.
gdiff=conv2(gdiff,[1 1;1 1]);
% Adjust the contrast of image, specifying contrast limits. Intensity scaling between the range 0 to 1.
gdiff=imadjust(gdiff,[0.5 0.7],[0 1],0.1);
% Conversion from double to binary.
B=logical(gdiff);
% Here B is the final b/w image of car for extracting number plate
subplot(235);imagesc(B);
title('Subtracted image dilated-eroded');
%% For the extraction of number plate region
% Eliminating the possible horizontal lines that could be edges of license plate.
% locating horizontal lines
er=imerode(B,strel('line',50,0));
% deletiong horizontal lines from b/w image B
out1=imsubtract(B,er);
% locating vertical lines
er=imerode(out1,strel('line',50,90));
% deletiong vertical lines after deleting horizontal lines
out1=imsubtract(out1,er);
subplot(236);imagesc(out1);
title('After Eliminating Horizontal and Vertical');
% Filling all the regions of the image.
F=imfill(out1,'holes');
figure(2)
colormap('gray')
subplot(231);imagesc(out1);
title('Filling holes in Image');
% Thinning the image to ensure character isolation.
H=bwmorph(F,'thin',1);
H=imerode(H,strel('line',3,90));
subplot(232);imagesc(H);
title('Thinning image');
% Selecting all the regions that are of pixel area more than 100.
final=bwareaopen(H,100);
subplot(233);imagesc(final);
title('Final Image after eliminating smaller regions');
% Bounding boxes are acquired.
Iprops=regionprops(final,'BoundingBox','Image');
% Selecting all the bounding boxes
NR=cat(1,Iprops.BoundingBox);
figure(3)
colormap('gray')
% Calling of controlling function.
r=controlling(NR); % Function 'controlling' outputs the array of indices of boxes required for extraction of characters.
if ~isempty(r) % If succesfully indices of desired boxes are achieved.
I={Iprops.Image}; % Cell array of 'Image' (one of the properties of regionprops)
noPlate=[];
flag=0;
for v=1:length(r)
N=I{1,r(v)}; % Extracting the binary image corresponding to the indices in 'r'.
subplot(2,5,v);imagesc(N);
title(v);
M=imresize(N,[42 24]);
imagetemp = num2str(v);
imagetemp = [imagetemp '.bmp'];
imwrite(M,imagetemp,'bmp');
if(length(r)==10 && (v==1 || v==2 || v==5 || v==6))
letter=readChar(N); % Reading the letter corresponding the binary image 'N'.
elseif(length(r)==10 && (v==3 || v==4 || v==7 || v==8 || v==9 || v==10))
letter=readNumber(N);
elseif(length(r)==9 && (v==1 || v==2 || v==4 || v==5))
letter=readChar(N); % Reading the letter corresponding the binary image 'N'.
elseif(length(r)==9 && (v==3 ||v==6 || v==7 || v==8 || v==9 ))
letter=readNumber(N);
else
letter=readLetter(N);
end
% while letter=='O' || letter=='0' % Since it wouldn't be easy to distinguish
% if v<=3 % between '0' and 'O' during the extraction of character
% letter='O'; % in binary image. Using the characteristic of plates in Karachi
% else % that starting three characters are alphabets, this code will
% letter='0'; % easily decide whether it is '0' or 'O'. The condition for 'if'
% end % just need to be changed if the code is to be implemented with some other
% break; % cities plates. The condition should be changed accordingly.
% end
noPlate=[noPlate letter]; % Appending every subsequent character in noPlate variable.
if v==1
CityCode=letter;
end
if v==2
CityCode=[CityCode letter]
end
if (isletter(letter) && v>2 && flag==0)
VehicleClass=letter
flag=1;
%vehicle class codes for delhi
if CityCode == 'DL'
if VehicleClass == 'S'
VehicleClass='TWL';
elseif VehicleClass == 'C'
VehicleClass='CAR';
elseif VehicleClass == 'E'
VehicleClass='ELE';
elseif VehicleClass == 'P'
VehicleClass='PAS';
elseif VehicleClass == 'R'
VehicleClass='RCK';
elseif VehicleClass == 'V'
VehicleClass='VAN';
elseif VehicleClass == 'T'
VehicleClass='TAX';
end
elseif CityCode == 'AP'
if VehicleClass == 'S'
VehicleClass='TWL'
elseif VehicleClass == 'B'
VehicleClass='CAR'
end
elseif CityCode == 'RJ'
if VehicleClass == 'S'
VehicleClass='TWL'
elseif VehicleClass == 'C'
VehicleClass='CAR'
end
elseif CityCode == 'HH'
if VehicleClass == 'S'
VehicleClass='TWL'
elseif VehicleClass == 'D'
VehicleClass='CAR'
end
else VehicleClass='CAR'
end
end
end
clc
fprintf('Number plate : %s\n',noPlate)
dbfile = fullfile(pwd,'SmartPark.db');
% conn = sqlite(dbfile,'create');
% createHistoryTable = ['create table History (VehicleNumber VARCHAR, VehicleClass VARCHAR, CheckInYear NUMERIC, CheckInMonth NUMERIC, CheckInDay NUMERIC, CheckInHour NUMERIC, CheckInMinute NUMERIC, CheckOutYear NUMERIC, CheckOutMonth NUMERIC, CheckOutDay NUMERIC, CheckOutHour NUMERIC, CheckOutMinute NUMERIC, Amount NUMERIC)'];
% exec(conn,createHistoryTable)
% createCurrentTable = ['create table Current (VehicleNumber VARCHAR, VehicleClass VARCHAR, CheckInYear NUMERIC, CheckInMonth NUMERIC, CheckInDay NUMERIC, CheckInHour NUMERIC, CheckInMinute NUMERIC, Amount NUMERIC)'];
% exec(conn,createCurrentTable)
% close(conn)
a = clock;
fix(a);
if (InOut=='In')
conn = sqlite(dbfile);
if(VehicleClass == 'TWL')
Amount = 10
elseif(VehicleClass=='CAR')
Amount = 20
else Amount = 20
end
VehicleData = {noPlate,VehicleClass,a(1),a(2),a(3),a(4),a(5),Amount} ;
insert(conn,'Current',{'VehicleNumber','VehicleClass','CheckInYear','CheckInMonth','CheckInDay','CheckInHour','CheckInMinute','Amount'},VehicleData)
close(conn)
elseif (InOut=='Ot')
conn = sqlite(dbfile);
VehicleDataAll = fetch(conn,'SELECT * FROM Current');
s = size(VehicleDataAll);
for i=s(1):-1:1
if(cell2mat(VehicleDataAll(i)) == noPlate)
VehicleDatax = VehicleDataAll(i);
for j=2:1:8
VehicleDatax = [VehicleDatax VehicleDataAll(i,j)];
end
break
end
if(i == 1)
%popup car not found in current table
msgbox('!!! Car not found in Current table !!!','Error')
end
end
VehicleData2 = cell2mat(VehicleDatax(3));
for j=4:1:8
VehicleData2 = [VehicleData2 cell2mat(VehicleDatax(j))];
end
VehicleData2;
Amount = VehicleData2(8-2);
t2 = [double(VehicleData2(3-2)),double(VehicleData2(4-2)),double(VehicleData2(5-2)),double(VehicleData2(6-2)),double(VehicleData2(7-2)),double(0)];
Amount = Amount+((etime(a,t2))/60)*1
VehicleData3 = {noPlate',VehicleClass,VehicleData2(3-2),VehicleData2(4-2),VehicleData2(5-2),VehicleData2(6-2),VehicleData2(7-2),a(1),a(2),a(3),a(4),a(5),Amount} ;
insert(conn,'History',{'VehicleNumber','VehicleClass','CheckOutYear','CheckInYear','CheckInMonth','CheckInDay','CheckInHour','CheckInMinute','CheckOutMonth','CheckOutDay','CheckOutHour','CheckOutMinute','Amount'},VehicleData3)
close(conn)
end
else
% If fail to extract the indexes in 'r' this line of error will be displayed.
[noPlate,VehicleClass,Amount] = numberPlateExtraction2(img,InOut)
noPlate = '0';
class = '0';
end
end