OpenCV C++案例实战十一《基于背景减除---行人计数》

OpenCV C++案例实战十一《基于背景减除---行人计数》


前言

本文将使用OpenCV C++ 对视频中的人流量进行统计。

一、图像预处理

请添加图片描述
原图如图所示。本案例的需求是想要统计画面中的人流量。画面中走动的行人可以看作是前景,那么我们就需要将前景、背景分割出来。我们可以使用OpenCV提供的BackgroundSubtractorMOG2 高斯混合模型,将行人从画面中分割出来,然后提取轮廓就可以统计人流量了。

Ptr<BackgroundSubtractorMOG2>MOG = createBackgroundSubtractorMOG2();
MOG->apply(frame, mask);

请添加图片描述
使用上面两行代码就可以创建高斯混合背景提取器。传入原图,返回背景减除结果。如上图所示。接下来只需对上图进行一些简单操作,再提取轮廓就可以进行人流统计了。

threshold(mask, mask, 200, 255, THRESH_BINARY );

morphologyEx(mask, mask, MORPH_OPEN, kernel);

dilate(mask, mask, kernel1);

进行二值化、形态学等操作可以将行人作为一个独立个体分割出来。效果如图。
请添加图片描述

二、对象计数

1.轮廓提取

将上面的二值图像进行轮廓检测,然后统计有效轮廓就可以完成对象计数了。

	vector<vector<Point>>contours;
	vector<vector<Point>>EffectiveContours;	
	findContours(mask, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
	for (int i = 0; i < contours.size(); i++)
	{			
		double area = contourArea(contours[i]);
	
		if (area > 300)
		{		
			EffectiveContours.push_back(contours[i]);
		}
		
	}

2.效果显示

	char text[10];
	for (int i = 0; i < EffectiveContours.size(); i++)
	{
		RotatedRect rect = minAreaRect(EffectiveContours[i]);

		Rect box = rect.boundingRect();

		rectangle(frame, Rect(box.x, box.y, box.width, box.height), Scalar(0, 255, 0), 2);

		sprintf_s(text, "%s%d", "Current:", EffectiveContours.size());

		putText(frame, text, Point(10, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
	}

请添加图片描述最终效果如图所示。

三、源码

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main()
{

	VideoCapture capture;
	capture.open("1.avi");

	if (!capture.isOpened())
	{
		cout << "Can not open video source!" << endl;
		system("pause");
		return -1;
	}

	Ptr<BackgroundSubtractorMOG2>MOG = createBackgroundSubtractorMOG2();

	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 5));
	Mat kernel1 = getStructuringElement(MORPH_RECT, Size(7, 3));

	Mat frame, mask;
	while (capture.read(frame))
	{
		MOG->apply(frame, mask);

		threshold(mask, mask, 200, 255, THRESH_BINARY );

		morphologyEx(mask, mask, MORPH_OPEN, kernel);

		dilate(mask, mask, kernel1);

		vector<vector<Point>>contours;
		vector<vector<Point>>EffectiveContours;	
		findContours(mask, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
		for (int i = 0; i < contours.size(); i++)
		{			
			double area = contourArea(contours[i]);

			if (area > 300)
			{		
				EffectiveContours.push_back(contours[i]);
			}
			
		}

		char text[10];
		for (int i = 0; i < EffectiveContours.size(); i++)
		{
			RotatedRect rect = minAreaRect(EffectiveContours[i]);

			Rect box = rect.boundingRect();

			rectangle(frame, Rect(box.x, box.y, box.width, box.height), Scalar(0, 255, 0), 2);

			sprintf_s(text, "%s%d", "Current:", EffectiveContours.size());

			putText(frame, text, Point(10, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 255, 0), 2);
		}
		imshow("frame", frame);
		imshow("mask", mask);

		char key = waitKey(10);
		if (key == 27)
		{
			break;
		}
	}

	destroyAllWindows();
	capture.release();
	system("pause");
	return 0;
}

总结

本文使用OpenCV C++ 基于背景减除进行人流计数,关键步骤有以下几点。
1、使用BackgroundSubtractorMOG2 将前景从背景中分割出来。
2、将分割出来的前景进行轮廓提取,从而统计出人流量。

  • 6
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zero___Chen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值