day37:光流法目标跟踪

该博客介绍了两种视觉跟踪方法:Frameback多项式拓展算法和基于LK的稀疏光流法。在Frameback算法中,作者实现了光流的计算,并通过HSV色彩空间显示了运动物体的轨迹。而在LK稀疏光流法中,通过角点检测和LK光流算法实现物体跟踪,并用线条描绘了跟踪轨迹。这两种方法都是视频处理和计算机视觉中的关键技术,用于实时物体跟踪。

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

 

1.Frameback多项式拓展算法 

 

 

void visionagin:: Myframeback()
{
	VideoCapture capture("C:\\Users\\86176\\Downloads\\visionimage\\detect.mp4");
	if (!capture.isOpened())
	{
		cout << "open failed !" << endl;
	}
	Mat preframe, pregray;
	capture.read(preframe);
	cvtColor(preframe, pregray, COLOR_BGR2GRAY);
	while (true)
	{
		Mat nextframe, nextgry;
		if (!capture.read(nextframe))
		{
			break;
		}
		imshow("frame", nextframe);
		cvtColor(nextframe, nextgry, COLOR_BGR2GRAY);
		//建立光流结果矩阵
		Mat_<Point2f>flow;
		calcOpticalFlowFarneback(pregray, nextgry, flow, 0.5, 3, 15, 3, 5,1.2,0);
		//x,y方向上的速度
		Mat vx = Mat::zeros(preframe.size(), CV_32FC1);
		Mat vy = Mat::zeros(preframe.size(), CV_32FC1);
		//取flow x,y作为两方向上的速度
		for (int i = 0; i < flow.rows; ++i)
		{
			for (int j = 0; j < flow.cols; ++j)
			{
				Point2f temp = flow.at<Point2f>(i,j);
				vx.at<float>(i,j) = temp.x;
				vy.at<float>(i, j) = temp.y;
			}
		}
		//将x,y方向上的速度合成
		//幅值和角度
		Mat magnitude, angle;
		cartToPolar(vx, vy, magnitude, angle);
		angle = angle * 180.0 / CV_PI / 20.0;
		//将幅值归一化到0-255,便于显示
		normalize(magnitude, magnitude, 0, 255, NORM_MINMAX);
		convertScaleAbs(magnitude, magnitude);
		convertScaleAbs(angle, angle);
		//将运动的幅值和角度转化到HSV颜色空间的图像

		vector<Mat>result;
		Mat hsv = Mat::zeros(preframe.size(), preframe.type());
		split(hsv, result);
		result[0]=angle;//angle决定图像颜色
		result[1]=Scalar(255);
		result[2]=magnitude;//幅值决定图像形状
		
		//合成
		merge(result, hsv);
		//将HSV转到bgr
		Mat bgrimg;
		cvtColor(hsv, bgrimg, COLOR_HSV2BGR);
		imshow("detect", bgrimg);
		nextgry.copyTo(pregray);
		int c = waitKey(50);
		if (27 == c)
		{
			break;
		}
	}
}

 

 2.基于LK稀疏光流法的物体跟踪

 

 

void lkdrawlines(vector<Point2f> p1, vector<Point2f> p2, Mat& img)
{
	vector<Scalar> color_lut;
	RNG rng(10086);
	if (color_lut.size() < p1.size())
	{
		for (int i = 0; i < p1.size(); ++i)
		{
			color_lut.push_back(Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)));
		}
	}
	for (int j = 0; j < p1.size(); ++j)
	{
		line(img, p1[j], p2[j], color_lut[j], 1);
	}

}
void visionagin:: MyLKdetect()
{
	VideoCapture capture("C:\\Users\\86176\\Downloads\\visionimage\\detect.mp4");
	if (!capture.isOpened())
	{
		cout << "open failed !" << endl;
	}
	Mat preframe, pregray;
	capture.read(preframe);
	cvtColor(preframe, pregray, COLOR_BGR2GRAY);
	//角点检测参数设置
	vector<Point2f> conners;
	int numofconnners = 5000;
	double quiltylevel = 0.01;
	double mindist = 10;
	goodFeaturesToTrack(pregray, conners, numofconnners, quiltylevel, mindist,Mat(),3,false);
	//初始状态的角点
	vector<Point2f>initpoints;
	initpoints.insert(initpoints.end(), conners.begin(), conners.end());
	//LK算法参数
	//前一帧图像角点
	vector<Point2f>prepoints;
	prepoints.insert(prepoints.end(), conners.begin(), conners.end());
	//当前帧图像角点
	vector<Point2f>nextpoints;

	vector<uchar>isitem;
	vector<float> err;
	TermCriteria T = TermCriteria(TermCriteria::COUNT  |TermCriteria::EPS, 30, 0.1);
	while (true)
	{
		Mat nextframe, nextgray;
		if (!capture.read(nextframe))
		{
			break;
		}
		imshow("frame", nextframe);
		cvtColor(nextframe, nextgray, COLOR_BGR2GRAY);
		//LK光流跟踪
		calcOpticalFlowPyrLK(pregray, nextgray, prepoints, nextpoints, isitem, err, Size(31, 31), 3, T,0.5,0);
		//判断角点是否移动,不移动就删除
		size_t i, k;
		for (i = k = 0; i < nextpoints.size(); ++i)
		{
			double dist = abs(nextpoints[i].x - prepoints[i].x) + abs(nextpoints[i].y - prepoints[i].y);
			if (isitem[i] && dist > 2)
			{
				prepoints[k] = prepoints[i];
				initpoints[k] = initpoints[i];
				nextpoints[k++] = nextpoints[i];
				circle(nextframe, nextpoints[i], 3, Scalar(0, 255, 0), -1, 8);
			}
		}
		//更新角点数目
		prepoints.resize(k);
		initpoints.resize(k);
		nextpoints.resize(k);
		//绘制轨迹
		//for (size_t t = 0; t < nextpoints.size(); ++t)
		//{
		//	line(nextframe,initpoints[t], nextpoints[t],Scalar(0,0,255) ,1);
		//}
		lkdrawlines(nextpoints, initpoints, nextframe);

		imshow("result", nextframe);

		//更新前后图像及角点
		swap( nextpoints, prepoints);
		nextframe.copyTo(preframe);
		cout<<"pre size is ::"<< prepoints.size() << endl;
		//如果角点数目小于30,重新检测
		if (initpoints.size() < 30)
		{
			goodFeaturesToTrack(pregray, conners, numofconnners, quiltylevel, mindist, Mat(), 3, false);
			initpoints.insert(initpoints.end(), conners.begin(), conners.end());
			prepoints.insert(prepoints.end(), conners.begin(), conners.end());
			cout << "total prepoints size is : " << prepoints.size() << endl;
		}
		int c = waitKey(50);
		if (27 == c)
		{
			break;
		}
	}
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值