• OpenCV:图像的普通二值化


    首先我们来看看图像二值化的过程,opencv一共有好几种不同的二值化算法可以使用,一般来说图像的像素,亮度等条件如果超过了某个或者低于了某个阈值,就会恒等于某个值,可以用于某些物体轮廓的监测:

    导包:

    import numpy as np
    import cv2
    import matplotlib.pyplot as plt
    def show(image):
        plt.imshow(image)
        plt.axis('off')
        plt.show()
    def imread(image):
        image=cv2.imread(image)
        image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
        return image

    读入图像:

    image=imread('123.jpg')
    show(image)
    gray=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
    plt.imshow(gray,'gray')#这里需要加一个参数之后才可以显示灰度图
    plt.axis('off')
    plt.show()

    进行二值化的参数设定:

    ret1,thresh1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
    ret2,thresh2 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
    ret3,thresh3 = cv2.threshold(gray,127,255,cv2.THRESH_TRUNC)
    ret4,thresh4 = cv2.threshold(gray,127,255,cv2.THRESH_TOZERO)
    ret5,thresh5 = cv2.threshold(gray,127,255,cv2.THRESH_TOZERO_INV)

    进行二值化:

    titles = ['original','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
    images = [gray, thresh1, thresh2, thresh3, thresh4, thresh5]
    plt.figure(figsize=(15,5))
    for i in range(6):
        plt.subplot(2,3,i+1)
        plt.imshow(images[i],'gray')
        plt.title(titles[i])
        plt.axis('off')
    plt.show()
  • 相关阅读:
    golang的make
    Go的指针
    vue 修改子组件的数据$refs
    vue中异步函数async和await的用法
    redis锁
    支付宝app支付商户配置
    微信小程序中this.data与this.setData的区别详解
    jQuery动态数字翻滚计数到指定数字的文字特效代码
    中英文判断
    jQuery点击图片弹出大图遮罩层
  • 原文地址:https://www.cnblogs.com/geeksongs/p/11132606.html
Copyright © 2020-2023  润新知