视频跟踪分割运动中的物体

博客主要提及视频跟踪分割技术,可用于分割运动中的物体,在信息技术领域有一定应用价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

视频跟踪分割运动中的物体


#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/background_segm.hpp"
#include <stdio.h>
#include <string>
using namespace cv;
static void help()
{
	printf("\n"
	"\n\n\t此程序演示了一种寻找轮廓,连接组件,清除背景的简单方法,实现跟踪分割视频中运动的物体\n"
	"\n\n\t程序运行开始后,便开始“学习背景”.\n"
	"\n\n\t可以通过【Space】空格键来切换是否打开“背景学习”技术。\n");
}
static void refineSegments(const Mat& img, Mat& mask, Mat& dst)
{
	int niters = 3;
	vector<vector<Point> > contours;
	vector<Vec4i> hierarchy;
	Mat temp;
	dilate(mask, temp, Mat(), Point(-1,-1), niters);
	erode(temp, temp, Mat(), Point(-1,-1), niters*2);
	dilate(temp, temp, Mat(), Point(-1,-1), niters);
	findContours( temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
	dst = Mat::zeros(img.size(), CV_8UC3);
	if( contours.size() == 0 )
		return;
	// iterate through all the top-level contours,
	// draw each connected component with its own random color
	// draw each connected component with its own random color
	int idx = 0, largestComp = 0;
	double maxArea = 0;
	for( ; idx >= 0; idx = hierarchy[idx][0] )
	{
		const vector<Point>& c = contours[idx];
		double area = fabs(contourArea(Mat(c)));
		if( area > maxArea )
		{
			maxArea = area;
			largestComp = idx;
		}
	}
	Scalar color( 123, 0, 255 );
	drawContours( dst, contours, largestComp, color, CV_FILLED, 8, hierarchy );
}
int main(int argc, char** argv)
{
	VideoCapture cap;
	bool update_bg_model = true;
	help();
	cap.open("E:\\图片\\bike.avi");//读取本地视频
	if( !cap.isOpened() )
	{
		printf("\nCan not open camera or video file\n");
		return -1;
	}
	Mat tmp_frame, bgmask, out_frame;
	cap >> tmp_frame;
	if(!tmp_frame.data)
	{
		printf("can not read data from the video source\n");
		return -1;
	}
	namedWindow("video", 1);
	namedWindow("segmented", 1);
	BackgroundSubtractorMOG bgsubtractor;
	bgsubtractor.set("noiseSigma", 10);
	for(;;)
	{
		cap >> tmp_frame;
		if( !tmp_frame.data )
			break;
		bgsubtractor(tmp_frame, bgmask, update_bg_model ? -1 : 0);
		refineSegments(tmp_frame, bgmask, out_frame);
		imshow("video", tmp_frame);
		imshow("segmented", out_frame);
		int keycode = waitKey(1);
		if( keycode == 27 )
			break;
		if( keycode == ' ' )
		{
			update_bg_model = !update_bg_model;
			printf("\n\t>‘背景学习’技术的状态 = %d\n",update_bg_model);
		}
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值