本文介绍了如何在人物周围绘制边界框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在大学里学习OpenCV项目。我正在编写一个代码,使用框架差异方法执行视频文件的简单背景减法,我成功但我想在减去图像中移动的人周围绘制一个边界框。



我尝试过:



I am starting to learn OpenCV for a project in college. I was working on a code that performs a simple background subtraction of video file using the frame difference method which i succeeded but i wanted to draw a bounding box around the person moving in the subtracted image.

What I have tried:

#include"opencv2\imgproc.hpp"
#include"opencv2\highgui.hpp"
#include"opencv2\video.hpp"
#include"opencv\cv.h"
#include<vector>
#include<stdint.h>
#include<iostream>

using namespace cv;
using namespace std;

Mat currentframe;
Mat prevframe, difframe;
vector<vector<point> > contours;
vector<vec4i> hierarchy;
int area = 0;
int idx = 5;
Rect rect;

int main(int argc, char* argv[]) {

	//Capture Video
	VideoCapture cap("1.mp4");
	if (!cap.isOpened()) {
		cerr << "Unable to open file" << endl;
		return -1;
	}

	

	//namedWindow("Input_Seq", 1);

	namedWindow("Frame Difference", 1);


	while (1) {
		cap >> currentframe;

		if (currentframe.empty())
			break;
		cvtColor(currentframe, currentframe, CV_BGR2GRAY);

		cap >> prevframe;

		if (prevframe.empty())
			break;
		cvtColor(prevframe, prevframe, CV_BGR2GRAY);

		absdiff(currentframe, prevframe, difframe);
		

		//GaussianBlur(difframe, difframe, Size(3, 3), 0, 1, BORDER_DEFAULT);
		Canny(difframe, difframe, 30, 100, 3);
		findContours(difframe, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

		
		

		imshow("Input_Seq", currentframe);
		imshow("Frame Difference", difframe);
		char c = (char)waitKey(25);
		if (c == 27)
			break;
	}
	cap.release();
	destroyAllWindows();
	return 0;
}

推荐答案


这篇关于如何在人物周围绘制边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 22:13