用VS打开opencv调用摄像头用哪个函数
时间: 2023-06-09 21:06:15 浏览: 42
您可以在OpenCV中使用`cv::VideoCapture`类来调用摄像头。以下是调用摄像头并显示图像的示例代码:
```
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
// 打开摄像头
VideoCapture cap(0);
// 检查摄像头是否打开成功
if (!cap.isOpened())
{
std::cout << "Failed to open camera!" << std::endl;
return -1;
}
// 循环读取摄像头帧
while (true)
{
Mat frame;
cap.read(frame);
// 检查帧是否读取成功
if (frame.empty())
{
std::cout << "Failed to capture frame!" << std::endl;
break;
}
// 在窗口中显示图像
imshow("Camera", frame);
// 按下ESC键退出循环
if (waitKey(30) == 27)
{
break;
}
}
return 0;
}
```