opencv——python(1)

发布时间:2019-07-30 10:29:21编辑:auto阅读(1451)

    opencv入门的基本使用

    1. 导入opencv模块

    import cv2


    2.导入numpy模块


    import numpy as np


    3.读取当前目录图片


    img = cv2.imread("1.jpg")


    4.创建图像


    emptyImage = np.zeros(img.shape, np.uint8)


    5.复制图像


    emptyImage2 = img.copy()


    6.得到图像副本,并做灰度处理


    emptyImage3 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


    7.显示图像


    cv2.imshow("EmptyImage", emptyImage)
    cv2.imshow("EmptyImage1", img)
    cv2.imshow("EmptyImage2", emptyImage2)
    cv2.imshow("EmptyImage3", emptyImage3)

    8.写入保存图片


    cv2.imwrite("./cat2.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 5])
    cv2.imwrite("./cat3.jpg", img, [int(cv2.IMWRITE_JPEG_QUALITY), 100])
    cv2.imwrite("./cat.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 0])
    cv2.imwrite("./cat2.png", img, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])

    9.窗口等待


    cv2.waitKey(0)
    cv2.destroyAllWindows()

关键字