opencv二值图像分割——python

1. 二值图像

图像二值化( Image Binarization)就是将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果的过程。

2. 全局阈值

cv2.threshold(src, thresholdValue, maxVal, thresholdingTechnique)
src:输入灰度图像数组。
thresholdValue:提及用于对像素值进行分类的值。
maxVal:如果像素值大于(有时小于)阈值,则给出的值。
thresholdingTechnique:要应用的阈值类型。
有5种不同的简单阈值技术是:

cv2.THRESH_BINARY:如果像素强度大于设置的阈值,则将值设置为 255,否则设置为 0(黑色)。
cv2.THRESH_BINARY_INV:cv2 的反转或相反情况。
cv2.THRESH_TRUNC:如果像素强度值大于阈值,则会将其截断为阈值。像素值设置为与阈值相同。所有其他值保持不变。
cv2.THRESH_TOZERO:像素强度设置为 0,对于所有像素强度,小于阈值。
cv2.THRESH_TOZERO_INV:cv2 的反转或相反情况。

import cv2
 
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "C:\\Users\\user\\Desktop\\flower.jpg"
 
    # read/load an image in grayscale mode
    grey_img = cv2.imread(img_path,0)
 
    # show the Input image on the newly created image window
    cv2.imshow('Input',grey_img)
 
    # applying cv2.THRESH_BINARY thresholding techniques
    ret, thresh_img = cv2.threshold(grey_img, 128, 255, cv2.THRESH_BINARY)
 
    # show the Output image on the newly created image window
    cv2.imshow('Output',thresh_img)

在这里插入图片描述
ret, th= cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
该方法也被称为大津法(OTSU)它是根据图像灰度分布,自动选择最佳的阈值,按照大津法求得的阈值进行图像二值化分割后,前景与背景图像的类间方差最大。适合处理所需提取的前景图像和背景图像差距较大的图像。其函数也十分简单,只需要把阈值thresh设置为0,然后设置type为cv2.THRESH_BINARY+cv2.THRESH_OTSU,会自动返回一个合适的阈值。

import cv2
 
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "./image-129.png"
 
    # read/load an image in grayscale mode
    gray_img = cv2.imread(img_path, 0)
 
    # show the Input image on the newly created image window
    print(gray_img.shape)
    cv2.imshow('Input', gray_img)
    cv2.namedWindow("Input", 0)
    cv2.waitKey(0)
 
    # applying cv2.THRESH_BINARY thresholding techniques
    ret, thresh_img= cv2.threshold(gray_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
 
    # show the Output image on the newly created image window
    
    cv2.imshow('Output',thresh_img)
    cv2.namedWindow("Output", 0)
    cv2.waitKey(0)
    cv2.imwrite('thresh.png', thresh_img)

在这里插入图片描述

3. 局部阈值

彩色图像色彩分布不均匀,使用全局阈值处理处理效果较差,通常采用局部阈值来进行分割,局部阈值的处理原理是,针对每一个像素点专门配置一个阈值来进行处理,这些阈值就构成了和原图像维度相同的矩阵。

cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C, dst=None)
src:灰度化的图片
maxValue:满足条件的像素点需要设置的灰度值
adaptiveMethod:自适应方法。有2种分别是均值和高斯加权:ADAPTIVE_THRESH_MEAN_C 或 ADAPTIVE_THRESH_GAUSSIAN_C,建议使用高斯加权和。
thresholdType:二值化方法,可以设置为THRESH_BINARY或者THRESH_BINARY_INV
blockSize:分割计算的区域大小,取奇数
C:常数,每个区域计算出的阈值的基础上在减去这个常数作为这个区域的最终阈值,可以为负数
dst:输出图像,可选

import cv2
 
if __name__ == "__main__" :
 
    # mentioning absolute path of the image
    img_path = "./image-129.png"
 
    # read/load an image in grayscale mode
    # gray_img = cv2.imread(img_path, 0)
    color_img = cv2.imread(img_path)
    gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)
 
    # show the Input image on the newly created image window
    print(gray_img.shape)
    cv2.imshow('Input', gray_img)
    cv2.namedWindow("Input", 0)
    cv2.waitKey(0)
 
    # applying cv2.THRESH_BINARY thresholding techniques
    thresh_img = cv2.adaptiveThreshold(gray_img, 127, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, -30)
 
    # show the Output image on the newly created image window
    
    cv2.imshow('Output',thresh_img)
    cv2.namedWindow("Output", 0)
    cv2.waitKey(0)
    cv2.imwrite('thresh.png', thresh_img)

在这里插入图片描述

4. 使用建议

灰度图像使用全局阈值分割:大津法 > 普通全局阈值
彩色图像使用局部阈值分割

猜你喜欢

转载自blog.csdn.net/CharmsLUO/article/details/123398938
今日推荐