Opencv——窗口显示与视频流处理实战

创建一个窗口并显示图片

import cv2

# 创建一个名为 "My Window" 的窗口 WINDOW_NORMAL size can change

cv2.namedWindow("My Window",cv2.WINDOW_NORMAL)
img = cv2.imread('./picture/1.jpg')
cv2.imshow("My Window",img)
# 等待窗口关闭
cv2.waitKey(0)

# 释放窗口占用的内存
cv2.destroyAllWindows()

读取摄像头信息并显示

import cv2

# 创建一个窗口
cv2.namedWindow("Video Stream", cv2.WINDOW_NORMAL)

# 打开摄像头
cap = cv2.VideoCapture(1)
if not cap.isOpened():
    print("无法打开摄像头")
    exit()
while True:
    # 读取摄像头视频流
    ret, frame = cap.read()

    # 检查是否成功读取了视频流
    if not ret:
        print("Failed to read video stream")
        break

    # 检查读取到的视频帧是否为空
    if frame is None:
        print("No video frame captured")
        continue

    # 在窗口展示视频流
    cv2.imshow("Video Stream", frame)

    # 按下q键退出程序
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放摄像头并关闭窗口
cap.release()
cv2.destroyAllWindows()

摄像头获取视频

import cv2

# 创建一个窗口
cv2.namedWindow("Video Stream", cv2.WINDOW_NORMAL)

# 打开摄像头
cap = cv2.VideoCapture("/home/book/Desktop/Opencv/video/test.mp4")
if not cap.isOpened():
    print("无法打开摄像头")
    exit()
while True:
    # 读取摄像头视频流
    ret, frame = cap.read()

    # 检查是否成功读取了视频流
    if not ret:
        print("Failed to read video stream")
        break

    # 检查读取到的视频帧是否为空
    if frame is None:
        print("No video frame captured")
        continue

    # 在窗口展示视频流
    cv2.imshow("Video Stream", frame)

    # 按下q键退出程序 1000/fps  是正常的播放速度
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放摄像头并关闭窗口
cap.release()
cv2.destroyAllWindows()

摄像头获取的视频流保存到文件中

import cv2

# 获取默认摄像头的设备编号
cap = cv2.VideoCapture("/home/book/Desktop/Opencv/video/test.mp4")

# 设置保存视频流的编码器和帧率
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
fps = 20.0

# 创建VideoWriter对象,用于将视频流写入本地文件 后面的参数需要真实的摄像头分辨率
out = cv2.VideoWriter('video/output.mp4', fourcc, fps, (1920, 1032))

# 循环读取摄像头的视频流,并将视频流写入本地文件
while True:
    ret, frame = cap.read()
    if ret:
        out.write(frame)
        cv2.imshow("Video Stream", frame)
        # 按q键退出循环
        if cv2.waitKey(1) == ord('q'):
            break
    else:
        print("无法获取摄像头的视频流")
        break

# 释放摄像头资源、关闭窗口和VideoWriter对象
cap.release()
out.release()
cv2.destroyAllWindows()

鼠标点击事件

import cv2
import numpy as np
def on_mouse_click(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        print("鼠标左键点击:({}, {})".format(x, y))
    elif event == cv2.EVENT_RBUTTONDOWN:
        print("鼠标右键点击:({}, {})".format(x, y))

# 用numpy创建一个全黑的图片
img = np.zeros((500, 500, 3), np.uint8)

# 创建一个窗口,并将鼠标事件回调函数注册到窗口中
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", on_mouse_click)

# 展示图片,并等待用户关闭窗口
cv2.imshow("Image", img)
cv2.waitKey(0)

# 关闭窗口
cv2.destroyAllWindows()

实战滑动条调色板

import cv2
import numpy as np

# 定义一个回调函数,用于处理滑动条数值的变化
def nothing(x):
    pass

# 创建一个黑色的图像和一个窗口
img = np.zeros((300, 512, 3), np.uint8)
cv2.namedWindow('image')

# 创建三个滑动条,分别对应于三个颜色通道的值,初始值为0
cv2.createTrackbar('R', 'image', 0, 255, nothing)
cv2.createTrackbar('G', 'image', 0, 255, nothing)
cv2.createTrackbar('B', 'image', 0, 255, nothing)

while True:
    # 获取三个滑动条的当前值
    r = cv2.getTrackbarPos('R', 'image')
    g = cv2.getTrackbarPos('G', 'image')
    b = cv2.getTrackbarPos('B', 'image')

    # 将图像的每个像素的RGB值设置为当前的滑动条值
    img[:] = [b, g, r]

    # 显示图像
    cv2.imshow('image', img)
    k = cv2.waitKey(1) & 0xFF
    if k == 27:  # 按下ESC键退出
        break

cv2.destroyAllWindows()

你可能感兴趣的:(opencv,计算机视觉,人工智能)