這是我的代碼。我意識到我不需要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格式。我無需查看輸出或爲流程添加更多延遲。這需要在後臺運行。 –