#include <opencv2/opencv.hpp>
#include"highgui.h"
#include"stdio.h"
#include"cv.h"
#include<ctime>
using namespace cv;
using namespace std;
// Global variables
Rect box,box1;
bool drawing_box = false;
bool gotBB = false;
int count1=0;
int change=0;
int conserve=0;
bool photo=false;
int n=0;
// bounding box mouse callback
void mouseHandler(int event, int x, int y, int flags, void *param){
switch( event ){
case CV_EVENT_MOUSEMOVE:
if (count1==1&&drawing_box){
box.width = x-box.x;
box.height = y-box.y;
}
else if(count1==2&&drawing_box){
box1.width = x-box1.x;
box1.height = y-box1.y;
}
break;
case CV_EVENT_LBUTTONDOWN:
drawing_box = true;
count1++;
if(count1==1){
box = Rect( x, y, 0, 0 );
}
else
box1=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;
}
if(count1==2)
{
gotBB = true;
}
break;
}
}
//
void Delay(int time)//time*1000为秒数
{
clock_t now = clock();
while( clock() - now < time );
}
IplImage* DoPyrDown(IplImage* image, int filter = IPL_GAUSSIAN_5x5)
{
// 设置大小缩小一倍
CvSize size = cvSize(image->width / 2,image->height / 2);
//初始化图片
IplImage* outImage = cvCreateImage(size,image->depth,image->nChannels);
//实现缩放
cvPyrDown(image,outImage);
cvReleaseImage(&image);
return outImage;
}
// tracker: get search patches around the last tracking box,
// and find the most similar one
void tracking(Mat frame, Mat &model, Rect &trackBox)
{
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 similarity;
matchTemplate(gray(searchWindow), model, similarity, CV_TM_CCOEFF_NORMED);
double mag_r;
Point point;
minMaxLoc(similarity, 0, &mag_r, 0, &point);
trackBox.x = point.x + searchWindow.x;
trackBox.y = point.y + searchWindow.y;
model = gray(trackBox);
}
int main(int argc, char * argv[])
{
VideoCapture capture(0);
bool fromfile = true;
//Init camera
if (!capture.isOpened())
{
cout << "capture device failed to open!" << endl;
return -1;
}
//Register mouse callback to draw the bounding box
cvNamedWindow("Tracker", CV_WINDOW_AUTOSIZE);
cvSetMouseCallback("Tracker", mouseHandler, NULL );
Mat frame, model,model1;
capture >> frame;
while(!gotBB)
{
if (!fromfile)
capture >> frame;
imshow("Tracker", frame);
if (cvWaitKey(20) == 'q')
return 1;
}
//Remove callback
cvSetMouseCallback("Tracker", NULL, NULL );
Mat gray;
cvtColor(frame, gray, CV_RGB2GRAY);
model = gray(box);
model1=gray(box1);
int frameCount = 0;
while (1)
{
capture >> frame;
if (frame.empty())
return -1;
frameCount++;
// tracking
tracking(frame, model, box);
// show
tracking(frame,model1,box1);
stringstream buf;
buf << frameCount;
string num = buf.str();
if(change==0)
{
rectangle(frame, box, Scalar(0, 0, 255), 3);
rectangle(frame, box1, Scalar(0, 255, 255), 3);
}
Rect rect = box1 & box;
imshow("Tracker", frame);
IplImage temp=IplImage(frame);
IplImage *frame1=&temp;
if ( cvWaitKey(1) == 27 )
break;
if(rect.width>0&&photo==false)
{
conserve++;
change=1;
//初始化一张图片
IplImage* outImage = cvCreateImage(cvGetSize(frame1),frame1->depth,frame1->nChannels);
// 将原图拷贝过来
cvCopy(frame1,outImage,NULL);
//实现缩放操作
outImage = DoPyrDown(outImage);
//设置保存的图片名称和格式
//char* outImageName ="m+test.jpg";
//保存图片
if(conserve%18==0)
{
photo=true;
n++;
char str[20];
sprintf(str, "%dtest.jpg", n);
cvSaveImage(str,outImage);
cvReleaseImage(&outImage);
printf(" 恭喜你,保存图片成功!\n");
}
}
else if(rect.width<=0)
{
photo=false;
change=0;
}
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)