一个自己用的opencv历程

#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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值