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


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

被折叠的 条评论
为什么被折叠?



