示例代码
#include <stdio.h>
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cam;
cam.open(0);
if (!cam.isOpened())
{
printf("Can not open camera!\n");
return -1;
}
cam.set(CAP_PROP_FRAME_WIDTH, 1279);
cam.set(CAP_PROP_FRAME_HEIGHT, 719);
namedWindow("camera", WINDOW_FREERATIO);
namedWindow("colorMap", WINDOW_FREERATIO);
Mat frame;
Mat after;
cam.read(frame);
printf("camera frame size:%d,%d\n", frame.cols, frame.rows);
while (true)
{
cam.read(frame);
applyColorMap(frame, after, COLORMAP_COOL);
imshow("camera", frame);
imshow("colorMap", after);
if (waitKey(1) == '1')
{
break;
}
}
return 0;
}
运行效果:
配置摄像头的分辨率时
CAP_PROP_FRAME_WIDTH
和CAP_PROP_FRAME_HEIGHT
比目标分辨率小1的原因是,如果刚好是1280*720,画面会卡顿。