Python-opencv读取图片,视频和调用摄像头

1.读取图片

import cv2

# LOAD AN IMAGE USING 'IMREAD'
img = cv2.imread("Resources/lena.png")
# DISPLAY
cv2.imshow("Lena Soderberg", img)
cv2.waitKey(0)

2.读取视频

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

3.调用摄像头

import cv2

frameWidth = 640
frameHeight = 480
#选用摄像头
cap = cv2.VideoCapture(0)
#设置参数
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 150)
while True:
    success, img = cap.read()
    cv2.imshow("Result", img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.set为设置参数,参数编号的对应关系怎么样,请看下面列表。

0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
3. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
4. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
5. CV_CAP_PROP_FPS Frame rate.
6. CV_CAP_PROP_FOURCC 4-character code of codec.
7. CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
8. CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
9. CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
10. CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
11. CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
12. CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
13. CV_CAP_PROP_HUE Hue of the image (only for cameras).
14. CV_CAP_PROP_GAIN Gain of the image (only for cameras).
15. CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
16. CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
17. CV_CAP_PROP_WHITE_BALANCE Currently unsupported
18. CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
使用python-opencv调用mjpg-stream视频流可以通过以下几个步骤实现: 1. 首先,我们需要安装python-opencv库。可以使用pip命令进行安装,如下所示: ``` pip install opencv-python ``` 2. 导入所需的库。我们需要导入opencv模块,以及用于从URL中读取视频流的模块urllib.request。代码如下: ```python import cv2 import urllib.request ``` 3. 定义视频流的URL。根据实际情况,将URL替换为相应的mjpg-stream视频流的URL。代码如下: ```python stream_url = "http://example.com/stream/video.mjpg" ``` 4. 使用urllib.request.urlopen()方法打开视频流URL,并读取其中的数据。代码如下: ```python stream = urllib.request.urlopen(stream_url) ``` 5. 使用cv2.VideoCapture()方法创建一个视频捕获对象,然后使用read()方法从视频流中读取每一帧图像数据。代码如下: ```python capture = cv2.VideoCapture(stream) ret, frame = capture.read() ``` 6. 在一个循环中,不断读取视频流中的每一帧图像数据,然后进行后续的处理。例如,可以显示图像、保存图像等。代码如下: ```python while True: ret, frame = capture.read() cv2.imshow("Video Stream", frame) # 处理图像数据的其他操作 if cv2.waitKey(1) == ord('q'): break capture.release() cv2.destroyAllWindows() ``` 在以上步骤中,我们首先安装了python-opencv库,然后导入所需的库,之后定义了视频流的URL。接着,通过urllib.request库打开视频流URL,并创建了一个视频捕获对象。最后,利用一个循环读取每一帧图像数据,并进行相应的处理,例如显示图像或保存图像。最后,释放资源并关闭窗口。 以上就是使用python-opencv调用mjpg-stream视频流的基本过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI小笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值