-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
706 lines (554 loc) · 27.4 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Euresys.Open_eVision_2_12;
using Excel = Microsoft.Office.Interop.Excel;
namespace Final_Project_Vehicle_Speed_Measurement
{
public partial class Form1 : Form
{
Form2 settings = new Form2();
Form3 videoInfo = new Form3();
EImageC24 OriginalImg1 = new EImageC24(); //eVision的彩色圖像物件
EImageC24 OriginalImg2 = new EImageC24(); // EImageC24 instance
EImageC24 Background = new EImageC24(); //eVision的灰階圖像物件
EImageBW8 GrayImg1 = new EImageBW8(); //eVision的灰階圖像物件
EImageBW8 GrayImg2 = new EImageBW8(); //eVision的灰階圖像物件
EImageBW8 BackgroundGray = new EImageBW8(); //eVision的灰階圖像物件
EImageBW8 GrayImg3 = new EImageBW8(); //eVision的灰階圖像物件
EColorLookup EColorLookup1 = new EColorLookup(); // EColorLookup instance
EBW8Vector In = new EBW8Vector(); // EBW8Vector instance
EBW8Vector Out = new EBW8Vector(); // EBW8Vector instance
EMatcher EMatcher1 = new EMatcher();
EROIBW8 EBW8Image1Roi1 = new EROIBW8();
ECodedImage2 codedImage1 = new ECodedImage2(); // ECodedImage2 instance
EImageEncoder codedImage1Encoder = new EImageEncoder(); // EImageEncoder instance
EObjectSelection codedImage1ObjectSelection = new EObjectSelection(); // EObjectSelection instance
BitmapData bmpData = null;
Bitmap bitmap = null;
Capture video;
public bool play = false;
bool firstSet = false;
double fps, totalframe, videotime, framecount;
string[] files;
float ScalingRatio = 0; //Picturebox與原始影像大小的縮放比例
bool selecting = false;
static Excel.Application Excel_APP1;
Excel.Workbook Excel_WB1;
Excel.Worksheet Excel_WS1;
public Form1()
{
InitializeComponent();
}
private void folderToolStripMenuItem_Click(object sender, EventArgs e)
{
folderBrowserDialog1.SelectedPath = "C:\\Users\\kashi\\Desktop\\畢業專題\\測試資料\\";
FileListBox.Items.Clear();
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
files = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.jpg").OrderBy(x => x.Length).ThenBy(x => x).ToArray();
foreach (string f in files)
{
FileListBox.Items.Add(Path.GetFileName(f));
}
}
}
private void excelToolStripMenuItem_Click(object sender, EventArgs e)
{
Excel_APP1 = new Excel.Application();
Excel_WB1 = Excel_APP1.Workbooks.Add();
Excel_WS1 = new Excel.Worksheet();
saveFileDialog1.Filter = "Excel|*.xlsx";
saveFileDialog1.Title = "Save a Excel";
Excel_WS1 = Excel_WB1.Worksheets[1];
Excel_WS1.Name = "Data";
Excel_APP1.Cells[1, 1] = "圖片(.jpg)";
Excel_APP1.Cells[1, 2] = "Profile下";
Excel_APP1.Cells[1, 3] = "Profile上";
OriginalImg1.Load(files[0]);
/*============================計算scaling ratio============================*/
float PictureBoxSizeRatio = (float)pbImg1.Width / pbImg1.Height;
float ImageSizeRatio = (float)OriginalImg1.Width / OriginalImg1.Height;
if (ImageSizeRatio > PictureBoxSizeRatio)
ScalingRatio = (float)pbImg1.Width / OriginalImg1.Width;
else
ScalingRatio = (float)pbImg1.Height / OriginalImg1.Height;
/*=========================================================================*/
for (int i = 0; i < FileListBox.Items.Count; i++)
{
FileListBox.SelectedIndex = i;
FileListBox.Refresh();
OriginalImg1.Load(files[i]);
OriginalImg1.Draw(pbImg1.CreateGraphics(), ScalingRatio);
//EC24Image2.SetSize(EC24Image1);
//EasyImage.Oper(EArithmeticLogicOperation.Copy, new EC24(0, 0, 0), EC24Image2);
//EC24Image1.ColorSystem = EColorSystem.Rgb;
//EColorLookup1.ConvertFromRgb(EColorSystem.Yiq);
//EColorLookup1.Transform(EC24Image1, EC24Image2);
//EC24Image2.Draw(pbImg2.CreateGraphics(), ScalingRatio);
GrayImg1.SetSize(OriginalImg1);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), GrayImg1);
EasyImage.Convert(OriginalImg1, GrayImg1); //轉灰階
//EasyImage.Oper(EArithmeticLogicOperation.Subtract, GrayImg1, BackgroundGray, GrayImg1);
//EasyImage.Threshold(GrayImg1, GrayImg1, 56);
//EasyImage.OpenBox(GrayImg1, GrayImg1, settings.set_value_3());
GrayImg1.Draw(pbImg2.CreateGraphics(), ScalingRatio);
EasyImage.ImageToLineSegment(GrayImg1, In, 1485, 700, 1683, 700); //設定車子進入的偵測線,判斷車子是否準備進來
EasyImage.ImageToLineSegment(GrayImg1, Out, 1485, 400, 1683, 400); //設定車子出去的偵測線,判斷車子是否準備出去
Excel_APP1.Cells[2 + i, 1] = Path.GetFileNameWithoutExtension(files[i]);
Excel_APP1.Cells[2 + i, 2] = getProfileValueSum(In);
Excel_APP1.Cells[2 + i, 3] = getProfileValueSum(Out);
//Console.WriteLine(files[i]);
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != "")
Excel_WB1.SaveAs(saveFileDialog1.FileName);
Excel_WS1 = null;
Excel_WB1.Close();
Excel_WB1 = null;
Excel_APP1.Quit();
Excel_APP1 = null;
}
private void vehicleToolStripMenuItem_Click(object sender, EventArgs e)
{
float PictureBoxSizeRatio, ImageSizeRatio;
codedImage1ObjectSelection.FeretAngle = 0.00f;
codedImage1Encoder.GrayscaleSingleThresholdSegmenter.WhiteLayerEncoded = true;
codedImage1Encoder.GrayscaleSingleThresholdSegmenter.BlackLayerEncoded = false;
codedImage1Encoder.SegmentationMethod = ESegmentationMethod.GrayscaleSingleThreshold;
codedImage1Encoder.GrayscaleSingleThresholdSegmenter.Mode = EGrayscaleSingleThreshold.MinResidue;
OriginalImg1.Load(files[0]);
/*============================計算scaling ratio============================*/
PictureBoxSizeRatio = (float)pbImg1.Width / pbImg1.Height;
ImageSizeRatio = (float)OriginalImg1.Width / OriginalImg1.Height;
if (ImageSizeRatio > PictureBoxSizeRatio)
ScalingRatio = (float)pbImg1.Width / OriginalImg1.Width;
else
ScalingRatio = (float)pbImg1.Height / OriginalImg1.Height;
/*=========================================================================*/
for (int i = 0; i < FileListBox.Items.Count; i++)
{
FileListBox.SelectedIndex = i;
FileListBox.Refresh();
OriginalImg1.Load(files[i]);
OriginalImg1.Draw(pbImg1.CreateGraphics(), ScalingRatio);
GrayImg1.SetSize(OriginalImg1);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), GrayImg1);
EasyImage.Convert(OriginalImg1, GrayImg1); //轉灰階
EasyImage.Median(BackgroundGray, BackgroundGray);
EasyImage.Median(GrayImg1, GrayImg1);
EasyImage.Oper(EArithmeticLogicOperation.Subtract, GrayImg1, BackgroundGray, GrayImg1);
EasyImage.Threshold(GrayImg1, GrayImg1, unchecked((uint)EThresholdMode.MinResidue));
EasyImage.ErodeBox(GrayImg1, GrayImg1, 1); //侵蝕
EasyImage.CloseBox(GrayImg1, GrayImg1, 10); //閉合
codedImage1ObjectSelection.FeretAngle = 0.00f;
codedImage1Encoder.Encode(GrayImg1, codedImage1);
codedImage1ObjectSelection.Clear();
codedImage1ObjectSelection.AddObjects(codedImage1);
codedImage1ObjectSelection.AttachedImage = GrayImg1;
codedImage1ObjectSelection.RemoveUsingUnsignedIntegerFeature(EFeature.RunCount, 1000, ESingleThresholdMode.Less); //移除RunCount小於1000的物件
if(codedImage1ObjectSelection.ElementCount > 0)
Console.WriteLine("(" + codedImage1ObjectSelection.GetElement(0).BoundingBoxCenterX + ", " + codedImage1ObjectSelection.GetElement(0).BoundingBoxCenterY + ")");
codedImage1.DrawFeature(pbImg1.CreateGraphics(), EDrawableFeature.BoundingBox, codedImage1ObjectSelection, ScalingRatio); // 把車的框框畫出來
GrayImg1.Draw(pbImg2.CreateGraphics(), ScalingRatio);
Console.WriteLine(files[i]);
}
}
private void FileListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (selecting)
{
OriginalImg1.Load(files[0]);
//============================計算scaling ratio============================
float PictureBoxSizeRatio = (float)pbImg1.Width / pbImg1.Height;
float ImageSizeRatio = (float)OriginalImg1.Width / OriginalImg1.Height;
if (ImageSizeRatio > PictureBoxSizeRatio)
ScalingRatio = (float)pbImg1.Width / OriginalImg1.Width;
else
ScalingRatio = (float)pbImg1.Height / OriginalImg1.Height;
//=========================================================================
OriginalImg1.Load(files[FileListBox.SelectedIndex]);
OriginalImg1.Draw(pbImg1.CreateGraphics(), ScalingRatio);
GrayImg1.SetSize(OriginalImg1);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), GrayImg1);
EasyImage.Convert(OriginalImg1, GrayImg1); //轉灰階
EasyImage.Median(BackgroundGray, BackgroundGray);
EasyImage.Median(GrayImg1, GrayImg1);
EasyImage.Oper(EArithmeticLogicOperation.Subtract, GrayImg1, BackgroundGray, GrayImg1);
EasyImage.Threshold(GrayImg1, GrayImg1, unchecked((uint)EThresholdMode.MinResidue));
EasyImage.ErodeBox(GrayImg1, GrayImg1, 1); //侵蝕
EasyImage.CloseBox(GrayImg1, GrayImg1, 10); //閉合
GrayImg1.Draw(pbImg2.CreateGraphics(), ScalingRatio);
Console.WriteLine(files[FileListBox.SelectedIndex]);
}
}
private void excelToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void excelToolStripMenuItem1_Click_1(object sender, EventArgs e)
{
saveFileDialog1.Filter = "JPEG|*.jpg";
saveFileDialog1.FileName = System.IO.Path.GetFileNameWithoutExtension(files[FileListBox.SelectedIndex]);
saveFileDialog1.Title = "Save the Picture";
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != "")
GrayImg1.Save(saveFileDialog1.FileName, EImageFileType.Jpeg);
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
settings.Show();
}
private void grayToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < FileListBox.Items.Count; i++)
{
FileListBox.SelectedItem = i;
FileListBox.Refresh();
OriginalImg1.Load(files[i]);
GrayImg1.SetSize(OriginalImg1);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), GrayImg1);
EasyImage.Convert(OriginalImg1, GrayImg1);
string path = settings.set_path() + "\\" + Path.GetFileName(files[i]);
if (!Directory.Exists(settings.set_path()))
Directory.CreateDirectory(settings.set_path());
GrayImg1.SaveJpeg(path);
}
MessageBox.Show("灰階化轉換完成", "通知");
}
private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
{
}
private void yIQToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int i = 0; i < FileListBox.Items.Count; i++)
{
FileListBox.SelectedItem = i;
FileListBox.Refresh();
OriginalImg1.Load(files[i]);
OriginalImg2.SetSize(OriginalImg1);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EC24(0, 0, 0), OriginalImg2);
OriginalImg1.ColorSystem = EColorSystem.Rgb;
EColorLookup1.ConvertFromRgb(EColorSystem.Yiq);
EColorLookup1.Transform(OriginalImg1, OriginalImg2);
//EC24Image2.Draw(pbImg2.CreateGraphics(), ScalingRatio);
string path = settings.set_path() + "\\" + Path.GetFileName(files[i]);
if (!Directory.Exists(settings.set_path()))
Directory.CreateDirectory(settings.set_path());
OriginalImg2.SaveJpeg(path);
}
MessageBox.Show("YIQ轉換完成", "通知");
}
private void backgroundToolStripMenuItem_Click(object sender, EventArgs e)
{
float PictureBoxSizeRatio, ImageSizeRatio;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
Background.Load(openFileDialog1.FileName);
PictureBoxSizeRatio = (float)pbImg3.Width / pbImg3.Height;
ImageSizeRatio = (float)Background.Width / Background.Height;
if (ImageSizeRatio > PictureBoxSizeRatio)
ScalingRatio = (float)pbImg3.Width / Background.Width;
else
ScalingRatio = (float)pbImg3.Height / Background.Height;
//顯示影像於Picturebox
pbImg3.Refresh(); //先清除目前圖像
Background.Draw(pbImg3.CreateGraphics(), ScalingRatio); //再繪製上去
BackgroundGray.SetSize(Background);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), BackgroundGray);
EasyImage.Convert(Background, BackgroundGray); //轉灰階
}
}
private void selectionToolStripMenuItem_Click(object sender, EventArgs e)
{
if (selecting)
selecting = false;
else
selecting = true;
}
private void clearToolStripMenuItem_Click(object sender, EventArgs e)
{
codedImage1ObjectSelection.Clear();
pbImg1.Refresh();
}
private void videoToolStripMenuItem_Click(object sender, EventArgs e)
{
play = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
video = new Capture(openFileDialog1.FileName); //讀影片
Mat m = new Mat();
m = video.QueryFrame();
pbImg1.Image = m.Bitmap; //顯示該frame,當預覽圖
video.SetCaptureProperty(CapProp.PosFrames, 0);
totalframe = video.GetCaptureProperty(CapProp.FrameCount);
videoInfo.setValue(totalframe, 0, 0, 0);
videoInfo.Show();
}
}
private async void videoPlayToolStripMenuItem_Click(object sender, EventArgs e)
{
if (video == null) return;
if (!play)
{
play = true;
videoPlayToolStripMenuItem.Text = "VideoPause";
}
else
{
play = false;
videoPlayToolStripMenuItem.Text = "VideoPlay";
}
try
{
while (play)
{
Mat frame = new Mat();
if (frame == null) break;
pbImg1.Image = frame.Bitmap; //顯示frame
fps = video.GetCaptureProperty(CapProp.Fps); //抓影片的fps
videotime = Math.Round(video.GetCaptureProperty(CapProp.PosMsec) / 1000, 3, MidpointRounding.AwayFromZero); //抓影片時間
framecount = video.GetCaptureProperty(CapProp.PosFrames);
refBitmap(frame);
OriginalImg1 = BitmapToEImageC24(ref bitmap);
if (!firstSet)
{
GrayImg1.SetSize(OriginalImg1);
GrayImg3.SetSize(OriginalImg1);
BackgroundGray.SetSize(OriginalImg1);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), GrayImg1);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), GrayImg3);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), BackgroundGray);
EasyImage.Convert(OriginalImg1, GrayImg1); //轉灰階
EasyImage.Convert(OriginalImg1, BackgroundGray); //轉灰階
firstSet = true;
}
else
{
EasyImage.Convert(OriginalImg1, GrayImg1); //轉灰階
//EasyImage.Oper(EArithmeticLogicOperation.Subtract, GrayImg1, BackgroundGray, GrayImg3);
//double profilevalue_in = getProfileValueSum(In);
//double profilevalue_out = getProfileValueSum(Out);
//if (profilevalue_in < 2500 && profilevalue_out < 2500)
//{
// Console.WriteLine("Background:" + profilevalue_in);
// EasyImage.Convert(OriginalImg1, BackgroundGray); //轉灰階
//}
//else
//{
// Console.WriteLine("有車進入:" + profilevalue_in);
// EasyImage.Convert(OriginalImg1, BackgroundGray); //轉灰階
//}
}
EasyImage.ImageToLineSegment(GrayImg1, In, 968, 585, 1747, 585); //設定車子進入的偵測線,判斷車子是否準備進來
EasyImage.ImageToLineSegment(GrayImg1, Out, 968, 209, 1747, 209); //設定車子出去的偵測線,判斷車子是否準備出去
Console.WriteLine(getProfileValueSum(In));
ShowImage(GrayImg1, pbImg2);
//ShowImage(BackgroundGray, pbImg3);
bitmap.Dispose();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
videoInfo.setValue(totalframe, framecount, videotime, fps);
await Task.Delay(1000 / Convert.ToInt32(fps)); //延遲
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private EImageC24 BitmapToEImageC24(ref Bitmap bitmap)
{
EImageC24 EC24Image1 = null;
try
{
EC24Image1 = new EImageC24();
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
bmpData = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
EC24Image1.SetImagePtr(bitmap.Width, bitmap.Height, bmpData.Scan0);
bitmap.UnlockBits(bmpData);
}
catch (EException e) //EException為eVision的例外處理
{
Console.WriteLine(e.ToString());
}
return EC24Image1;
}
private void ShowImage(EImageC24 img, PictureBox pb)
{
try
{
Bitmap bmp;
bmp = new Bitmap(pb.Width, pb.Height);
float PictureBoxSizeRatio = (float)pb.Width / pb.Height;
float ImageSizeRatio = (float)img.Width / img.Height;
if (ImageSizeRatio > PictureBoxSizeRatio)
ScalingRatio = (float)pb.Width / img.Width;
else
ScalingRatio = (float)pb.Height / img.Height;
if(pb.InvokeRequired)
{
pb.Invoke(new MethodInvoker(delegate () {
img.Draw(Graphics.FromImage(bmp), ScalingRatio);
pb.Image = bmp;
}));
}
else
{
img.Draw(Graphics.FromImage(bmp), ScalingRatio);
pb.Image = bmp;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private async void excelToolStripMenuItem2_Click(object sender, EventArgs e)
{
Excel_APP1 = new Excel.Application();
Excel_WB1 = Excel_APP1.Workbooks.Add();
Excel_WS1 = new Excel.Worksheet();
saveFileDialog1.Filter = "Excel|*.xlsx";
saveFileDialog1.Title = "Save a Excel";
Excel_WS1 = Excel_WB1.Worksheets[1];
Excel_WS1.Name = "Data";
Excel_APP1.Cells[1, 1] = "圖片(.jpg)";
Excel_APP1.Cells[1, 2] = "Profile下";
Excel_APP1.Cells[1, 3] = "Profile上";
Excel_APP1.Cells[1, 4] = "時間";
try
{
while (framecount != totalframe)
{
Mat frame = new Mat();
frame = video.QueryFrame(); //擷取影片frame
if (frame == null) break;
pbImg1.Image = frame.Bitmap; //顯示frame
fps = video.GetCaptureProperty(CapProp.Fps); //抓影片的fps
videotime = video.GetCaptureProperty(CapProp.PosMsec) / 1000; //抓影片時間
framecount = video.GetCaptureProperty(CapProp.PosFrames);
Bitmap bitmap_source = (Bitmap)frame.Bitmap;
if (bitmap == null)
bitmap = (Bitmap)bitmap_source.Clone();
bitmap = bitmap_source;
if (bitmap == null)
return;
OriginalImg1 = BitmapToEImageC24(ref bitmap);
GrayImg1.SetSize(OriginalImg1);
EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), GrayImg1);
EasyImage.Convert(OriginalImg1, GrayImg1); //轉灰階
EasyImage.ImageToLineSegment(GrayImg1, In, 413, 104, 797, 104); //設定車子進入的偵測線,判斷車子是否準備進來
EasyImage.ImageToLineSegment(GrayImg1, Out, 325, 891, 776, 891); //設定車子出去的偵測線,判斷車子是否準備出去
ShowImage(GrayImg1, pbImg2);
//Console.WriteLine("In:" + getProfileValueSum(In) + " / " + "Out:" + getProfileValueSum(Out) + " / " + "Time:" + videotime);
Excel_APP1.Cells[framecount + 1, 1] = framecount.ToString();
Excel_APP1.Cells[framecount + 1, 2] = getProfileValueSum(In);
Excel_APP1.Cells[framecount + 1, 3] = getProfileValueSum(Out);
Excel_APP1.Cells[framecount + 1, 4] = videotime;
bitmap.Dispose();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
videoInfo.setValue(totalframe, framecount, videotime, fps);
await Task.Delay(1000 / Convert.ToInt32(fps)); //延遲
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName != "")
Excel_WB1.SaveAs(saveFileDialog1.FileName);
Excel_WS1 = null;
Excel_WB1.Close();
Excel_WB1 = null;
Excel_APP1.Quit();
Excel_APP1 = null;
}
private void ShowImage(EImageBW8 img, PictureBox pb)
{
try
{
Bitmap bmp;
bmp = new Bitmap(pb.Width, pb.Height);
float PictureBoxSizeRatio = (float)pb.Width / pb.Height;
float ImageSizeRatio = (float)img.Width / img.Height;
if (ImageSizeRatio > PictureBoxSizeRatio)
ScalingRatio = (float)pb.Width / img.Width;
else
ScalingRatio = (float)pb.Height / img.Height;
if (pb.InvokeRequired)
{
pb.Invoke(new MethodInvoker(delegate () {
img.Draw(Graphics.FromImage(bmp), ScalingRatio);
pb.Image = bmp;
}));
}
else
{
img.Draw(Graphics.FromImage(bmp), ScalingRatio);
pb.Image = bmp;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void exportToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void imageToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void pbImg1_MouseUp(object sender, MouseEventArgs e)
{
}
private void fileToolStripMenuItem1_Click(object sender, EventArgs e)
{
float PictureBoxSizeRatio, ImageSizeRatio;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
OriginalImg1.Load(openFileDialog1.FileName);
PictureBoxSizeRatio = (float)pbImg1.Width / pbImg1.Height;
ImageSizeRatio = (float)OriginalImg1.Width / OriginalImg1.Height;
if (ImageSizeRatio > PictureBoxSizeRatio)
ScalingRatio = (float)pbImg1.Width / OriginalImg1.Width;
else
ScalingRatio = (float)pbImg1.Height / OriginalImg1.Height;
//顯示影像於Picturebox
pbImg1.Refresh(); //先清除目前圖像
OriginalImg1.Draw(pbImg1.CreateGraphics(), ScalingRatio); //再繪製上去
}
}
private double getProfileValueSum(EBW8Vector eBW8) //Profile線上的值相加起來
{
double sum = 0;
for (int i = 0; i < eBW8.NumElements; i++)
{
sum += eBW8.GetElement(i).Value;
}
return sum;
}
public void refBitmap(Mat frame)
{
Bitmap bitmap_source = (Bitmap)frame.Bitmap;
if (bitmap == null)
bitmap = (Bitmap)bitmap_source.Clone();
bitmap = bitmap_source;
if (bitmap == null)
return;
}
}
}