-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
306 lines (241 loc) · 10.3 KB
/
Form1.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.Util;
using Emgu.CV;
using Emgu.CV.Structure;
using System.Drawing.Imaging;
namespace ID_Card_segmentation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Image<Gray, byte> srcImg;
string path;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if(ofd.ShowDialog()==DialogResult.OK)
{
path = ofd.FileName;
}
else
{
return;
}
srcImg = new Image<Gray, byte>(path);
Image<Gray, byte> src_cpy = srcImg.Copy();
//pictureBoxOri.Image = srcImg.Bitmap;
Image<Gray, byte> adap = preprocess(srcImg);
Image<Gray, byte> morp = morphologyEx(adap);
//pictureBoxMorp.Image = morp.Bitmap;
Image<Gray, byte> copy = morp.Copy();
int w = copy.Width;
int h = copy.Height;
if(w<h)
{
MessageBox.Show("Only accept image with width > height");
return;
}
//X axis
int[] hist_x = new int[w];
for (int i = 0; i < w; i++)
{
double sum = 0;
for (int j = 0; j < h; j++)
{
sum = sum + copy[j, i].Intensity;
}
hist_x[i] = (int)sum;
}
int max_x = hist_x.Max();
int max_x_index = hist_x.ToList().IndexOf(max_x);
int thresh_val_x = 80000;
List<int> list_x = hist_x.ToList<int>();
//lọc các cột lấy phần có tổng giá trị pixel > ngưỡng (chứa 2 vân tay)
int[] pack_x = list_x.Select((v, i) => new { v, i }).Where(x => x.v > thresh_val_x).Select(x => x.i).ToArray(); //lấy index chứ k lấy value
List<List<int>> arr_good_x = new List<List<int>>();//chứa tất cả các cột > ngưỡng
List<int> g_x = new List<int>();//list tạm thời dùng trong vòng for ở dưới
for (int i = 0; i < pack_x.Length-1; i++)
{
if ((pack_x[i+1]-pack_x[i])==1)//nếu liền kề thì để vào 1 list
{
g_x.Add(pack_x[i + 1]);
}
else//tách đc hết các pixel liền kề vào 1 list
{
arr_good_x.Add(g_x);
g_x = new List<int>();
}
if(i == (pack_x.Length-2))//trong trường hợp pixel liền kề đến cuối mảng
{
arr_good_x.Add(g_x);
}
}
List<int> slice_x = new List<int>();
foreach (List<int> arr in arr_good_x)
{
if(arr.Contains(max_x_index)) //list nào có chứa max_index thì chứa vân tay
{
slice_x = arr;
}
}
int start_x = slice_x.First();
int stop_x = slice_x.Last();
Rectangle roi = new Rectangle(start_x, 0, stop_x - start_x, h);
Image<Gray, byte> crop_x = new Image<Gray, byte>(w, h);
Image<Gray, byte> small = morp.Copy(roi);
crop_x.ROI = roi;
CvInvoke.cvCopy(small, crop_x, IntPtr.Zero); // xóa phần ko nằm trong start_x->start_y
crop_x.ROI = Rectangle.Empty;
//pictureBoxMorp.Image = crop_x.Bitmap;
//Y axis
int[] hist_y = new int[h];
for (int i = 0; i < h; i++)
{
double sum = 0;
for (int j = 0; j < w; j++)
{
sum = sum + crop_x[i, j].Intensity;
}
hist_y[i] = (int)sum;
}
int max_y = hist_y.Max();
//int max_y_index = hist_y.ToList().IndexOf(max_y);
int thresh_val_y = 20000;
List<int> list_y = hist_y.ToList<int>();
int[] pack_y = list_y.Select((v, i) => new { v, i }).Where(y => y.v > thresh_val_y).Select(y => y.i).ToArray();
List<List<int>> arr_good_y = new List<List<int>>();
List<int> g_y = new List<int>();
for (int i = 0; i < pack_y.Length - 1; i++)
{
if ((pack_y[i + 1] - pack_y[i]) == 1)
{
g_y.Add(pack_y[i + 1]);
}
else
{
arr_good_y.Add(g_y);
g_y = new List<int>();
}
if (i == (pack_y.Length - 2))
{
arr_good_y.Add(g_y);
}
}
List<List<int>> slice_y = new List<List<int>>();
List<int> del_y = new List<int>();
foreach (List<int> arr in arr_good_y)
{
if (arr.Count > 100 && !(arr.Contains(0)||arr.Contains(h-1)))//điều kiện lấy 2 vân tay độ dài > 100pixel + ko phải phần gần mép của ảnh
{
slice_y.Add(arr);
}
else if (arr.Contains(1)||arr.Contains(h-1)) //lấy để xóa (hiển thị ra cho vui)
{
del_y = arr;
}
}
Rectangle roi2 = new Rectangle(0, del_y.First(), w, del_y.Last()-del_y.First());
Image<Gray, byte> crop_y = crop_x.Copy();
Image<Gray, byte> small2 = new Image<Gray, byte>(w, del_y.Last() - del_y.First());
crop_y.ROI = roi2;
CvInvoke.cvCopy(small2, crop_y, IntPtr.Zero); // xóa phần gần mép ảnh
crop_y.ROI = Rectangle.Empty;
// get boxes
int[] start_y = new int[2];
int[] stop_y = new int[2];
List<Rectangle> box = new List<Rectangle>();
for (int i = 0 ; i < slice_y.Count ; i++)
{
start_y[i] = slice_y[i].First();
stop_y[i] = slice_y[i].Last();
Rectangle rect = new Rectangle(start_x,start_y[i],stop_x-start_x,stop_y[i]-start_y[i]);
box.Add(rect);
CvInvoke.Rectangle(srcImg, rect, new MCvScalar(0), 2);
}
// copy to 416x416 format
List<Image<Gray, byte>> fingers = new List<Image<Gray,byte>>();
for (int i = 0; i < 2; i++)
{
//Image<Gray, byte> template = new Image<Gray, byte>(416, 416, new Gray(255));
Image<Gray, byte> finger = src_cpy.Copy(box[i]);
//Rectangle rec = new Rectangle();
//if (finger.Width < 416 && finger.Height < 416)
//{
// rec.X = (int)(416 - finger.Width) / 2;
// rec.Y = (int)(416 - finger.Height) / 2;
// rec.Width = finger.Width;
// rec.Height = finger.Height;
// template.ROI = rec;
// finger.CopyTo(template);
// template.ROI = Rectangle.Empty;
//}
fingers.Add(finger);
}
//rotate finger template
if (del_y.Contains(h - 1))
{
fingers[0] = fingers[0].Rotate(90, new Gray(255));
fingers[1] = fingers[1].Rotate(90, new Gray(255));
}
else if (del_y.Contains(1))
{
fingers[0] = fingers[0].Rotate(270, new Gray(255));
fingers[1] = fingers[1].Rotate(270, new Gray(255));
}
//display
pictureBoxFin1.Image = fingers[0].Bitmap;
pictureBoxFin2.Image = fingers[1].Bitmap;
pictureBoxOri.Image = srcImg.Bitmap;
pictureBoxMorp.Image = crop_y.Bitmap;
button2.Enabled = true;
}
private Image<Gray, byte> preprocess(Image<Gray, byte> input)
{
Image<Gray, byte> copy = input.Copy();
CvInvoke.Blur(copy, copy, new Size(5, 5), new Point(-1, -1));
CvInvoke.AdaptiveThreshold(copy, copy, 255, Emgu.CV.CvEnum.AdaptiveThresholdType.GaussianC, Emgu.CV.CvEnum.ThresholdType.BinaryInv, 11, 2);
return copy;
}
private Image<Gray, byte> morphologyEx(Image<Gray, byte> input)
{
Image<Gray, byte> copy = input.Copy();
Mat kernel = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new Size(3, 3), new Point(-1, -1));
CvInvoke.Erode(copy, copy, kernel, new Point(-1, -1), 1, Emgu.CV.CvEnum.BorderType.Default, new MCvScalar(0));
CvInvoke.Dilate(copy, copy, kernel, new Point(-1, -1), 8, Emgu.CV.CvEnum.BorderType.Default, new MCvScalar(0));
CvInvoke.Erode(copy, copy, kernel, new Point(-1, -1), 7, Emgu.CV.CvEnum.BorderType.Default, new MCvScalar(0));
return copy;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
//sfd.Filter = "Bitmap Image (.bmp)|*.bmp";
if(sfd.ShowDialog()==DialogResult.OK)
{
try
{
pictureBoxFin1.Image.Save(sfd.FileName + "_left.bmp" , ImageFormat.Bmp);
pictureBoxFin2.Image.Save(sfd.FileName + "_right.bmp" , ImageFormat.Bmp);
MessageBox.Show("Successfully saved the images");
}
catch
{
MessageBox.Show("Error saving images! Try again!");
}
}
}
}
}