本人菜鸟一枚,很多代码到现在读起来也挺费劲的,前段时间在http://blog.csdn.net/thefutureisour/article/details/7530177上看到一篇关于摄像头启动的博文,学到很多。作为知识储备,因此写此博文以方便自己今后的资料查找和学习,话不多说,直接上代码。
#include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> using namespace cv; int main() { VideoCapture cap(1); if(!cap.isOpened()) { return -1; } Mat frame; Mat edges; bool stop = false; while(!stop) { cap>>frame; cvtColor(frame, edges, CV_BGR2GRAY); imshow("当前视频",edges); if(waitKey(30) >=0) stop = true; } return 0; }
1. VideoCapture类有两种用法,一种是VideoCapture(const string& filename)用来打开视频文件,一种是VideoCapture(int device)用来打开设备。
2. isOpened函数用来检测VideoCapture类是否打开成功。
3. C++版本的OpenCV有一个明显的好处,就是不需要释放操作(不论是视频还是图片),VideoCapture类的析构函数会自动帮你完成。