这是我的代码。我意识到我不需要cv ::这是为intellisense。我正在Qt和opencv之间。在这里,这是我的问题。在main()我试图引用takePicture();因为它返回一个Mat框架,我试过cv :: Mat pic ;.这编译好,但它从来没有捕获下一帧。它只是复制图像,但是如果我输入该函数,则每次都会抓取一个新帧。 请看if(input =='c')的代码注释。那么给了什么?Opencv Mat函数只在未被引用时才起作用?
#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;
}
没有意义的是清理不工作的代码。处理帧的速度要慢得多。我将不得不切换到C格式。我无需查看输出或为流程添加更多延迟。这需要在后台运行。 –