0
点赞
收藏
分享

微信扫一扫

【千律】OpenCV基础:图像的灰度化和二值化

闲鱼不咸_99f1 2022-03-19 阅读 68
pythonopencv

环境:Python3.8 和 OpenCV

内容:图像的灰度化和二值化

import cv2 as cv
import matplotlib.pyplot as plt


# 封装图片显示函数
def image_show(image):
    if image.ndim == 2:
        plt.imshow(image, cmap='gray')
    else:
        image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
        plt.imshow(image)
    plt.show()


if __name__ == '__main__':
    # 图像的读取与显示
    img = cv.imread('lenna.png')
    image_show(img)

    # 图像的灰度化
    img_gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    image_show(img_gray)

    # 图像的二值化
    [_, img_bin] = cv.threshold(img_gray, 70, 255, cv.THRESH_BINARY)
    image_show(img_bin)
举报

相关推荐

0 条评论