• 【转载】opencv 二值化函数——cv2.threshold


    https://blog.csdn.net/weixin_38570251/article/details/82079080

    threshold:固定阈值二值化,

    ret, dst = cv2.threshold(src, thresh, maxval,type)

    src: 输入图,只能输入单通道图像,通常来说为灰度图

    dst: 输出图

    thresh: 阈值

    maxval: 当像素值超过了阈值(或者小于阈值,根据type来决定),所赋予的值

    type:二值化操作的类型,包含以下5种类型: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO;cv2.THRESH_TOZERO_INV

    官方文档的示例代码:

    importcv2

    importnumpyasnp

    frommatplotlibimportpyplotasplt

    img = cv2.imread('gradient.png',0)

    ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)

    ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)

    ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)

    ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)

    ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)

    titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']

    images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

    foriinxrange(6):

    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')

        plt.title(titles[i])

        plt.xticks([]),plt.yticks([])

    plt.show()



    作者:dopami
    链接:https://www.jianshu.com/p/a5fd69f8ad0f
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    观察者模式(Observer)
    外观模式(三层解耦)
    建造者模式(Builder)
    简单工厂
    单例模式(Winform窗体的实现)
    20180213 字符串spilt方法,字符串打包zip方法
    20180212第一发:Python与Json编码解码举例
    Eclipse插件Fat Jar
    java学习之浅谈多线程4SwingWorker
    Android SDK manager 闪退
  • 原文地址:https://www.cnblogs.com/sweeeper/p/12053141.html
Copyright © 2020-2023  润新知