OpenCV学习笔记(1):图像和视频的加载和显示
想学习的全面一点看过来吧~ 最全注释,初学不懂的都在注释上面了
01 创建和显示窗口
import cv2
import matplotlib.pyplot as plt
import numpy as np
"""
https://www.bilibili.com/video/BV12Z4y1z7jb?spm_id_from=333.337.search-card.all.click
【不要再看那些过时的OpenCV老教程了】2022巨献,OpenCV零基础小白最新版全套
33h19m46s
"""
def display_window():
cv2.namedWindow(winname="window",)
cv2.imshow('window', 0)
key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
print('准备销毁窗口')
cv2.destroyAllWindows()
display_window()
02 读取和显示图像
opencv 显示图片 读取图片的颜色通道是按照蓝绿红(BGR) 所以一般opencv读出来的图片不要用别的方式去展示 比如matplotlib matplotlib显示 注意要加plt.show()函数
def display_picture():
img1 = cv2.imread('picture.png')
cv2.imshow('test', img1)
key = cv2.waitKey(0)
if key & 0xFF == ord('q'):
cv2.destroyAllWindows()
display_picture()
测试一下颜色通道不一致时的图像区别: 运行注释掉上边的plt绘图函数后有以下结果: 原图在这:
03 保存图片
cv2.imwrite(filename, img, params=None) 保存图片函数
def save_picture():
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.resizeWindow('img', 640, 480)
img = cv2.imread('picture.png')
while True:
cv2.imshow('img', img)
key = cv2.waitKey(0)
if key == ord('q'):
break
elif key == ord('s'):
cv2.imwrite('123.jpeg', img)
else:
print(key)
cv2.destroyAllWindows()
save_picture()
04 显示视频
cv2.VideoCapture(‘video.mp4’) cv2.waitKey(1000 // 30) 读到的图片显示的时间 越小切换到下一帧的时间越短 相当于视频快进 想和原视频播放一致的话,需要计算延时时间 需要知道原先视频的帧数
def display_video():
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.resizeWindow('img', 640, 480)
cap = cv2.VideoCapture('video.mp4')
while True:
ret, frame = cap.read()
if not ret:
break
cv2.imshow('img', frame)
key = cv2.waitKey(1000 // 30)
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
display_video()
05 视频录制
def record_video():
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
vw = cv2.VideoWriter('output.mp4', fourcc, 30, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
vw.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1000 // 30) == ord('q'):
break
cap.release()
vw.release()
cv2.destroyAllWindows()
06 控制鼠标
def control_mouse():
import numpy
def mouse_callback(event, x, y, flags, userdata):
print(event, x, y, flags, userdata)
if event == 2:
cv2.destroyAllWindows()
cv2.namedWindow('mouse', cv2.WINDOW_NORMAL)
cv2.resizeWindow('mouse', 640, 360)
cv2.setMouseCallback('mouse', mouse_callback, '123')
img = numpy.zeros((360, 360, 3), np.uint8)
while True:
cv2.imshow('mouse', img)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
control_mouse()
结果: 单击时显示 为了方便大家,整理了一下常用的鼠标事件
事件名 | 鼠标活动 | 代号 |
---|
EVENT_MOUSEMOVE | 鼠标移动 | 0 | EVENT_LBUTTONDOWN | 左键按下 | 1 | EVENT_RBUTTONDOWN | 右键按下 | 2 | EVENT_MBUTTONDOWN | 中键按下 | 3 | EVENT_LBUTTONUP | 左键释放 | 4 | EVENT_RBUTTONUP | 右键释放 | 5 | EVENT_MBUTTONUP | 中键释放 | 6 | EVENT_LBUTTONDBLCLK | 左键双击 | 7 | EVENT_RBUTTONDBLCLK | 右键双击 | 8 | EVENT_MBUTTONDBLCLK | 中键双击 | 9 | EVENT_MOUSEWHEEL | 滑动滚轮上下滚动 | 10 | EVENT_MOUSEHWHEEL | 滑动滚轮左右滚动 | 11 |
07 TrackBar拖动控件的使用
def trackbar():
cv2.namedWindow('trackbar', cv2.WINDOW_NORMAL)
cv2.resizeWindow('trackbar', 640, 480)
def callback(value):
pass
cv2.createTrackbar('R', 'trackbar', 0, 255, callback)
cv2.createTrackbar('G', 'trackbar', 0, 255, callback)
cv2.createTrackbar('B', 'trackbar', 0, 255, callback)
img = np.zeros((360, 360, 3), np.uint8)
while True:
r = cv2.getTrackbarPos('R', 'trackbar')
g = cv2.getTrackbarPos('G', 'trackbar')
b = cv2.getTrackbarPos('B', 'trackbar')
img[:] = [b, g, r]
cv2.imshow('trackbar', img)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
trackbar()
延时1ms即改变,也就是实时,没事可以玩一下,像画图的调色板一样
后边也会持续更新OpenCV系列的学习笔记的,觉得有用的小伙伴点个赞加个关注支持一下吧!一起学习!!!还有已完结的PyQt5可视化GUI界面设计系列哦~
|