opencv--对象跟踪(CAMShift)

本文深入探讨了CAMShift算法在视频目标跟踪中的应用,详细解释了如何使用OpenCV实现目标的定位与跟踪,包括从图像预处理到目标区域选择,再到直方图计算与反向投影的过程。

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

MeanShift算法:跟踪图像高密度区域,均值漂移,大小变化无法跟踪

CAMShift算法:可以跟踪大小变化,通过改变模型大小匹配

#include<opencv2/opencv.hpp>
#include<opencv2/tracking.hpp>
#include<iostream>
#include<math.h>

using namespace cv;
using namespace std;

int smin = 15;
int vmin = 40;
int vmax = 256;
int bins = 16;

int main(int argc, char** argv)
{
	VideoCapture capture;
	capture.open("./video/balltest.mp4");
	if(!capture.isOpened())
	{
		printf("[%s][%d]could not find video data file...\n",__FUNCTION__,__LINE__);
		return -1;
	}
	bool firstRead = true;
	float hrange[] = {0,180};
	const float* hranges = hrange;
	Rect selection;
	Mat frame;
	Mat hsv,hue,mask,hist,backprojection;
	Mat drawImg = Mat::zeros(300,300,CV_8UC3);
	bool havedest = true;//用来处理前几针没有目标物体
	while(capture.read(frame))
	{
		if(havedest == false)
		{
			imshow("input",frame);	
			if(waitKey(200) == 'h')
			{
				havedest = true;
			}
			else
			{
				continue;
			}
		}

		if(firstRead)
		{
			//区域选择
			printf("[%s][%d]Please choice roi\n",__FUNCTION__,__LINE__);
			Rect2d first = selectROI("input",frame);
			selection.x = first.x;
			selection.y = first.y;
			selection.width = first.width;
			selection.height = first.height;
			printf("[%s][%d]ROI.x=%d,ROI.y= %d,width = %d,height = %d\n",__FUNCTION__,__LINE__,
				selection.x,selection.y,selection.width,selection.height);
		}
		//转化为HSV色彩空间
		cvtColor(frame,hsv,COLOR_BGR2HSV);
		inRange(hsv,Scalar(0,smin,vmin),Scalar(180,vmax,vmax),mask);
		hue = Mat(hsv.size(),hsv.depth());
		int channels[] = {0,0};
		mixChannels(&hsv, 1, &hue, 1,channels, 1);
		if(firstRead)
		{
		//计算直方图
		Mat roi(hue,selection);
		Mat maskroi(mask,selection);
		calcHist(&roi,1,0,maskroi,hist,1,&bins,&hranges);
		normalize(hist,hist,0,255,NORM_MINMAX);
		//画直方图
		int binw = drawImg.cols/bins;
		Mat colorIndex = Mat(1,bins,CV_8UC3);
		for(int i=0;i <bins;i++)
		{
			colorIndex.at<Vec3b>(0,i) = Vec3b(saturate_cast<uchar>(i*180/bins),255,255);
		}
		cvtColor(colorIndex,colorIndex,COLOR_HSV2BGR);
		for(int i=0;i < bins;i++)
		{
			int val = saturate_cast<int>(hist.at<float>(i)*drawImg.rows/255);
			rectangle(drawImg,Point(i*binw,drawImg.rows),Point((i+1)*binw,drawImg.rows-val),\
				Scalar(colorIndex.at<Vec3b>(0,i)),-1,8,0);
		}
			firstRead = false;
		}
		//跟踪
		calcBackProject(&hue,1,0,hist,backprojection,&hranges);//反向映射图
		backprojection &=mask;
		RotatedRect trackBox = CamShift(backprojection,selection,TermCriteria((TermCriteria::COUNT|TermCriteria::EPS),10,1));

		//画出位置
		ellipse(frame,trackBox,Scalar(0,0,255),3,8);
		//imshow("drawImg",drawImg);
		//imshow("hsv",hsv);
		imshow("input",frame);
		if(27 == waitKey(30))
		{
			break;
		}
	}
	capture.release();
	return 0;
}

效果动图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值