使用函數cv::VideoCapture::set()函數設定攝像頭的分辨率
#include "cartoon.h"
int main()
{
VideoCapture capture(0);
if (!capture.isOpened()) { //判斷能夠打開攝像頭
cout<<"can not open the camera"<<endl;
cin.get();
exit(1);
}
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
int count=0;
while (1) {
Mat frame;
capture>>frame; //載入圖像
if (frame.empty()) { //判斷圖像是否載入
cout<<"can not load the frame"<<endl;
} else {
count++;
if (count == 1) {
cout<<frame.cols<<" "<<frame.rows<<endl;
}
imshow("camera", frame);
char c=waitKey(30); //延時30毫秒
if (c == 27) //按ESC鍵退出
break;
}
}
}
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2QvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39jN5EzN0EzM5EjMygDM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)