使用Opencv-python库读取图像、本地视频和摄像头实时数据

使用Opencv-python库读取图像、本地视频和摄像头实时数据

Python中使用OpenCV读取图像、本地视频和摄像头数据很简单,
首先需要安装Python,然后安装Opencv-python库

pip install opencv-python

然后在PyCharm或者VScode等IDE中输入对应的Python代码

一、使用opencv-python读取图像

Lena.jpg
比如说我们要显示上面这幅数字图像处理中的lena.jpg这幅图像,读取的python代码如下所示:

import cv2

# Load an image using imread
img = cv2.imread("images/lena.jpg")
# img = cv2.imread("Resources/test.png")
# Display image
cv2.imshow("Lena image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

在Python中运行结果如下:
显示图像

使用opencv-python读取本地视频

Opencv-python在线文档中有关于的Python示例代码:https://docs.opencv.org/4.9.0/dd/d43/tutorial_py_video_display.html

import numpy as np
import cv2 as cv
cap = cv.VideoCapture('vtest.avi')
while cap.isOpened():
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
cap.release()
cv.destroyAllWindows()

test_video.mp4
视频资源可以到https://github.com/murtazahassan/Learn-OpenCV-in-3-hours/blob/master/Resources/test_video.mp4下载
相关的显示本地视频的Python代码如下:

import cv2

frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture("Resources/test_video.mp4")
while True:
    success, img = cap.read()
    img = cv2.resize(img, (frameWidth, frameHeight))
    cv2.imshow("Result", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

相应的运行结果如下图所示:
读取本地视频并显示

三、使用opencv-python读取摄像头数据并实时显示

使用opencv-python读取摄像头数据是非简单,opencv-python文档tutorial_py_video_display里面有对应的示例代码,如下:

import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
    print("Cannot open camera")
    exit()
while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    # if frame is read correctly ret is True
    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break
    # Our operations on the frame come here
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    # Display the resulting frame
    cv.imshow('frame', gray)
    if cv.waitKey(1) == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

我本地读取摄像头数据并显示的python代码如下:

import cv2

frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 50)

while True:
    success, img = cap.read()
    cv2.imshow("Result", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

运行结果如下图所示:
使用opencv-python读取本地摄像头视频

参考资料

最近更新

  1. TCP协议是安全的吗?

    2024-01-27 06:50:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-27 06:50:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-27 06:50:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-27 06:50:03       18 阅读

热门阅读

  1. 洛谷B3622

    2024-01-27 06:50:03       29 阅读
  2. Git常用命令

    2024-01-27 06:50:03       37 阅读
  3. Python图像处理:PIL库的使用

    2024-01-27 06:50:03       35 阅读
  4. 计算机网络(第六版)复习提纲11

    2024-01-27 06:50:03       27 阅读
  5. Axios 中不同的 responseType 选项

    2024-01-27 06:50:03       23 阅读
  6. Rust复合类型之元组

    2024-01-27 06:50:03       27 阅读