using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.VideoSurveillance;
using System.Diagnostics;
using System.Threading;
namespace TrafficStatistics
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private static MCvFont _font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0);//定義顯示的字體
private static Capture cameraCapture;
private static BlobTrackerAuto<Bgr> tracker;//定義一個塊狀的汽車跟蹤器
private static FGDetector<Bgr> detector;//定義一個背景建模檢測器
private object lockObject = new object(); //用於鎖定的對象
private Stopwatch sw = new Stopwatch(); //計時器
// Image<Bgr, Byte> frame;//得到視頻文件中的一幀
// bool stop = false;
/// <summary>
/// 打開視頻文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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, Byte> 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, Byte> 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();
}
}
}
}
}
視頻監控中遇到的內存分配失敗問題
這幾天在做車流量統計,用的是Emgu,查看Emgu安裝文件下的Examples例子,經過修改後,第一次選擇視頻進行車輛統計的時候沒有問題,但是不關閉窗體第二次選擇視頻進行統計的時候會出現內存分配失敗的問題。搜了很多方法還是沒有解決。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.