Opencv Mat功能只有在未被引用时才有效?

时间:2016-03-11 21:40:22

标签: c++ opencv

这是我的代码。我意识到我不需要cv ::这是用于intellisense。我要介于Qt和opencv之间。这就是我的问题。在main()中我试图引用takePicture();因为它返回一个Mat框架,我试过cv :: Mat pic;。编译很好,但它永远不会捕获下一帧。它只是复制图像,但如果我输入该功能,它每次都会抓取一个新帧。 请参阅我的代码注释if(input =='c')。那是什么给出的?

    #include <opencv/cv.h> 
    #include "opencv2/highgui/highgui.hpp"
    #include <string.h> 
    #include <iostream>
    #include <time.h>

    using namespace cv; 
    using namespace std; 

    char buffer[100];
    char input; 
    int c= 1; // counter 


    // time.h timestamp
    const string currentDateTime()
   { 
     time_t     now = time(0);
     struct tm  tstruct;
     char       buf[80];
     tstruct = *localtime(&now);
     strftime(buf, sizeof(buf),"%m/%d %X", &tstruct);

     return buf;
    } 

    // capture image
    cv::Mat takePicture() {

        cv::Mat frame;
        VideoCapture cap(0);

        while(!cap.isOpened()){
        cout << "cant connect to cam" << std::endl;
        }

        double dWidth  =  cap.get(CV_CAP_PROP_FRAME_WIDTH); 
        double dHeight =  cap.get(CV_CAP_PROP_FRAME_HEIGHT); 

        cap >> frame;
        cv::resize(frame, frame, cv::Size(320, 240)); // resizes the image 

        cv::rectangle(frame, cv::Rect(0,228,130,20), 
        cv::Scalar(255,255,255), -1);   // -1 fills the rect

        cv::putText(frame,  currentDateTime(), 
        cv::Point(0,240), 1,1, cv::Scalar(0,0,0),1); //  adds time stamp

        return frame; // returns frame
     }

    int main(int argc, char* argv[])
    {
        cv::Mat pic; 
        pic = takePicture(); 


        while (1)
        {
          cout << "enter c to capture or q to quit"<<endl; 
          cin>> input; 

          if (input == 'c'){
              sprintf(buffer,"C://pics//image%d.jpg" ,c);
              imwrite(buffer, takePicture() );  // this works               
            //imwrite(buffer, pic);             // this doesn't 

              cout << buffer<<endl;     
              c++; // inc picture number    

            }else{

          if(input == 'q') 
            return -1;  
         }
        }       
      return 0; 
    }

2 个答案:

答案 0 :(得分:0)

它不是您问题的答案,但我对您的代码进行了一些更改。

我认为这是更方便的方式。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <string.h>
#include <iostream>
#include <time.h>

using namespace cv;
using namespace std;

char buffer[100];
char input;
int c= 1; // counter


// time.h timestamp
const string currentDateTime()
{
    time_t     now = time(0);
    struct tm  tstruct;
    char       buf[80];
    tstruct = *localtime(&now);
    strftime(buf, sizeof(buf),"%m/%d %X", &tstruct);

    return buf;
}

// capture image
Mat takePicture( VideoCapture cap )
{
    Mat frame;

    double dWidth  =  cap.get(CV_CAP_PROP_FRAME_WIDTH);
    double dHeight =  cap.get(CV_CAP_PROP_FRAME_HEIGHT);

    cap >> frame;
    resize(frame, frame, Size(320, 240)); // resizes the image

    rectangle(frame, Rect(0,228,130,20),
              Scalar(255,255,255), -1);   // -1 fills the rect

    putText(frame,  currentDateTime(),
            Point(0,240), 1,1, Scalar(0,0,0),1); //  adds time stamp

    return frame; // returns frame
}

int main(int argc, char* argv[])
{
    VideoCapture cap(0);
    if( !cap.isOpened())
    {
        cout << "cant connect to cam" << std::endl;
        return -1;
    }

    Mat pic;


    cout << "enter c to capture or q to quit" << endl;
    while (1)
    {
        pic = takePicture(cap);
        imshow("Video Capture", pic);
        int input = waitKey(30);

        if (input == 'c')
        {
            sprintf(buffer,"C:/pics/image%d.jpg",c);
            imwrite(buffer, pic);

            cout << buffer<<endl;
            c++; // inc picture number

        }
        else
        {

            if(input == 'q')
                return -1;
        }
    }
    return 0;
}

答案 1 :(得分:0)

pic = takePicture();应该在里面。永远不会更新。这就是为什么它只使用第一张图片

相关问题