• 图像的读取
import cv2 as cv

'''
图片的读取
'''
if __name__ == '__main__':

    # 读取图片
    img = cv.imread(r"images/1.jpg",cv.IMREAD_COLOR)
    # 显示图片
    cv.imshow("pic show",img)
    # 暂停
    cv.waitKey(0)

OpenCV(python)从入门到精通——文件操作-LMLPHP

  • 不同读取方式,灰度图读取
import cv2 as cv

'''
图片的读取
'''
if __name__ == '__main__':

    # 读取图片
    img = cv.imread(r"images/1.jpg",cv.IMREAD_GRAYSCALE)
    # 显示图片
    cv.imshow("pic show",img)
    # 暂停
    cv.waitKey(0)

OpenCV(python)从入门到精通——文件操作-LMLPHP

  • 写入图
import numpy as np
import cv2 as cv

img = np.empty((200,200,3),np.uint8)

img[...,0] = 255
img[...,1] = 0
img[...,2] =0

# bgr --->rgb的方式

#方式一: img = img[...,::-1]

# 方式2
img = cv.cvtColor(img,cv.COLOR_BGR2RGB)

cv.imwrite("img_save.jpg",img)
  • 读取视频
import cv2 as cv
# 创建对象
cap = cv.VideoCapture(0)

while True:

    # ret 表示读取状态,frame 表示此时图片
    ret,frame = cap.read()
    # 显示
    cv.imshow("frame",frame)
    if cv.waitKey(30) & 0xFF==ord('q'):
        break
# 释放资源
cap.release()
# 销毁所有窗口
cv.destroyWindow()

12-20 22:38