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

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

Python中使用OpenCV读取图像、本地视频和摄像头数据很简单,

首先需要安装Python,然后安装Opencv-python库

复制代码
pip install opencv-python

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

一、使用opencv-python读取图像

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

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

python 复制代码
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()

视频资源可以到https://github.com/murtazahassan/Learn-OpenCV-in-3-hours/blob/master/Resources/test_video.mp4下载

相关的显示本地视频的Python代码如下:

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里面有对应的示例代码,如下:

python 复制代码
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)
    # 等待1毫秒,如果用户按下q键则退出循环
    if cv.waitKey(1) == ord('q'):
        break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

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

python 复制代码
import cv2

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

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

cap.release()
cv2.destroyAllWindows()

运行结果如下图所示:

参考资料

相关推荐
龙泉寺天下行走11 小时前
LangChain Skills框架核心解析
python·langchain·aigc
echome88811 小时前
Python 装饰器详解:从入门到精通的实用指南
开发语言·python
tang7778911 小时前
爬虫代理IP池到底有啥用?
网络·爬虫·python·网络协议·tcp/ip·ip
sg_knight11 小时前
设计模式实战:享元模式(Flyweight)
python·设计模式·享元模式·flyweight
墨有66611 小时前
基于弦论流体对偶与环空间约化的湍流精确数值模型
python·流体力学·弦理论
兰文彬11 小时前
n8n 2.x版本没有内嵌Python环境
开发语言·python
smileNicky12 小时前
Spring AI系列之对话记忆与工具调用指南
人工智能·python·spring
飞Link12 小时前
深度解析 TS2Vec:时序表示学习中的层次化建模(Hierarchical Contrastive Learning)
开发语言·python·学习·数据挖掘
代码探秘者12 小时前
【Java集合】ArrayList :底层原理、数组互转与扩容计算
java·开发语言·jvm·数据库·后端·python·算法
格鸰爱童话13 小时前
向AI学习项目技能(二)
java·人工智能·python·学习