我有一款4k摄像头,在设置分辨率为4k的时候总是出现帧率不够的情况,
使用命令查看
v4l2-ctl --device=/dev/video0 --list-formats-ext
发现
v4l2-ctl --device=/dev/video0 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 3840x2160
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 2592x1944
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 2688x1512
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 2048x1536
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 1600x1200
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 1280x960
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 960x540
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 720x1280
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 3840x2160
Interval: Discrete 1.000s (1.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 2592x1944
Interval: Discrete 0.500s (2.000 fps)
Size: Discrete 2688x1512
Interval: Discrete 0.500s (2.000 fps)
Size: Discrete 2048x1536
Interval: Discrete 0.333s (3.000 fps)
Size: Discrete 1920x1080
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1600x1200
Interval: Discrete 0.200s (5.000 fps)
Size: Discrete 1280x960
Interval: Discrete 0.133s (7.500 fps)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 960x540
Interval: Discrete 0.050s (20.000 fps)
Size: Discrete 800x600
Interval: Discrete 0.050s (20.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
Interval: Discrete 0.040s (25.000 fps)
Interval: Discrete 0.050s (20.000 fps)
Interval: Discrete 0.100s (10.000 fps)
Size: Discrete 720x1280
Interval: Discrete 0.200s (5.000 fps)
因此我们需要设置视频捕获格式为 MJPG
C++ opencv中设置视频捕获格式为 MJPG
报错如下:
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1186) setProperty OpenCV | GStreamer warning: GStreamer: unhandled property
[ WARN:0] global ../modules/videoio/src/cap_gstreamer.cpp (1036) getProperty OpenCV | GStreamer warning: unhandled property: 6
这些警告可能是由于使用 GStreamer 后端而不是 V4L2(Video4Linux2)后端引起的。在某些系统上,默认情况下,OpenCV 使用 GStreamer 进行视频捕获。
要在 C++ 中使用 V4L2 后端设置视频捕获格式为 MJPG,您可以尝试使用以下代码:
#include <opencv2/opencv.hpp>
int main()
{
// 打开视频捕获设备
cv::VideoCapture cap(0, cv::CAP_V4L2);
// 设置视频捕获格式为 MJPG
cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
// 检查是否成功设置格式
int fourcc = cap.get(cv::CAP_PROP_FOURCC);
if (fourcc == cv::VideoWriter::fourcc('M', 'J', 'P', 'G'))
{
std::cout << "视频捕获格式已成功设置为 MJPG" << std::endl;
}
else
{
std::cout << "视频捕获格式设置失败" << std::endl;
}
// 释放视频捕获设备
cap.release();
return 0;
}
在上述代码中,我们通过将 cv::CAP_V4L2 作为第二个参数传递给 cv::VideoCapture 构造函数来明确使用 V4L2 后端。这样可以确保使用 V4L2 后端进行视频捕获,而不是默认的 GStreamer 后端。
请注意,V4L2 后端的可用性取决于您的系统和安装的 OpenCV 版本。如果您的系统不支持 V4L2 后端,您可能无法通过设置 FourCC 来更改视频捕获格式。文章来源:https://www.toymoban.com/news/detail-728432.html
下面是一个设置(Video Capture) ioctl: VIDIOC_ENUM_FMT 类型: 为MJPG
并且设置帧率和分辨率并显示视频的代码文章来源地址https://www.toymoban.com/news/detail-728432.html
/*****************************************************
2023.8.5: test_opencv.cpp
******************************************************/
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
using namespace cv;
using namespace std;
int main()
{
// 测试视频
// VideoCapture cap;
cv::VideoCapture cap(0, cv::CAP_V4L2);
if (!cap.isOpened())
{
printf("camera 打开失败\n");
}
else
{
printf("camera 打开 ok\n");
}
// 设置视频捕获格式为 MJPG
cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
// 检查是否成功设置格式
int fourcc = cap.get(cv::CAP_PROP_FOURCC);
if (fourcc == cv::VideoWriter::fourcc('M', 'J', 'P', 'G'))
{
std::cout << "视频捕获格式已成功设置为 MJPG" << std::endl;
}
else
{
std::cout << "视频捕获格式设置失败" << std::endl;
}
// 设置视频分辨率
int width = 3840;
int height = 2160;
cap.set(cv::CAP_PROP_FRAME_WIDTH, width);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, height);
// 设置视频帧率
double frameRate = 30.0;
cap.set(cv::CAP_PROP_FPS, frameRate);
// 检查是否成功设置分辨率和帧率
int setWidth = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int setHeight = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
double setFrameRate = cap.get(cv::CAP_PROP_FPS);
if (setWidth == width && setHeight == height && setFrameRate == frameRate)
{
std::cout << "视频分辨率和帧率已成功设置" << std::endl;
printf("width=%d\n", frameWidth);
printf("height=%d\n", frameHeight);
printf("width=%d\n", frameWidth);
}
else
{
std::cout << "视频分辨率和帧率设置失败" << std::endl;
}
Mat frame;
while (true)
{
cap >> frame;
if (frame.data == NULL)
{
printf("Image is empty\n");
break;
}
namedWindow("111", 0); // 参数为零,则可以自由拖动
imshow("111", frame);
waitKey(1);
}
return 0;
}
到了这里,关于C++ opencv设置视频的捕获方式为 MJPG设置失败的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!