我正在运行 ubuntu 22.04 和 opencv4 下面的代码,我正在编译:
// COMPILE: g++ -o sample sample.cpp `pkg-config --cflags --libs opencv4`:
#include <opencv4/opencv2/core.hpp>
#include <opencv4/opencv2/videoio.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
int main(int, char**)
{
Mat frame;
//--- INITIALIZE VIDEOCAPTURE
//VideoCapture cap(0, CAP_FFMPEG);
VideoCapture cap(0);
// open the default camera using default API
// cap.open(0);
// OR advance usage: select any API backend
int deviceID = 0; // 0 = open default camera
int apiID = cv::CAP_ANY; // 0 = autodetect default API
// open selected camera using selected API
cap.open(deviceID, apiID);
// check if we succeeded
if (!cap.isOpened()) {
cerr << "ERROR! Unable to open camera\n";
return -1;
}
cap.set(CAP_PROP_FPS,30);
cap.set(CAP_PROP_FRAME_WIDTH,1920);
cap.set(CAP_PROP_FRAME_HEIGHT,1280);
cout << "Width: " << cap.get(CAP_PROP_FRAME_WIDTH) << endl;
cout << "Height: " << cap.get(CAP_PROP_FRAME_HEIGHT) << endl;
cout << "FPS: " << cap.get(CAP_PROP_FPS) << endl;
//--- GRAB AND WRITE LOOP
cout << "Start grabbing" << endl
<< "Press any key to terminate" << endl;
for (;;)
{
// wait for a new frame from camera and store it into 'frame'
cap.read(frame);
// check if we succeeded
if (frame.empty()) {
cerr << "ERROR! blank frame grabbed\n";
break;
}
// show live and wait for a key with timeout long enough to show images
imshow("Live", frame);
if (waitKey(5) >= 0)
break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
只有在我注释行以设置 WIDTH、HEIGHT 和 FPS 时,代码才有效。就像现在一样,它永远不会到达 couts 以获得 WIDTH、HEIGHT 和 FPS。从 VideoCapture 添加或删除 CAP_FFMPEG 没有任何区别。 使用 3 个设置器,代码开始,不会给出任何错误,只是停止。
我在 python 中尝试了相同的代码并且设置器工作。在 C++ 中,
cout << "Backend: " << cap.getBackendName() << endl;
显示 FFMPEG。
有什么想法吗? 感谢您的帮助。