Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
#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;
}
Posted

1 solution

There are a few things, you are not using the rect object anywhere. Why? Isn't that supposed to capture the box bounding the found person? The rect which you will detect, will be used to render the box around the person.

I wrote an article covering this aspect, but I was working with faces. So after detecting the fact, I took the area of that face and drew a rectangle with the provided X Y values. You can do the same in this case, read the article for more on that, Highlighting the faces in uploaded image in ASP.NET web applications[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900