使用Opencv

使用Opencv-python库读取图像、本地视频和摄像头实时数据Python中使用OpenCV读取图像、本地视频和摄像头数据很简单,

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

代码语言:javascript复制pip install opencv-python然后在PyCharm或者VScode等IDE中输入对应的Python代码

一、使用opencv-python读取图像

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

代码语言:javascript复制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

代码语言:javascript复制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代码如下:

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

代码语言:javascript复制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代码如下:

代码语言:javascript复制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()运行结果如下图所示:

使用opencv-python读取本地摄像头视频参考资料LEARN OPENCV in 3 HOURS with Python | Including 3xProjects | Computer VisionLearn-OpenCV-in-3-hoursOpenCV tutorialtutorial_py_video_displayLEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Visionmurtazahassan/Learn-OpenCV-cpp-in-4-HoursOpenCV官网OpenCV-Get StartedOpenCV Github仓库源代码

Copyright © 2088 洛江仙网游活动宝典 All Rights Reserved.
友情链接