运动物体检测2
Record
案例:
1、使用椭圆对运动物体轮廓进行拟合(略);
2、使用滤波方法去除噪声(略);
3、根据轮廓正外接矩形大小对轮廓进行筛选(略);
4、绘制物体的运动轨迹(done);
5、简单的车辆计数(done);
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.Util;
using Emgu.CV.Structure;
using Emgu.CV.CvEnum;
using Emgu.Util;
using System.Drawing;
namespace lesson36
{
class Program
{
public static VectorOfPoint vcenterPoints = new VectorOfPoint(); //存储中心点集
public static int carNum = 0; //存储车的计数数目
static void Main(string[] args)
{
//运动物体质心轨迹绘制
//VideoCapture cap = new VideoCapture("man.avi");
//if(!cap.IsOpened)
//{
// Console.WriteLine("Open video failed..");
// return;
//}
//int count = 0;
//Mat bgImg = new Mat();
//Mat frame = new Mat();
//while(true)
//{
// cap.Read(frame);
// if(frame.IsEmpty)
// {
// Console.WriteLine("frame is empty..");
// break;
// }
// count++;
// if(count == 1)
// {
// bgImg = frame.Clone(); //背景差法
// }
// Mat result = MoveDetect(bgImg, frame);
// CvInvoke.Imshow("result", result);
// if (CvInvoke.WaitKey(50) == 27)
// {
// break;
// }
//}
///车辆计数演示
VideoCapture cap = new VideoCapture("car.avi");
if(!cap.IsOpened)
{
Console.WriteLine("Open the video failed..");
return;
}
Mat preframe = new Mat();
Mat frame = new Mat();
int count = 0;
while (true)
{
cap.Read(frame);
if(frame.IsEmpty)
{
Console.WriteLine("frame is empty..");
break;
}
count++;
if (count == 1)
{
preframe = frame.Clone();
}
Mat result = MoveDetect4(preframe, frame);
CvInvoke.Imshow("result", result);
preframe = frame.Clone(); //更新前一帧
if (CvInvoke.WaitKey(50) == 27)
break;
}
}
static Mat MoveDetect4(Mat preframe,Mat frame)
{
Mat result = frame.Clone();
Mat gray = new Mat();
Mat gray2 = new Mat();
CvInvoke.CvtColor(preframe, gray, ColorConversion.Bgr2Gray);
CvInvoke.CvtColor(frame, gray2, ColorConversion.Bgr2Gray);
Mat diff = new Mat();
CvInvoke.AbsDiff(gray2, gray, diff);
CvInvoke.Imshow("diff", diff);
//转换为二值图
CvInvoke.Threshold(diff, diff, 20, 255, ThresholdType.Binary);
//滤波
CvInvoke.MedianBlur(diff, diff, 17);
//膨胀
Mat element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(21, 21), new Point(-1, -1));
CvInvoke.Dilate(diff, diff, element, new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
CvInvoke.Imshow("dilate", diff);
//计算轮廓
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(diff, contours, null, RetrType.External, ChainApproxMethod.ChainApproxNone);
//对轮廓进行遍历,当轮廓经过特定位置时认为经过了检测线,计数变量+1
for (int i = 0; i < contours.Size; i++)
{
Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
if(rect.Width > 40 && rect.Width < 180 &&
rect.Height > 40 && rect.Height < 180) //对轮廓大小进行筛选
{
//绘制出包含车辆的矩形
CvInvoke.Rectangle(result, rect, new MCvScalar(0, 255, 0), 2);
//当车辆质心Y方向在这个范围内时,车辆计数加1
if((rect.Y + rect.Height/2 - 1) >=138 && (rect.Y + rect.Height / 2 - 1 ) <= 142)
{
carNum++;
//车辆经过时绘制显示线示意一下
CvInvoke.Line(result, new Point(0, 141), new Point(frame.Cols - 1, 142), new MCvScalar(0, 0, 255), 3);
CvInvoke.Circle(result, new Point((int)(rect.X + rect.Width / 2), 142), 5, new MCvScalar(0, 255, 255), -1);
}
}
}
//绘制扫描线
CvInvoke.Line(result, new Point(0, 141), new Point(frame.Cols - 1, 141), new MCvScalar(0, 0, 255), 1);
string strNum = string.Format("CarNUm={0}", carNum); //格式化字符串
CvInvoke.PutText(result, strNum, new Point(10, 25), FontFace.HersheyComplex, 1.2, new MCvScalar(0, 255, 0), 2);
return result;
}
static Mat MoveDetect(Mat bgImg,Mat fgImg)
{
Mat result = fgImg.Clone();
Mat gray = new Mat();
Mat gray2 = new Mat();
CvInvoke.CvtColor(bgImg, gray, ColorConversion.Bgr2Gray);
CvInvoke.CvtColor(fgImg, gray2, ColorConversion.Bgr2Gray);
//做差
Mat diff = new Mat();
CvInvoke.AbsDiff(gray2, gray, diff);
CvInvoke.Imshow("diff", diff);
//二值化
CvInvoke.Threshold(diff, diff, 45, 255, ThresholdType.Binary);
CvInvoke.Imshow("threshold", diff);
//滤波膨胀
CvInvoke.MedianBlur(diff, diff, 5);
Mat element = CvInvoke.GetStructuringElement(ElementShape.Rectangle, new Size(13, 13), new Point(-1, -1));
CvInvoke.Dilate(diff, diff, element, new Point(-1, -1), 1, BorderType.Default, new MCvScalar());
CvInvoke.Imshow("dilate", diff);
//计算轮廓
VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint();
CvInvoke.FindContours(diff, contours, null, RetrType.External, ChainApproxMethod.ChainApproxNone);
for(int i = 0; i < contours.Size; i++)
{
Rectangle rect = CvInvoke.BoundingRectangle(contours[i]);
if(rect.Width > 10 && rect.Height >10) //轮廓筛选
{
RotatedRect ellipse = CvInvoke.MinAreaRect(contours[i]);
//绘制最小外接椭圆
CvInvoke.Ellipse(result, new Point((int)ellipse.Center.X, (int)ellipse.Center.Y),
new Size((int)ellipse.Size.Width/2, (int)ellipse.Size.Height/2), ellipse.Angle,
0, 360, new MCvScalar(0, 255, 0), 2);
Point ptCenter = new Point((int)ellipse.Center.X, (int)ellipse.Center.Y);
Point[] pts = {
ptCenter };
vcenterPoints.Push(pts);
if(vcenterPoints.Size > 1) //点集中至少有两个点
{
for(int j = 0; j < vcenterPoints.Size-1; j++)
{
CvInvoke.Line(result, vcenterPoints[j], vcenterPoints[j + 1], new MCvScalar(255, 0, 0), 2);
}
}
CvInvoke.Circle(result, ptCenter, 3, new MCvScalar(0, 0, 255), -1);
}
}
return result;
}
}
}
效果
1、物体运动轨迹绘制:
2、简单的车辆计数: