-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlate_detection.cs
235 lines (192 loc) · 8.7 KB
/
Plate_detection.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using System.Drawing;
using Emgu.CV.CvEnum;
namespace ANPR_System
{
public class Plate_detection
{
/// <summary>
/// check whether the plate is tilted or not, if yes, rotate it.
/// </summary>
/// <param name="plate"></param>
/// <param name="plate_feature"></param>
/// <returns>rotated image</returns>
private static Image<Gray, byte> crop_and_rotated_plate(Image<Gray, byte> plate, RotatedRect plate_feature)
{
PointF[] boxes = CvInvoke.BoxPoints(plate_feature);
List<double> Xs = new List<double>();
List<double> Ys = new List<double>();
foreach (PointF box in boxes)
{
Xs.Add(box.X);
Ys.Add(box.Y);
}
double Xmax = Xs.Max();
double Ymax = Ys.Max();
double Xmin = Xs.Min();
double Ymin = Ys.Min();
Rectangle r = plate_feature.MinAreaRect();
double X = r.X;
double Y = r.Y;
double W = r.Width;
double H = r.Height;
double angle = plate_feature.Angle;
float X_center = (float)(Xmax + Xmin) / 2;
float Y_center = (float)(Ymax + Ymin) / 2;
Size patch_size = new Size((int)(Xmax-Xmin),(int)(Ymax-Ymin));
if (angle < (-45))
{
angle = angle + 90;
}
if (angle == 0)
{
return plate;
}
Mat map_matrix = new Mat(new Size(2, 3), DepthType.Cv64F, 1);
PointF center = new PointF(X_center, Y_center);
CvInvoke.GetRotationMatrix2D(center, angle, 1.0, map_matrix);
Image<Gray, byte> cropped = new Image<Gray, byte>(patch_size);
CvInvoke.GetRectSubPix(plate, patch_size, center, cropped);
Image<Gray, byte> warp_affine = cropped.WarpAffine(map_matrix, Inter.Linear, Warp.Default, BorderType.Default, new Gray(0));
int warp_H = 0;
int warp_W = 0;
if (warp_affine.Width > warp_affine.Height)
{
warp_H = warp_affine.Height;
warp_W = warp_affine.Width;
}
else
{
warp_H = warp_affine.Width;
warp_W = warp_affine.Height;
}
Image<Gray, byte> output = new Image<Gray, byte>(warp_W, warp_H);
CvInvoke.GetRectSubPix(warp_affine, new Size(warp_W, warp_H), center, output);
//CvInvoke.Imshow("warpaffine", warp_affine);
//CvInvoke.Imshow("GetRectSubPix", output);
//CvInvoke.WaitKey();
//CvInvoke.Imshow("plate", output);
//CvInvoke.WaitKey();
return warp_affine;
//return plate;
}
/// <summary>
/// clean the input suspected plate image and decide if that is a possible plate or not by checking ratio condition.
/// </summary>
/// <param name="suspected_plate"></param>
/// <param name="type"></param>
/// <returns>a tupple that contains:
/// 1: image of the plate,
/// 2: the (x,y,w,h) of the actual plate in the image
/// </returns>
public static Tuple<Image<Gray, byte>, Rectangle> clean_plate(Image<Gray, byte> suspected_plate, Plate_feature.type_of_plate type)
{
//Image<Gray, byte> threshold_plate = Image_utils.adaptive_threshold(suspected_plate);
Image<Gray, byte> threshold_plate = suspected_plate.ThresholdBinary(new Gray(100), new Gray(255));
VectorOfVectorOfPoint contours = Image_utils.find_all_contours(threshold_plate);
Rectangle r = new Rectangle();
Tuple<Image<Gray, byte>, Rectangle> output = new Tuple<Image<Gray, byte>, Rectangle>(suspected_plate, r);
if (contours != null)
{
double max_area = 0;
VectorOfPoint max_contour = new VectorOfPoint();
for (int i = 0; i < contours.Size; i++)
{
double temp = CvInvoke.ContourArea(contours[i]);
if (temp > max_area)
{
max_area = temp;
max_contour = contours[i];
}
}
VectorOfVectorOfPoint c = new VectorOfVectorOfPoint(max_contour);
CvInvoke.DrawContours(threshold_plate, c, 0, new MCvScalar(0), 2);
r = CvInvoke.BoundingRectangle(max_contour);
RotatedRect plate_feature = CvInvoke.MinAreaRect(max_contour);
Image<Gray, byte> rotated_plate = crop_and_rotated_plate(suspected_plate, plate_feature);
output = new Tuple<Image<Gray, byte>, Rectangle>(rotated_plate, r);
return output;
}
else
{
return output;
}
}
/// <summary>
/// check whether the possible plate after clean func is a plate or not by finding the characters in it.
/// </summary>
/// <param name="suspected_plate"></param>
/// <param name="type"></param>
/// <returns></returns>
public static Tuple<bool, List<Image<Gray, byte>>> check_plate_has_character(Image<Gray, byte> suspected_plate, Plate_feature.type_of_plate type)
{
List<Image<Gray, byte>> characters_found_on_plate = Plate_segmentation.segment_characters_from_plate(suspected_plate);
Tuple<bool, List<Image<Gray, byte>>> output = new Tuple<bool, List<Image<Gray, byte>>>(false, characters_found_on_plate);
if (characters_found_on_plate.Count > 5)
{
output = new Tuple<bool, List<Image<Gray, byte>>>(true, characters_found_on_plate);
return output;
}
else
{
return output;
}
}
/// <summary>
///
/// </summary>
/// <param name="input_img">original image</param>
/// <param name="type"></param>
/// <returns></returns>
public static List<Image<Gray, byte>> find_all_possible_plates(Image<Gray, byte> input_img, Plate_feature.type_of_plate type)
{
Image<Gray, byte> blur = Image_utils.gaussian_blur(input_img);
Image<Gray, byte> sobelx = Image_utils.sobel_X(blur).Convert<Gray, byte>();
Image<Gray, byte> adaptive = Image_utils.adaptive_threshold(sobelx);
Image<Gray, byte> morp = Image_utils.morphology_ex(adaptive, Plate_feature.type_of_plate.RECT_PLATE);
VectorOfVectorOfPoint contours = Image_utils.find_all_contours(morp);
List<Image<Gray, byte>> approved_plates = new List<Image<Gray, byte>>();
for (int i = 0; i < contours.Size; i++)
{
double area = CvInvoke.ContourArea(contours[i]);
Rectangle r = CvInvoke.BoundingRectangle(contours[i]);
RotatedRect rr = CvInvoke.MinAreaRect(contours[i]);
Image<Gray, byte> suspected_plate = input_img.Copy(r);
Tuple<Image<Gray, byte>, Rectangle> after_clean_plate_tuple;
Tuple<bool, List<Image<Gray, byte>>> after_check_plate_has_characters_tuple;
if (!Plate_feature.ratio_check(type, area, r.Width, r.Height))
{
continue;
}
else
{
if (!Plate_feature.validate_rotation_and_ratio(type, rr))
{
continue;
}
else
{
after_clean_plate_tuple = clean_plate(suspected_plate, type);
after_check_plate_has_characters_tuple = check_plate_has_character(after_clean_plate_tuple.Item1, type);
if (!after_check_plate_has_characters_tuple.Item1)
{
continue;
}
else
{
approved_plates = after_check_plate_has_characters_tuple.Item2;
}
}
}
}
return approved_plates;
}
}
}