图像二值化( Image Binarization)就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果的过程。在数字图像处理中,二值图像占有非常重要的地位,图像的二值化使图像中数据量大为减少,从而能凸显出目标的轮廓。

全局二值化

原图像:

OTSU方法处理结果:

Triangle结果:

从这两张图的对比可以看出,在处理人像时,Triangle方法相较与OTSU方法没有明显的优势,甚至OTSU方法要略胜一筹。在学习中了解到:Triangle方法更适合用于单波峰图像的二值化,常用于医学图像分析,于是做了如下实验:
医学细胞图像

直方图分布

OTSU结果:

Triangle方法结果:

但是如果单从这份结果来看的话,Triangle的优势还是不明显

局部二值化

mean方法结果:

Gaussion结果:

从这两张图的对比,可以认为Gaussion方法更加细腻,但并不是绝对的

自己定义的二值化方法

通过自己所设计的方法来实现二值化,其实就是计算得到一个threshold来进行二值化。
结果:

代码部分

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt


def global_binary(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    plt.hist(gray.ravel(), 256, [0, 256])
    plt.show()
    """ src:输入图像 thresh:设置阈值,当进行二值化时,大于该值的为1,小于归0 maxval:maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding types.最大像素值 type:所采用的方法 注意:当设置了自动搜索阈值的方法时(如cv.THRESH_OTSU或者cv.THRESH_TRIANGLE),手动设置的阈值将不生效 """
    thr, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    print("threshold:{}".format(thr))
    cv.imshow("binary", binary)


def local_binary(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    """ src:输入图像 maxValue:同上 adaptiveMethod:自适应方法,常用的有ADAPTIVE_THRESH_MEAN_C以及ADAPTIVE_THRESH_GAUSSIAN_C,分别为均值发育高斯均值法 The #BORDER_REPLICATE | #BORDER_ISOLATED is used to process boundaries. thresholdType: must be either #THRESH_BINARY or #THRESH_BINARY_INV,只能是二值化或INV(反二值化) blockSize:局部二值的参考块大小 C : Constant subtracted from the mean or weighted mean. Normally, it is positive but may be zero or negative as well. 对每个块,结算得到的阈值减去常数C再来进行二值化,用于减小特殊值带来的误差 """
    dst = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10)
    cv.imshow("local_binary", dst)


def custom_binary(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    h, w = gray.shape[:2]
    print("h:{}, w:{}".format(h, w))
    mean = np.sum(gray.ravel()) / (h*w)
    thr, binary = cv.threshold(gray, mean, 255, cv.THRESH_BINARY)
    print("threshold:{}".format(thr))
    cv.imshow("custom_binary", binary)


src = cv.imread("data/lena.jpg")
cell = cv.imread("data/cell.jpg")
# cv.imshow("original", cell)
# global_binary(cell)
# local_binary(src)
custom_binary(src)

cv.waitKey(0)
cv.destroyAllWindows()

本文地址:https://blog.csdn.net/weixin_43860783/article/details/110471280