[转]OpenCV2打开摄像头并显示图像
cited from: https://blog.csdn.net/zxc024000/article/details/49745065
网上有很多例子,但绝大部分是用Opencv1写的。这里基于Opencv2,打开摄像头,并显示图像
1 #include <opencv2\core\core.hpp> 2 #include <opencv2\highgui\highgui.hpp> 3 #include <opencv2\opencv.hpp> 4 5 using namespace cv; 6 int main() 7 { 8 VideoCapture capture(0);//创建VideoCapture对象 9 if (!capture.isOpened())//判断是否打开摄像头 10 return 1; 11 bool stop(false); 12 Mat frame;//用来存放读取的视频序列 13 Mat dst; 14 namedWindow("linduo"); 15 while (!stop) 16 { 17 if (!capture.read(frame))//读取ing 18 { 19 break; 20 } 21 //cvtColor(frame, dst, CV_RGB2GRAY);//转灰度图 22 //imshow("linduo", dst);//灰度图显示 23 imshow("linduo", frame);//正常显示 24 //Esc键停止 25 char c = cvWaitKey(33); 26 if (c == 27)break; 27 } 28 capture.release(); 29 }