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

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

一、C++版本

1. 从相机中读取视频

/*从相机中读取视频*/
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
	//打开捕获器(0-系统默认摄像头)
	VideoCapture cap(0); 
	Mat frame;
	//打开失败
	if (!cap.isOpened()) {
        cerr << "Cannot open camera";
        return -1;
    }
	//打开成功
	while (true) {
		//读取视频帧
		cap.read(frame);
		//显示图像
		imshow("Pig", frame);
		//监听键盘,按任意键退出
		if (waitKey(5) >= 0)
            break;
	}
    cap.release();  //释放捕获器
	return 0;
}

2. 从文件中读取视频

/*从文件中读取视频*/
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    //视频文件所在路径
    String path = "vtest001.avi";
	//打开捕获器
	VideoCapture cap(path); 
	Mat frame;
	//打开失败
	if (!cap.isOpened()) {
        cerr << "Cannot open video";
        return -1;
    }
	//打开成功
	while (true) {
		//读取视频帧
		cap.read(frame);
		//显示图像
		imshow("Pig", frame);
		//监听键盘,按任意键退出
		if (waitKey(5) >= 0)
            break;
	}
    cap.release();  //释放捕获器
	return 0;
}

3. 保存视频

/*保存视频*/
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
	//打开捕获器(0-系统默认摄像头)
	VideoCapture cap(0);
	Mat frame;
	//创建视频写入对象并定义保存格式
    VideoWriter videoWriter("out001.avi", videoWriter.fourcc('X', 'I', 'V', 'D'),25 ,Size(100, 100));
	//打开失败
	if (!cap.isOpened()) {
        cerr << "Cannot open camera";
        return -1;
    }
	//打开成功
    while(true)
    {
		//逐帧读取
		cap.read(frame);
		//显示视频画面
		imshow("test",frame);
		//进行视频保存
        videoWriter.write(frame);
		//监听键盘,按任意键退出
		if (waitKey(5) >= 0)
            break;
    }
    cap.release();  //释放捕获器
    videoWriter.release();  //释放视频写入对象
	return 0;
}

二、Python版本

1. 从相机中读取视频

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

2. 从文件中读取视频

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

3. 保存视频

'''保存视频'''
import numpy as np  #导入科学计算库numpy
import cv2 as cv  #导入opencv-python
#打开捕获器(0-系统默认摄像头)
cap = cv.VideoCapture(0)
# 定义编解码器并创建VideoWriter对象
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (640,  480))  #视频保存格式
while cap.isOpened():
    #如果正确读取帧,ret为True
    ret, frame = cap.read()
    #读取失败,则退出循环
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    #图像处理-翻转图像
    frame = cv.flip(frame, 0)
    #保存图像
    out.write(frame)
    #图像显示
    cv.imshow('frame', frame)
    #获取键盘按下那个键
    key_pressed = cv.waitKey(60)
    #如果按下esc键,就退出循环
    if key_pressed == 27:
        break
cap.release()   #释放捕获器 
out.release()  #关闭视频保存
cv.destroyAllWindows()   #关闭图像窗口

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>