OpenCV之Tracking追踪demo

OpenCV之Tracking追踪demo

纯属为了记录<—>

单目标的追踪基于OpenCV3.0

OpenCV中提供tracker中有BOOSTING,MIL,KCF,TLD,MEDIANFLOW 四种跟踪方法

通过提取前景进行图像的轮廓检测进行运动物体的框选实时跟踪

#include<opencv2/opencv.hpp>
#include<opencv2/photo.hpp>
#include<opencv2/objdetect.hpp>
#include<opencv2/xfeatures2d.hpp>
#include<opencv2/features2d.hpp>
#include<opencv2/calib3d/calib3d.hpp>
#include<opencv2/stitching.hpp>
#include<opencv2/video.hpp>
#include<opencv2/bgsegm.hpp>
#include<opencv2/tracking.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/utility.hpp>
#include<cstring>
#include<iostream>
#include<fstream>

using namespace std;
using namespace cv;
using namespace cv::xfeatures2d;
int main()
{
	Mat frame;
	VideoCapture capture("E:/Images/video.avi");
	Ptr<Tracker> tracker = Tracker::create("KCF");   //BOOSTING,MIL,KCF,TLD,MEDIANFLOW 四种跟踪方法
	//Ptr<Tracker> tracker = Tracker::create("TLD");
	Rect2d rect;
	capture.read(frame);
	resize(frame, frame, Size(), 1.25, 1.25);
	imshow("frame", frame);
	rect = selectROI("frame", frame, false, false);
	tracker->init(frame, rect);
	cout << "starting tracking!" << endl;
	while (capture.read(frame))
	{
		resize(frame, frame, Size(), 1.25, 1.25);
		tracker->update(frame, rect);
		rectangle(frame, rect, Scalar(0, 0, 255), 2, 1);
		imshow("TRACK", frame);
		char c = waitKey(50);
		if (c == 27)
			break;
	}
	waitKey(0);
	return 0;
}

移动物体的侦测与统计

对运动物体的侦测以及在摄像头下的运动物体数量实时显示以及记录
通过背景建模进行前景的提取并对移动物体进行侦测

   //移动物体的侦测与统计
    #include <opencv2/opencv.hpp>
    #include <iostream>
    using namespace cv;
    using namespace std;

int main() 
{
	VideoCapture capture;
	capture.open("E:/Images/video.avi");
	if (!capture.isOpened()) {
		printf("could not load video data...\n");
		return -1;
	}

	namedWindow("input video", CV_WINDOW_AUTOSIZE);
	namedWindow("motion objects", CV_WINDOW_AUTOSIZE);

	// 初始BS模型   背景建模取背景或前景图
	Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2();
	//Ptr<BackgroundSubtractorMOG2> pMOG2 = createBackgroundSubtractorMOG2();
	//Ptr<BackgroundSubtractorKNN> pMOG2 = createBackgroundSubtractorKNN();
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));

	vector<vector<Point>> contours;
	//vector<Vec4i> hireachy;
	int count = 0;
	int maxcount = 0;
	int mincount = 100;

	Mat frame, gray, mogMask, backImage;
	while (capture.read(frame)) {
		imshow("input video", frame);
		pMOG2->apply(frame, mogMask);   //背景建模取前景图
		pMOG2->getBackgroundImage(backImage);   //提取背景图,基本没什么用
		GaussianBlur(mogMask, mogMask, Size(3, 3), 0);
		threshold(mogMask, mogMask, 100, 255, THRESH_BINARY);   //THRESH_OTSU 表示取自适应中最合适的阈值范围
		//threshold(mogMask, mogMask, 100, 255, THRESH_BINARY | THRESH_OTSU);
		morphologyEx(mogMask, mogMask, MORPH_OPEN, kernel, Point(-1, -1));
		imshow("mogMask", mogMask);
		imshow("backImage", backImage);

		findContours(mogMask, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));
		count = 0;
		char numText[8];
		for (size_t t = 0; t < contours.size(); t++) {
			double area = contourArea(contours[t]);
			if (area < 40) continue;
			Rect selection = boundingRect(contours[t]);
			if (selection.width < 5 || selection.height < 8) continue;
			count++;
			rectangle(frame, selection, Scalar(0, 0, 255), 2, 8);

			sprintf(numText, "%d", count);
			putText(frame, numText, Point(selection.x, selection.y), CV_FONT_NORMAL, FONT_HERSHEY_PLAIN, Scalar(255, 0, 0), 1, 8);
			if (count > maxcount)
			{
				maxcount = count;
				cout << "maxcount=" << maxcount << endl;
			}
			if (count < mincount)
			{
				mincount = count;
				cout << "mincount=" << mincount << endl;
			}
		}

		imshow("motion objects", frame);
		char c = waitKey(50);
		if (c == 27) {         // ESC 
			break;
		}
	}

	capture.release();
	waitKey(0);
	return 0;
}

2020-04-21:
新增基于模板的跟踪算法,实时效果还可以呀
直接撸代码,就是干!

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

using namespace std;
using namespace cv;

Rect match(Mat &Image, Mat &templateImage, int method);

int main(int argc, char *argv[])
{
	VideoCapture cap(0);
	if (!cap.isOpened())
	{
		cout << "capture not Open!" << endl;
		return 0;
	}

	cap.set(CAP_PROP_FRAME_WIDTH, 600);
	cap.set(CAP_PROP_FRAME_HEIGHT, 600);

	Mat frame;
	cap.read(frame);
	imshow("Image", frame);
	Rect roi = selectROI("Image", frame, false, false);
	Mat tempImage = frame(roi);
	Mat grayTemp;
	cvtColor(tempImage, grayTemp, CV_BGR2GRAY);
	while (cap.read(frame))
	{
		Mat Image;
		cvtColor(frame, Image, CV_BGR2GRAY);
		Rect trackroi = match(Image, grayTemp, CV_TM_SQDIFF);

		rectangle(frame, trackroi, Scalar(0, 0, 255), 1, 8, 0);
		imshow("frame", frame);

		char c = waitKey(50);
		if (c == 27) break;
	}

	waitKey(0);
	return 0;
}


Rect match(Mat &Image, Mat &templateImage, int method)
{
	int result_cols = Image.cols - templateImage.cols + 1;
	int result_rows = Image.rows - templateImage.rows + 1;

	Mat result = Mat(result_cols, result_rows, CV_32FC1);

	matchTemplate(Image, templateImage, result, method);
	normalize(result, result, 0, 1, NORM_MINMAX);

	double minVal, maxVal;
	Point minLoc, maxLoc, matchLoc;
	minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());

	switch (method)
	{
	case CV_TM_SQDIFF:
	case CV_TM_SQDIFF_NORMED:
		matchLoc = minLoc;
		break;
	default:
		matchLoc = maxLoc;
		break;
	}

	rectangle(Image, Rect(matchLoc, Size(templateImage.cols, templateImage.rows)), Scalar(0, 0, 255), 2, 8, 0);

	return Rect(matchLoc, Size(templateImage.cols, templateImage.rows));
}

觉得可以点个赞也行呀,老铁们呐。
效果不是很好,仅供参考!!!
欢迎指正批评!!!

欢迎关注微信公众号–木木夕算法笔记,与博主交流!
公众号会时不时更新有趣的图像算法哦!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值