opencv图像的读取和保存以及调用摄像头。

import cv2
import numpy as np

img = cv2.imread("C:/Users/93917/Pictures/Screenshots/one.png")
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(img.shape)
print(img.size)
print(img.dtype)
print(type(img))
cv2.imwrite("d:/haha.jpg", img)

上面的代码中,imread读取图片,img是一个numpy的多维数组,,imwrite能够将图片保存到指定文件路径中,路径中的后缀名可以用任意opencv支持的图片格式。waitKey中的参数如果小于等于0,则无限等待,如果大于0,则等待给定的时间,单位是毫秒.waitKey的返回值是按键的ASCII码。

通过语句

img_array = np.array(img)

调动摄像头

import cv2 as cv

def video_demo():
    capture = cv.VideoCapture(0)   #为什么是0

    while True:
        ret, frame = capture.read()
        frame = cv.flip(frame, 1)
        cv.imshow("video", frame)

        c = cv.waitKey(10)
        if c == 27:
            break

video_demo()

frame代表视频中的每一帧,flip可以对图片进行镜像变换。

capture中,参数可以是数字,代表的是摄像头的编号,也可是是视频文件。

原文地址:https://www.cnblogs.com/loubin/p/12270214.html