opencv打开USB摄像头
程序员文章站
2022-03-16 17:46:52
...
#include <thread>
Mat gImage;
#define FRAME_RATE 30 //默认帧率30
/* 默认分辨率720p */
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720
/******************************USB摄像头 *******************************/
void threadUsbCapture()
{
VideoCapture cap;
prctl(PR_SET_NAME,"threadOpencvCapture");
cap.open(4);
/* opencv默认是640*480,这里修改成720p */
cap.set(CAP_PROP_FRAME_WIDTH, DEFAULT_WIDTH);
cap.set(CAP_PROP_FRAME_HEIGHT, DEFAULT_HEIGHT);
while(1)
{
cap >> gImage;
usleep(1000 * 1000 / FRAME_RATE);
}
}
int main(int argc, char *argv[])
{
/* 初始化显示窗口 */
const string wintitle = "camera";
namedWindow(wintitle);
thread t(threadUsbCapture);
while (1)
{
/* 获取video0的数据 */
if (gImage.empty())
continue;
/* 显示图片 */
imshow(wintitle, gImage);
/* 等待按键 */
if (waitKey(30) >= 0)
{
break;
}
}
}