在使用OpenCV
打开2k USB
摄像头时,发现VideoCapture
实例化的对象捕捉的视频帧的分辨率总是
640
×
480
640 \times 480
640×480,而我的摄像头硬件分辨率最高可以支持
2560
×
1440
2560 \times 1440
2560×1440,经研究发现需要重新设置分辨率。
使用OpenCV测试摄像头支持的最高分辨率
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace std;
int main()
{
cv::Mat frame;
cv::VideoCapture cap(0);
//默认分辨率
double frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
double frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
double frame_rate = cap.get(cv::CAP_PROP_FPS);
cout << "default : " << "( " << frame_width << ", " << frame_height << " )" << ", " << frame_rate << endl;
//设置分辨率,编码格式
cap.set(cv::CAP_PROP_FRAME_WIDTH, 5000);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 5000);
cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
double frame_set_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
double frame_set_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
double frame_set_rate = cap.get(cv::CAP_PROP_FPS);
cout << "after Change : " << "( " << frame_set_width << ", " << frame_set_height << " )" << ", " << frame_set_rate << endl;
return 0;
}
default : ( 640, 480 ), 30
after Change : ( 2560, 1440 ), 30
[ WARN:0] terminating async callback
可以看到,欲设置分辨率 5000 × 5000 5000\times5000 5000×5000,但是设置结束后,显示的分辨率为: 2560 × 1440 2560 \times 1440 2560×1440。因此, 2560 × 1440 2560 \times 1440 2560×1440就是该摄像头所支持的最高分辨率。
更改分辨率
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
using namespace std;
int main()
{
cv::Mat frame;
cv::VideoCapture cap(0);
//默认分辨率
double frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
double frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
double frame_rate = cap.get(cv::CAP_PROP_FPS);
cout << "default : " << "( " << frame_width << ", " << frame_height << " )" << ", " << frame_rate << endl;
//设置分辨率,编码格式
cap.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
cap.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
cap.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
double frame_set_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
double frame_set_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
double frame_set_rate = cap.get(cv::CAP_PROP_FPS);
cout << "after Change : " << "( " << frame_set_width << ", " << " )" << frame_set_height << ", " << frame_set_rate << endl;
if (!cap.isOpened())
{
return -1;
}
cout << "摄像头是否打开:" << cap.isOpened() << endl;
while (1) {
cap.read(frame);
cv::imwrite("./1920-1080-4.jpg", frame);
cv::namedWindow("frame", 0);
cv::resizeWindow("frame", 1280, 720); //设置长宽大小
cv::moveWindow("frame", 0, 0); //移动窗口到(0,0)坐标
cv::imshow("frame", frame);
cv::waitKey(1); //间隔时间长一点,保存完整图片
}
cap.release();
cv::destroyAllWindows();
return 0;
}
如此,捕获的视频帧分辨率可高达 1920 × 1080 1920 \times 1080 1920×1080。
void cv::namedWindow(const String& winname,
int flags = WINDOW_AUTOSIZE
)
void cv::resizeWindow(const String& winname,
int width,
int height
)
void cv::moveWindow(const String& winname,
int x,
int y
)
参考文献: