0
点赞
收藏
分享

微信扫一扫

阈值处理——threshold和 adaptiveThreshold

东林梁 2022-03-30 阅读 75

在这里插入图片描述

一、阈值处理理论
阈值处理指剔除图像内像素值高于一定值或低于一定值的像素点。如设阈值为127,然后:

  • 将图像内所有像素值大于127的像素点的值设为255.
  • 将图像内所有像素值小于127的像素点的值设为0.

二、普通阈值

def threshold_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    # 二值化阈值THRESH_BINARY:对于灰度值大于阈值thresh的像素点,将其灰度值设定为最大值;对于灰度值小于或等于阈值thresh的像素点,将其灰度值设定为0
    # 反二值化阈值THRESH_BINARY_INV:对于灰度值大于阈值thresh的像素点,将其灰度值设定为0;对于灰度值小于或等于阈值thresh的像素点,将其灰度值设定为255
    # 截断阈值化THRESH_TRUNC:对于像素值大于127的像素点,将其像素值设定为127;对于像素值小于或等于127的像素点,将其像素值将保持不变
    # THRESH_OTSU方法:遍历当前图像所有可能阈值,从而找到最佳的阈值
    """
    threshold(src, thresh, maxval, type, dst=None):进行阈值化处理
        src:要进行阈值分割的图像,可以是多通道的,8位或32位浮点型数值
        thresh:设定的阈值
        maxval:当type为THRESH_BINARY或 THRESH_BINARY_INV 类型时,需要设定的最大值
        type:阈值要分割的类型
        ret:返回的阈值
        dst:阈值分割结果图像,与原始图像具有相同大小和类型
    """
    ret, binary = cv.threshold(gray, 127, 255, cv.THRESH_BINARY|cv.THRESH_OTSU)
    print('threshold value: %s' % ret)
    cv.imshow('threshold_demo', binary)

在这里插入图片描述

三、自适应阈值

def local_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    """
    adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None)
        src:处理的原始图像,必须是8位单通道的图像
        maxValue:最大值
        adaptiveMethod:自适应方法,逐个像素地计算自适应阈值。自适应阈值等于每个像素由参数blockSize所指定的加权平均值减去常量C
        thresholdType:阈值处理方式,必须是THRESH_BINARY或 THRESH_BINARY_INV 中的一个
        blockSize:块大小,表示一个像素在计算其阈值时所使用的领域尺寸,通常为3、5、7奇数等
        C:常量
    """

    # 自适应阈值ADAPTIVE_THRESH_MEAN_C:邻域所有像素点的权重值是一致的
    # 高斯阈值ADAPTIVE_THRESH_GAUSSIAN_C:与邻域各个像素点到中心点的距离有关,通过高斯方程得到各个点的权重值。
    binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10)
    cv.imshow('local_demo', binary)

在这里插入图片描述

四、自定义阈值

def custom_threshold(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    h, w = gray.shape[:2]
    # 变成一位数组
    m = np.reshape(gray, [1, w*h])
    print('一维数组m:', m)
    mean = m.sum()/(w*h+6)
    print('mean:', mean)
    # 分割
    ret, binary = cv.threshold(gray, mean, 255, cv.THRESH_BINARY)
    cv.imshow('custom_threshold', binary)

在这里插入图片描述
五、超大图像普通阈值

def big_qimage_binary(image):
    print(image.shape)
    # 一、分块,指定宽高
    cw = 256
    ch = 256
    h, w = image.shape[:2]
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    for row in range(0, h, ch):
        for col in range(0, w, cw):
            # 图像roi
            roi = gray[row:row + ch, col:cw + col]
            print(np.std(roi), np.mean(roi))
            ret, dst = cv.threshold(roi, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
            # 空白图像过滤
            gray[row:row + ch, col:cw + col] = dst
            print(np.std(dst), np.mean(dst))
            print()
    cv.imwrite('D:/OpenCV/opencv-python/yang1.jpg', gray)
    cv.imshow('big_qimage_binary', gray)

在这里插入图片描述

在这里插入图片描述
六、超大图像自适应阈值

def big_jimage_binary(image):
    print(image.shape)
    # 一、分块,指定宽高
    cw = 256
    ch = 256
    h, w = image.shape[:2]
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    for row in range(0, h, ch):
        for col in range(0, w, cw):
            roi = gray[row:row + ch, col:cw + col]
            dst = cv.adaptiveThreshold(roi, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 127, 20)
            gray[row:row + ch, col:cw + col] = dst
            print(np.std(dst), np.mean(dst))
            print()
    cv.imwrite('D:/OpenCV/opencv-python/yang2.jpg', gray)
    cv.imshow('big_jimage_binary', gray)

在这里插入图片描述

在这里插入图片描述
七、超大图像自定义形式

def big_zimage_binary(image):
    print(image.shape)
    cw = 256
    ch = 256
    h, w = image.shape[:2]
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    for row in range(0, h, ch):
        for col in range(0, w, cw):
            roi = gray[row:row + ch, col:cw + col]
            # np.std(roi):计算沿指定轴的标准差。返回数组元素的标准差
            dev = np.std(roi)
            if dev < 15:
                gray[row:row + ch, col:cw + col] = 255
            else:
                # dst = cv.adaptiveThreshold(roi, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 127, 20)
                ret, dst = cv.threshold(roi, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
                gray[row:row + ch, col:cw + col] = dst
            print(np.std(dst), np.mean(dst))
            print()
    cv.imwrite('D:/OpenCV/opencv-python/yang4.jpg', gray)
    cv.imshow('big_zimage_binary', gray)

在这里插入图片描述

在这里插入图片描述

举报

相关推荐

0 条评论