How to use Opencv to call the computer camera?

When we want to use opencv to process video images, the first step is often to call the computer camera. The blogger below will provide two versions of the code (with detailed notes) to help you learn how to use Opencv to call the computer camera for video. Record and save:

1. C++ version

1. Read video from camera

/*从相机中读取视频*/
#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. Read video from file

/*从文件中读取视频*/
#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. Save the video

/*保存视频*/
#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;
}

2. Python version

1. Read video from camera

'''从相机中读取视频'''
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. Read video from file

'''从文件中读取视频'''
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. Save the video

'''保存视频'''
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()   #关闭图像窗口

Guess you like

Origin blog.csdn.net/weixin_49851451/article/details/128343674