采用最新版OpenCV 4.7.0 摄像头对罗技摄像头进行视频图像抓取时,发现存在打开摄像头慢的问题。

测试环境如下:

系统 Windows 10 专业版
CPU Intel i7-7700K @ 4.20GHz 
摄像头型号 罗技Logitech C930c 网络摄像头
OpenCV版本 4.7.0
语言 C++

测试结果表明:

官网编译好的OpenCV 4.7.0库,可以用cv::CAP_ANY(默认)及cv::CAP_DSHOW等方式打开摄像头并抓取图像,摄像头打开时间为2~5秒,图像帧的分辨率越高,打开时间越长。

官网编译好的OpenCV库无法采用cv::CAP_MSMF方式打开摄像头,需要自己编译。

在编译时,需要勾选上“WITH_MSMF”。

在采用cv::CAP_MSMF打开摄像头时,需要先声明环境变量以提高摄像头加载速度。

_putenv("OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS=0");

摄像头打开时间缩短到0.3秒左右。

完整代码如下:

#include <iostream>
#include <cstdlib>
#include <opencv2/opencv.hpp>
int main()
{
	_putenv("OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS=0");
	int camIndex = 0;
	int outputWidth = 1920;
	int outputHeight = 1080;

	cv::VideoCapture camera_capture;
	camera_capture.open(camIndex, cv::CAP_MSMF);
	camera_capture.set(cv::CAP_PROP_FRAME_WIDTH, outputWidth);
	camera_capture.set(cv::CAP_PROP_FRAME_HEIGHT, outputHeight);
	
	if (!camera_capture.isOpened()) {
		std::cerr << "ERROR! Unable to open camera\n";
		return -1;
	}

	cv::Mat cam_frame_mat;
	while (1)
	{
		camera_capture >> cam_frame_mat;
		if (!cam_frame_mat.empty())
		{
			cv::imshow("cam_frame", cam_frame_mat);
		}
		if (cv::waitKey(1) == 27)
			break;
	}

	return 0;
}

另外解决方案供参考:

1. 采用ffmpeg抓取并通过回调函数将图像帧数据传出;

2. 采用Windows系统自带的Media Fondation库抓取摄像头图像。

有啥问题,欢迎到评论区讨论。

GitHub 加速计划 / opencv31 / opencv
238
21
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
1f47f5ba imgproc: replace assertion in cornerSubPix with descriptive error #28404 Replace CV_Assert with explicit bounds check for initial corners in cornerSubPix. This provides a descriptive runtime error instead of abrupt termination, improving error handling for Python and C++ users. No algorithmic or behavioral change for valid inputs. Fixes #25139 (Related to #7276). ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 10 小时前
fba658b9 Improve precision of RotatedRect::points 1 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐