当前位置:   article > 正文

如何使用Opencv调用电脑摄像头?_opencv打开摄像头

opencv打开摄像头

当我们想要使用opencv对视频图像进行处理时,往往第一步便是需要调用电脑摄像头,下面博主将提供两种版本的代码(含详细注释),帮助大家学习如何使用Opencv调用电脑摄像头进行视频录制并保存:

一、C++版本

1. 从相机中读取视频

  1. /*从相机中读取视频*/
  2. #include <opencv2/imgcodecs.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. #include <iostream>
  6. using namespace cv;
  7. using namespace std;
  8. int main()
  9. {
  10. //打开捕获器(0-系统默认摄像头)
  11. VideoCapture cap(0);
  12. Mat frame;
  13. //打开失败
  14. if (!cap.isOpened()) {
  15. cerr << "Cannot open camera";
  16. return -1;
  17. }
  18. //打开成功
  19. while (true) {
  20. //读取视频帧
  21. cap.read(frame);
  22. //显示图像
  23. imshow("Pig", frame);
  24. //监听键盘,按任意键退出
  25. if (waitKey(5) >= 0)
  26. break;
  27. }
  28. cap.release(); //释放捕获器
  29. return 0;
  30. }

2. 从文件中读取视频

  1. /*从文件中读取视频*/
  2. #include <opencv2/imgcodecs.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. #include <iostream>
  6. using namespace cv;
  7. using namespace std;
  8. int main()
  9. {
  10. //视频文件所在路径
  11. String path = "vtest001.avi";
  12. //打开捕获器
  13. VideoCapture cap(path);
  14. Mat frame;
  15. //打开失败
  16. if (!cap.isOpened()) {
  17. cerr << "Cannot open video";
  18. return -1;
  19. }
  20. //打开成功
  21. while (true) {
  22. //读取视频帧
  23. cap.read(frame);
  24. //显示图像
  25. imshow("Pig", frame);
  26. //监听键盘,按任意键退出
  27. if (waitKey(5) >= 0)
  28. break;
  29. }
  30. cap.release(); //释放捕获器
  31. return 0;
  32. }

3. 保存视频

  1. /*保存视频*/
  2. #include <opencv2/imgcodecs.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <opencv2/imgproc.hpp>
  5. #include <iostream>
  6. using namespace cv;
  7. using namespace std;
  8. int main()
  9. {
  10. //打开捕获器(0-系统默认摄像头)
  11. VideoCapture cap(0);
  12. Mat frame;
  13. //创建视频写入对象并定义保存格式
  14. VideoWriter videoWriter("out001.avi", videoWriter.fourcc('X', 'I', 'V', 'D'),25 ,Size(100, 100));
  15. //打开失败
  16. if (!cap.isOpened()) {
  17. cerr << "Cannot open camera";
  18. return -1;
  19. }
  20. //打开成功
  21. while(true)
  22. {
  23. //逐帧读取
  24. cap.read(frame);
  25. //显示视频画面
  26. imshow("test",frame);
  27. //进行视频保存
  28. videoWriter.write(frame);
  29. //监听键盘,按任意键退出
  30. if (waitKey(5) >= 0)
  31. break;
  32. }
  33. cap.release(); //释放捕获器
  34. videoWriter.release(); //释放视频写入对象
  35. return 0;
  36. }

二、Python版本

1. 从相机中读取视频

  1. '''从相机中读取视频'''
  2. import numpy as np #导入科学计算库numpy
  3. import cv2 as cv #导入opencv-python
  4. #打开捕获器(0-系统默认摄像头)
  5. cap = cv.VideoCapture(0)
  6. #打开失败
  7. if not cap.isOpened():
  8. print("Cannot open camera")
  9. exit()
  10. #打开成功
  11. while True:
  12. #如果正确读取帧,ret为True
  13. ret, frame = cap.read()
  14. #读取失败,则退出循环
  15. if not ret:
  16. print("Can't receive frame (stream end?). Exiting ...")
  17. break
  18. #图像处理-转换为灰度图
  19. gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
  20. #显示画面
  21. cv.imshow('frame', gray)
  22. #获取键盘按下那个键
  23. key_pressed = cv.waitKey(60)
  24. #如果按下esc键,就退出循环
  25. if key_pressed == 27:
  26. break
  27. cap.release() #释放捕获器
  28. cv.destroyAllWindows() #关闭图像窗口

2. 从文件中读取视频

  1. '''从文件中读取视频'''
  2. import numpy as np #导入科学计算库numpy
  3. import cv2 as cv #导入opencv-python
  4. #打开捕获器('vtest.avi'-视频文件所在路径)
  5. cap = cv.VideoCapture('vtest.avi')
  6. #逐帧处理图像
  7. while cap.isOpened():
  8. #如果正确读取帧,ret为True
  9. ret, frame = cap.read()
  10. #读取失败,则退出循环
  11. if not ret:
  12. print("Can't receive frame (stream end?). Exiting ...")
  13. break
  14. #图像处理-转换为灰度图
  15. gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
  16. #显示画面
  17. cv.imshow('frame', gray)
  18. #获取键盘按下那个键
  19. key_pressed = cv.waitKey(60)
  20. #如果按下esc键,就退出循环
  21. if key_pressed == 27:
  22. break
  23. cap.release() #释放捕获器
  24. cv.destroyAllWindows() #关闭图像窗口

3. 保存视频

  1. '''保存视频'''
  2. import numpy as np #导入科学计算库numpy
  3. import cv2 as cv #导入opencv-python
  4. #打开捕获器(0-系统默认摄像头)
  5. cap = cv.VideoCapture(0)
  6. # 定义编解码器并创建VideoWriter对象
  7. fourcc = cv.VideoWriter_fourcc(*'XVID')
  8. out = cv.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) #视频保存格式
  9. while cap.isOpened():
  10. #如果正确读取帧,ret为True
  11. ret, frame = cap.read()
  12. #读取失败,则退出循环
  13. if not ret:
  14. print("Can't receive frame (stream end?). Exiting ...")
  15. break
  16. #图像处理-翻转图像
  17. frame = cv.flip(frame, 0)
  18. #保存图像
  19. out.write(frame)
  20. #图像显示
  21. cv.imshow('frame', frame)
  22. #获取键盘按下那个键
  23. key_pressed = cv.waitKey(60)
  24. #如果按下esc键,就退出循环
  25. if key_pressed == 27:
  26. break
  27. cap.release() #释放捕获器
  28. out.release() #关闭视频保存
  29. cv.destroyAllWindows() #关闭图像窗口
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/819643
推荐阅读
相关标签
  

闽ICP备14008679号