-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlicense_plate.m
191 lines (159 loc) · 4.55 KB
/
license_plate.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
function license_plate()
close all;
clear all;
%%Image preprocessing
img = imread('D:\github_ws\test thesis\33.jpg');
imgray = rgb2gray(img);
imgray=imresize(imgray, [480 640]);
figure, imshow(imgray);
%hold on;
nonoise_img = medfilt2(imgray, [3 3]);
figure, imshow(nonoise_img);
filtered = edge(nonoise_img,'canny');
figure, imshow(filtered);
L = bwlabeln(filtered, 8);
S = regionprops(L, 'Area');
BW = ismember(L,find([S.Area]>=8));
figure, imshow(BW);
H3 = imfill(BW,'holes');
figure, imshow(H3);
im = bwareaopen(H3, 1200);
figure, imshow(im);
%%Below steps are to find location of number plate
Iprops=regionprops(im,'BoundingBox','Area', 'Image');
area = Iprops.Area;
count = numel(Iprops);
maxa= area;
boundingBox = Iprops.BoundingBox;
for i=1:count
if maxa<Iprops(i).Area
maxa=Iprops(i).Area;
boundingBox=Iprops(i).BoundingBox;
end
%rectangle('position',Iprops(i).BoundingBox,'Edgecolor','r');
%figure, imshow(Iprops(i).Image);
end
%%crop the number plate area
im = imcrop(imgray, boundingBox);
figure, imshow(im);
im = imresize(im, [150 350]);
[counts,x] = imhist(im,16);
T = otsuthresh(counts);
im = imbinarize(im, T);
im = ~im;
%%Finding the rectangle exactly outside of text
mn=10000000; idx=1; idy=1;
H = 110; W = 320;
for i=1:150-H
for j = 1:350-W
temp = sum(im(i:i+H, j))+sum(im(i, j:j+W))+sum(im(i+H, j:j+W))+sum(im(i:i+H, j+W));
if(temp<=mn)
mn = temp;
idx = i; idy = j;
end
end
end
figure, imshow(im);
hold on;
%plot(310, 1:150, 'r.');
plot(idy, idx:idx+H, 'r.');
plot(idy:idy+W, idx, 'r.');
plot(idy+W, idx:idx+H, 'r.');
plot(idy:idy+W, idx+H, 'r.');
%%Removing small object
text = imcrop(im, [idy idx W H]);
text = bwareafilt(text, 9);
figure, imshow(text);
hold on;
%%Finding separation Line
mn=10000000; dis=10000000; idx1=1;
rowsum = sum(text, 2);
for i=1:length(rowsum)
if rowsum(i)<=mn && abs(75-i)<=dis
mn = rowsum(i); dis = abs(75-i);
idx1 = i;
end
end
plot(1:350, idx1, 'r.');
%Cropping First Line and second Line
firstLine = text(1:idx1, :);
secondLine = text(idx1+1:end, :);
firstLine = cut_border(firstLine);
secondLine = cut_border(secondLine);
firstLine = [false(size(firstLine, 1), 1) firstLine false(size(firstLine, 1), 1)];
secondLine = [false(size(secondLine, 1), 1) secondLine false(size(secondLine, 1), 1)];
%firstLine = bwareafilt(firstLine, 3);
%secondLine = bwareafilt(secondLine, 6);
figure, imshow(firstLine);
figure, imshow(secondLine);
%%FirstLine
colsum = sum(firstLine, 1);
startIdx = -1;
line1='';
line2='';
cnt = 0;
for i=1:length(colsum)
if colsum(i)>0 && startIdx == -1
startIdx = i;
elseif colsum(i)==0 && startIdx~=-1
%figure, imshow(firstLine(:, startIdx:i-1));
cnt = cnt+1;
if cnt==2
line1 = [line1 ' Metro'];
startIdx = -1;
else
char = cut_border(firstLine(:, startIdx:i-1));
figure, imshow(char);
line1 = [line1 ' ' character(char, 1)];
startIdx = -1;
end
end
end
%%SecondLine
colsum = sum(secondLine, 1);
startIdx = -1;
for i=1:length(colsum)
if colsum(i)>0 && startIdx == -1
startIdx = i;
elseif colsum(i)==0 && startIdx~=-1
%figure, imshow(secondLine(:, startIdx:i-1 ));
char = cut_border(secondLine(:, startIdx:i-1));
line2 = [line2 character(char, 2)];
startIdx = -1;
end
end
disp(line1);
disp(line2);
end
function mat = cut_border(img)
mat = [];
rowsum = sum(img, 2);
rlow = 0; rhigh = 0;
for i=1:length(rowsum)
if rowsum(i)~=0
rlow = i;
break;
end
end
for i=length(rowsum):-1:1
if rowsum(i)~=0
rhigh = i;
break;
end
end
colsum = sum(img, 1);
clow = 0; chigh = 0;
for i=1:length(colsum)
if colsum(i)~=0
clow = i;
break;
end
end
for i=length(colsum):-1:1
if colsum(i)~=0
chigh = i;
break;
end
end
mat = img(rlow:rhigh, clow:chigh);
end