python opencv cv2.threshold() (将固定级别的阈值应用于每个数组元素)ThresholdTypes 最大类间方差法(大津法,OTSU)

本文深入探讨了OpenCV中的cv2.threshold()函数,详细解释了如何应用固定级别阈值到数组元素,以及如何从灰度图像中获取二值图像。文章涵盖了不同类型的阈值处理,包括使用Otsu和Triangle算法自动确定最佳阈值的方法。

cv2.threshold()

def threshold(src, thresh, maxval, type, dst=None): # real signature unknown; restored from __doc__
    """
    threshold(src, thresh, maxval, type[, dst]) -> retval, dst
    .   @brief Applies a fixed-level threshold to each array element.
    	将固定级别的阈值应用于每个数组元素。
    .   
    .   The function applies fixed-level thresholding to a multiple-channel array. The function is typically
    .   used to get a bi-level (binary) image out of a grayscale image ( #compare could be also used for
    .   this purpose) or for removing a noise, that is, filtering out pixels with too small or too large
    .   values. There are several types of thresholding supported by the function. They are determined by
    .   type parameter.
    	该功能将固定级别的阈值应用于多通道阵列。 
    	该函数通常用于从灰度图像中获取双(二进制)图像(#compare也可用于此目的)或用于去除噪声,即滤除具有太小或太大值的像素 。 该功能支持几种类型的阈值处理。 它们由类型参数确定。
    .   
    .   Also, the special values #THRESH_OTSU or #THRESH_TRIANGLE may be combined with one of the
    .   above values. In these cases, the function determines the optimal threshold value using the Otsu's
    .   or Triangle algorithm and uses it instead of the specified thresh.
		此外,特殊值#THRESH_OTSU或#THRESH_TRIANGLE可以与上述值之一组合。 
		在这些情况下,该函数使用Otsu或Triangle算法确定最佳阈值,并使用该阈值代替指定的阈值。
    .   
    .   @note Currently, the Otsu's and Triangle methods are implemented only for 8-bit single-channel images.
    	当前,仅对8位单通道图像实施Otsu和Triangle方法。
    .   
    .   @param src input array (multiple-channel, 8-bit or 32-bit floating point).
    	输入数组(多通道,8位或32位浮点)。
    	
    .   @param dst output array of the same size  
    	and type and the same number of channels as src.	
    	与src具有相同大小,类型和相同通道数的输出数组。
    	
    .   @param thresh threshold value. 阈值。
    .   @param maxval maximum value to use with the #THRESH_BINARY and #THRESH_BINARY_INV thresholding
    .   types.
		#THRESH_BINARY和#THRESH_BINARY_INV阈值类型使用的最大值。
		
    .   @param type thresholding type (see #ThresholdTypes).
    	阈值类型(请参阅#ThresholdTypes)。
    	
    .   @return the computed threshold value if Otsu's or Triangle methods used.
    	如果使用Otsu或Triangle方法,则返回计算出的阈值。
    .   
    .   @sa  adaptiveThreshold, findContours, compare, min, max
    """
    pass

函数示例

注:cv.threshold()函数有两个返回值,第一个返回值ret,比如用大津法(cv.THRESH_OTSU)的时候,可以查看它自动计算得出的二值化的阈值。

# -*- coding: utf-8 -*-
"""
@File    : 191213_测试_阈值分割.py
@Time    : 2019/12/13 15:14
@Author  : Dontla
@Email   : sxana@qq.com
@Software: PyCharm
"""
import cv2 as cv
import numpy as np

# 【读取图像】
image = cv.imread('feiji.jpg')
# 【将图像灰度化】
image = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
# print(image.dtype)  # uint8
# 图像分割:阈值分割法
ret, thresh = cv.threshold(image, 120, 255, cv.THRESH_BINARY)
# print(ret) # 120.0
# 【显示图像】
cv.imshow('win', thresh)
cv.waitKey(0)

结果:
在这里插入图片描述

cv::ThresholdTypes

#include <opencv2/imgproc.hpp>

type of the threshold operation
在这里插入图片描述
在这里插入图片描述
Enumerator
THRESH_BINARY
Python: cv.THRESH_BINARY
dst(x,y)={maxval0if src(x,y)>threshotherwise
THRESH_BINARY_INV
Python: cv.THRESH_BINARY_INV
dst(x,y)={0maxvalif src(x,y)>threshotherwise
THRESH_TRUNC
Python: cv.THRESH_TRUNC
dst(x,y)={thresholdsrc(x,y)if src(x,y)>threshotherwise
THRESH_TOZERO
Python: cv.THRESH_TOZERO
dst(x,y)={src(x,y)0if src(x,y)>threshotherwise
THRESH_TOZERO_INV
Python: cv.THRESH_TOZERO_INV
dst(x,y)={0src(x,y)if src(x,y)>threshotherwise
THRESH_MASK
Python: cv.THRESH_MASK
THRESH_OTSU
Python: cv.THRESH_OTSU
flag, use Otsu algorithm to choose the optimal threshold value

THRESH_TRIANGLE
Python: cv.THRESH_TRIANGLE
flag, use Triangle algorithm to choose the optimal threshold value

Function Documentation

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dontla

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值