C++ opencv设置视频的捕获方式为 MJPG设置失败

我有一款4k摄像头,在设置分辨率为4k的时候总是出现帧率不够的情况,

使用命令查看

csharp 复制代码
v4l2-ctl --device=/dev/video0 --list-formats-ext

发现

csharp 复制代码
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

报错如下:

csharp 复制代码
[ 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,您可以尝试使用以下代码:

csharp 复制代码
#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 来更改视频捕获格式。

下面是一个设置(Video Capture) ioctl: VIDIOC_ENUM_FMT 类型: 为MJPG

并且设置帧率和分辨率并显示视频的代码

csharp 复制代码
/*****************************************************
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;
}
相关推荐
tadus_zeng15 分钟前
Windows C++ 排查死锁
c++·windows
EverestVIP16 分钟前
VS中动态库(外部库)导出与使用
开发语言·c++·windows
胡斌附体1 小时前
qt socket编程正确重启tcpServer的姿势
开发语言·c++·qt·socket编程
GalaxyPokemon1 小时前
Muduo网络库实现 [十] - EventLoopThreadPool模块
linux·服务器·网络·c++
守正出琦1 小时前
日期类的实现
数据结构·c++·算法
ChoSeitaku1 小时前
NO.63十六届蓝桥杯备战|基础算法-⼆分答案|木材加工|砍树|跳石头(C++)
c++·算法·蓝桥杯
YueiL1 小时前
C++入门练习之 给出年分m和一年中的第n天,算出第n天是几月几号
开发语言·c++·算法
我不想当小卡拉米2 小时前
C++:继承+菱形虚拟继承的一箭双雕
开发语言·jvm·c++
机器视觉知识推荐、就业指导2 小时前
QML 批量创建模块 【Repeater】 组件详解
前端·c++·qml
橙橙子2302 小时前
c++柔性数组、友元、类模版
开发语言·c++·柔性数组