private void button1_Click(object sender, EventArgs e) { var dialog = new OpenFileDialog(); dialog.Filter = "视频文件|*.avi;*.rmvb;*.rm"; if (dialog.ShowDialog() == DialogResult.OK) { String fileName = dialog.FileName; //cameraCapture = CvInvoke.cvCreateFileCapture(file); try { cameraCapture = new Capture(fileName); //cameraCapture.FlipHorizontal = ! cameraCapture.F } catch (Exception ex) { MessageBox.Show(ex.Message); return; } } detector = new FGDetector<bgr>(FORGROUND_DETECTOR_TYPE.FGD); tracker = new BlobTrackerAuto<bgr>(); Application.Idle += ProcessFrame; } void ProcessFrame(object sender, EventArgs e) { using (MemStorage stor = new MemStorage()) //得到视频文件中的一帧 { Image<bgr,> frame = cameraCapture.QueryFrame(); if (frame != null) { frame._SmoothGaussian(3); //filter out noises:对于当前的图像进行高斯平滑处理过滤噪声 #region use the BG/FG detector to find the forground mask detector.Update(frame);//更新当前图像 Image<gray,> forgroundMask = detector.ForegroundMask;//得到前景图像 #endregion try { //tracker.Process(frame, forgroundMask);//处理图像产生一种色调掩模,调用这句话会出现内存不足异常 //tracker.Process(frame); tracker.Process(cameraCapture.QuerySmallFrame().PyrUp());//只传入一半的图片进行识别和跟踪 } catch (Exception ex) { MessageBox.Show(ex.Message); } foreach (MCvBlob blob in tracker) { frame.Draw((Rectangle)blob, new Bgr(255.0, 255.0, 255.0), 2); //frame.Draw(blob.ID.ToString(), ref _font, Point.Round(blob.Center), new Bgr(255.0, 255.0, 255.0)); vehicleStatTextBox.Text = blob.ID.ToString();//统计车辆数目 } imageBox1.Image = frame; //tracker.Dispose(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)