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 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 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 btnDetect_Click(object sender, EventArgs e) { double t1 = 0, t2 = 0, timeDiff = 0, fov = 5.0, speed = 0; bool hascar = false; valuein = double.Parse(Value_In.Text); valueout = double.Parse(Value_Out.Text); last_In = 0; last_Out = 0; EC24Image1.Load(files[0]); /*============================計算scaling ratio============================*/ float PictureBoxSizeRatio = (float)pbimg.Width / pbimg.Height; float ImageSizeRatio = (float)EC24Image1.Width / EC24Image1.Height; if (ImageSizeRatio > PictureBoxSizeRatio) { ScalingRatio = (float)pbimg.Width / EC24Image1.Width; } else { ScalingRatio = (float)pbimg.Height / EC24Image1.Height; } /*=========================================================================*/ for (int i = 0; i < listBox1.Items.Count; i++) { listBox1.SelectedIndex = i; listBox1.Refresh(); EC24Image1.Load(files[i]); EC24Image1.Draw(pbimg.CreateGraphics(), ScalingRatio); EBW8Image1.SetSize(EC24Image1); EasyImage.Oper(EArithmeticLogicOperation.Copy, new EBW8(0), EBW8Image1); EasyImage.Convert(EC24Image1, EBW8Image1); //轉灰階 EasyImage.ImageToLineSegment(EBW8Image1, In, 1000, 1079, 1750, 1079); //設定偵測線在最底部,判斷車子是否準備進來 EasyImage.ImageToLineSegment(EBW8Image1, Out, 1000, 0, 1750, 0); //設定偵測線在最頂部,判斷車子是否準備出去 if (CarIn(In) && !hascar) //假如沒有車子,且CarIn有偵測到車子情況下,視同進入 { previous = i; //記錄車子進來的圖片張數 //t1 = DateTime.Now.TimeOfDay.Seconds + DateTime.Now.TimeOfDay.Milliseconds / 1000.0; //記錄進來的時間 speed = 0; hascar = true; } else if (CarOut(Out) && hascar && speed == 0) //假如有車子,且CarOut有偵測到車子並尚未計算速度的情況下,視同出去 { current = i; //記錄車子出去的圖片張數 if (current - previous <= 10 || current - previous >= 100) //防呆機制,假如10張內或超過100張視同有問題 { //last_Out = 0; hascar = false; } else { Console.WriteLine("車子進來的第一張圖片:" + files[previous]); Console.WriteLine("車子出去的最後一張圖片:" + files[current]); t1 = Double.Parse(Path.GetFileNameWithoutExtension(files[previous])); //擷取進來的圖檔名當作進來的時間 t2 = Double.Parse(Path.GetFileNameWithoutExtension(files[current])); //擷取出去的圖檔名當作出去的時間 //t2 = DateTime.Now.TimeOfDay.Seconds + DateTime.Now.TimeOfDay.Milliseconds / 1000.0; //記錄出去的時間 Console.WriteLine("T1=" + t1.ToString()); Console.WriteLine("T2=" + t2.ToString()); //秒數做溢位處理 /* * if (t1 > t2) * timeDiff = (t2 + 60) - t1; * else */ timeDiff = t2 - t1; //出去的時間減去進來的時間 Console.WriteLine("timeDiff = " + timeDiff); speed = fov / (timeDiff / 4) * 3.6; //計算車子的速度,因為採用4倍慢速錄影所以時間要除以4,速度單位為km/hr speed = Math.Round(speed, 2, MidpointRounding.AwayFromZero); //將速度四捨五入 labelSpeed.Text = "Speed: " + speed.ToString(); Console.WriteLine("Speed: " + speed); labelSpeed.Update(); last_In = 0; hascar = false; } } } }