c语言版本

	VideoCapture capture;
	capture.open(1);
	if (!capture.isOpened())
	{
		printf("文件打开失败");
	}
	capture.set(CAP_PROP_FRAME_WIDTH, SRC_WIDTH);        //设置宽度
	capture.set(CAP_PROP_FRAME_HEIGHT, SRC_HEIGHT);  //设置长度

python版本

cap = cv2.VideoCapture(0) #打开默认摄像头采集图像

width = 640  #定义摄像头获取图像宽度
height = 480   #定义摄像头获取图像长度

cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)  #设置宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)  #设置长度

设置摄像头参数 谨慎修改!!

capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080);//宽度
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 960);//高度
capture.set(CV_CAP_PROP_FPS, 30);//帧率 帧/秒
capture.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 1
capture.set(CV_CAP_PROP_CONTRAST,40);//对比度 40
capture.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50
capture.set(CV_CAP_PROP_HUE, 50);//色调 50
capture.set(CV_CAP_PROP_EXPOSURE, 50);//曝光 50

获取摄像头参数

capture.get(CV_CAP_PROP_FRAME_WIDTH);
capture.get(CV_CAP_PROP_FRAME_HEIGHT);
capture.get(CV_CAP_PROP_FPS);
capture.get(CV_CAP_PROP_BRIGHTNESS);
capture.get(CV_CAP_PROP_CONTRAST);
capture.get(CV_CAP_PROP_SATURATION);
capture.get(CV_CAP_PROP_HUE);
capture.get(CV_CAP_PROP_EXPOSURE);

注意:opencv3和opencv4的宏定义有些区别
opencv4如下:

	capture.get(CAP_PROP_FRAME_WIDTH);
	capture.get(CAP_PROP_FRAME_HEIGHT);
	capture.get(CAP_PROP_FPS);

c语言完整程序

/*****************************************************
2021.5.7: opencv_capture_from_usb.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>    
#include <conio.h> 

using namespace cv;
using namespace std;

#define SRC_WIDTH  1920
#define SRC_HEIGHT 1080

int main()
{
	//测试视频
	VideoCapture capture;
	capture.open(1);
	//capture.open("v4l2src device=/dev/video4 ! video/x-raw,width=1920,height=1020,framerate=30/1 ! videoconvert ! appsink");
	if (!capture.isOpened())
	{
		printf("文件打开失败");
	}
	capture.set(CAP_PROP_FRAME_WIDTH, SRC_WIDTH);        //设置宽度
	capture.set(CAP_PROP_FRAME_HEIGHT, SRC_HEIGHT);  //设置长度
	Mat frame;
	int n = 0;
	char* cstr = new char[120];
	while (true)
	{
		
		capture >> frame;
		if (frame.data == NULL)
		{
			printf("Image is empty\n");
			//writer.write(frame);
			break;
			//continue;
		}
		char kk=waitKey(2);
		if (kk == 'S' || kk == 's')
		{

			sprintf(cstr, "%s%d%s", "caliberation/", n++, ".jpg");
			imwrite(cstr, frame);
			printf("保存了图片\n");

		}

		
		namedWindow("111", 0);//参数为零,则可以自由拖动
		imshow("111", frame);
		waitKey(2);
	}

	return 0;

}

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐