基于感知哈希的视觉目标跟踪算法代码

  • Post author:
  • Post category:其他


#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

static Rect box;
static bool drawing_box = false;
static bool gotBB = false;

static void mouseHandler(int event, int x, int y, int flags, void *param){
	switch( event ){
	case CV_EVENT_MOUSEMOVE:
		if (drawing_box){
			box.width = x-box.x;
			box.height = y-box.y;
		}
		break;
	case CV_EVENT_LBUTTONDOWN:
		drawing_box = true;
		box = Rect( x, y, 0, 0 );
		break;
	case CV_EVENT_LBUTTONUP:
		drawing_box = false;
		if( box.width < 0 ){
			box.x += box.width;
			box.width *= -1;
		}
		if( box.height < 0 ){
			box.y += box.height;
			box.height *= -1;
		}
		gotBB = true;
		break;
	}
}

// calculate the hash code of image
static Mat calHashCode(Mat image)
{
	resize(image, image, Size(8, 8));
	Scalar imageMean = mean(image);
	return (image > imageMean[0]);
}

// calculate the pHash code of image
static Mat calPHashCode(Mat image)
{
	Mat floatImage, imageDct;
	resize(image, image, Size(32, 32));	
	image.convertTo(floatImage, CV_32FC1);
	dct(floatImage, imageDct);
	Rect roi(0, 0, 8, 8);
	Scalar imageMean = mean(imageDct(roi));
	return (imageDct(roi) > imageMean[0]);
}

// get hamming distance of two hash code
static int calHammingDistance(Mat modelHashCode, Mat testHashCode)
{
	return countNonZero(modelHashCode != testHashCode);
}

// tracker: get search patches around the last tracking box,
// and find the most similar one using hamming distance
static void hashTrack(Mat frame, Mat &model, Rect &trackBox, int flag = 0)
{
	Mat gray;
	cvtColor(frame, gray, CV_RGB2GRAY);

	Rect searchWindow;
	searchWindow.width = trackBox.width * 3;
	searchWindow.height = trackBox.height * 3;
	searchWindow.x = trackBox.x + trackBox.width * 0.5 - searchWindow.width * 0.5;
	searchWindow.y = trackBox.y + trackBox.height * 0.5 - searchWindow.height * 0.5;
	searchWindow &= Rect(0, 0, frame.cols, frame.rows);

	Mat modelHashCode, testHashCode;
	if (flag)
		modelHashCode = calHashCode(model);
	else
		modelHashCode = calPHashCode(model);
	int step = 2;
	int min = 1000;
	Rect window = trackBox;
	for (int i = 0; i * step < searchWindow.height - trackBox.height; i++)
	{
		window.y = searchWindow.y + i * step;
		for (int j = 0; j * step < searchWindow.width - trackBox.width; j++)
		{
			window.x = searchWindow.x + j * step;
			if (flag)
				testHashCode = calHashCode(gray(window));
			else
				testHashCode = calPHashCode(gray(window));
			int distance = calHammingDistance(modelHashCode, testHashCode);
			if (distance < min)
			{
				trackBox = window;
				min = distance;
			}
		}
	}
	model = gray(trackBox);
	cout << "The min hanming distance is: " << min << endl;
}

int main(int argc, const char * argv[])
{
	VideoCapture capture;
	// from video
	capture.open("optical_flow_input.avi");
	bool fromfile = true;

	// from camera
	//capture.open(0);
	//bool fromfile = false;

	//Init camera
	if (!capture.isOpened())
	{
		cout << "capture device failed to open!" << endl;
		return -1;
	}
	//Register mouse callback to draw the bounding box
	cvNamedWindow("hashTracker", CV_WINDOW_AUTOSIZE);
	cvSetMouseCallback("hashTracker", mouseHandler, NULL ); 

	Mat frame, model;
	capture >> frame;
	while(!gotBB)
	{
		if (!fromfile)
			capture >> frame;

		imshow("hashTracker", frame);
		if (cvWaitKey(20) == 'q')
			return 1;
	}
	//Remove callback
	cvSetMouseCallback("hashTracker", NULL, NULL ); 

	Mat gray;
	cvtColor(frame, gray, CV_RGB2GRAY); 
	model = gray(box);

	int frameCount = 0;
	while (1)
	{
		capture >> frame;
		if (frame.empty())
			return -1;
		double t = (double)cvGetTickCount();
		frameCount++;

		// tracking
		hashTrack(frame, model, box, 0);	

		// show
		stringstream buf;
		buf << frameCount;
		string num = buf.str();
		putText(frame, num, Point(20, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 3);
		rectangle(frame, box, Scalar(0, 0, 255), 3);
		imshow("hashTracker", frame);


		t = (double)cvGetTickCount() - t;
		cout << "cost time: " << t / ((double)cvGetTickFrequency()*1000.) << endl;

		if ( cvWaitKey(1) == 27 )
			break;
	}

	return 0;
}

转载:http://blog.csdn.net/zouxy09