OpenCV学习之路(六)——对象检测与跟踪(基于颜色)

版权声明:谢谢你那么厉害还看了我的文章,欢迎转载交流学习~ https://blog.csdn.net/kilotwo/article/details/88926383

一些基本知识

contours被定义成二维浮点型向量,这里面将来会存储找到的边界的(x,y)坐标。vector<Vec4i>hierarchy是定义的层级。这个在找边界findcontours的时候会自动生成,这里只是给它开辟一个空间。将来findContours( src, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));就能算出边界的坐标,存在contours里面。
typedef Vec<int, 4> Vec4i;
Vec4i指的是四个整形数
膨胀:高亮区域增长(取局部MAX)
腐蚀:暗部地区增长(取局部MIN)

基于颜色跟踪实现步骤

  • inRange过滤
  • 形态学操作提取
  • 轮廓查找
  • 外界矩形获取
  • 位置标定

inRange()彩色图像分割

void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)

  • src:输入图像,CV2常用Mat类型;
  • lowerb:lower boundary下限,scalar类型的像素值,单通道scalar取一个值就行,彩图3通道scalar三个值;
  • upperb:上限,类型与lowerb同理;
  • dst:输出图像,尺寸与src一致,类型是CV_8U,但没有指定通道数。
  • 对于多通道的输入,输出结果是各个通道的结果相与,当各通道结果都在上下限之内时,输出为255,否则为0。因此也有人将输出理解为掩码模板!

形态学开操作和膨胀

morphologyEx(mask, mask, MORPH_OPEN, kernel1, Point(-1, -1), 1); // 开操作
dilate(mask, mask, kernel2, Point(-1, -1), 4);// 膨胀

使用经过颜色分割后的图像来实现轮廓发现与位置标定,返回ROI矩形

void processFrame(Mat &binary, Rect &rect) {
	vector<vector<Point>> contours;//储存轮廓点(二维向量)
	vector<Vec4i> hireachy;			//轮廓层次信息
	
	findContours(binary, contours, hireachy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));//查找轮廓
	if (contours.size() > 0) {
		double maxArea = 0.0;
		for (size_t t = 0; t < contours.size(); t++) { //几个轮廓遍历
			double area = contourArea(contours[t]);
			if (area > maxArea) {//寻找面积最大的轮廓
				maxArea = area;
				rect = boundingRect(contours[t]);
			}
		}
	}
	else {
		rect.x = rect.y = rect.width = rect.height = 0;
	}

}

完整代码实现

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

using namespace std;
using namespace cv;

Rect roi;
void processFrame(Mat &binary, Rect &rect);

int main(int argc, char* argv) {
	// load video
	VideoCapture capture;
	capture.open("006.mp4");
	if (!capture.isOpened()) {
		printf("could not find video file");
		return -1;
	}

	Mat frame, mask;
	Mat kernel1 = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));
	Mat kernel2 = getStructuringElement(MORPH_RECT, Size(5, 5), Point(-1, -1));

	namedWindow("input video", CV_WINDOW_AUTOSIZE);
	namedWindow("track mask", CV_WINDOW_AUTOSIZE);
	while (capture.read(frame)) {
		inRange(frame, Scalar(0, 127, 0), Scalar(120, 255, 120), mask); // 过滤
		morphologyEx(mask, mask, MORPH_OPEN, kernel1, Point(-1, -1), 1); // 开操作
		dilate(mask, mask, kernel2, Point(-1, -1), 4);// 膨胀
		imshow("track mask", mask);

		// 使用经过颜色过滤后的图像来实现轮廓发现与位置标定,返回ROI矩形
		processFrame(mask, roi); 
		//利用对角线两点来画矩形:
		//void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = 8, int shift = 0)
		// 传入矩形参数来画矩形:
		//void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness = 1, int lineType = 8, int shift = 0)
		//得到位置后在原图像上画出ROI
		rectangle(frame, roi, Scalar(0, 0, 255), 3, 8, 0);
		imshow("input video",frame);

		// trigger exit
		char c = waitKey(100);
		if (c == 27) {
			break;
		}
	}

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

void processFrame(Mat &binary, Rect &rect) {
	vector<vector<Point>> contours;//储存轮廓点(二维向量)
	vector<Vec4i> hireachy;			//轮廓层次信息
	
	findContours(binary, contours, hireachy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point(0, 0));//查找轮廓
	if (contours.size() > 0) {
		double maxArea = 0.0;
		for (size_t t = 0; t < contours.size(); t++) { //几个轮廓遍历
			//double area = contourArea(contours[static_cast<int>(t)]);
			double area = contourArea(contours[t]);
			if (area > maxArea) {//寻找面积最大的轮廓
				maxArea = area;
				//rect = boundingRect(contours[static_cast<int>(t)]);
				rect = boundingRect(contours[t]);
			}
		}
	}
	else {
		rect.x = rect.y = rect.width = rect.height = 0;
	}

}

效果图

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/kilotwo/article/details/88926383
今日推荐