Python使用OpenCV的阈值技术Set-2(自适应阈值)

先决条件: 使用OpenCV的简单阈值

在里面以前的帖子, 使用不同类型的阈值技术解释了简单阈值。另一种阈值技术是自适应阈值。在”简单阈值”中, 使用了全局阈值, 该全局值始终保持不变。因此, 恒定的阈值对于不同区域的光照条件变化无济于事。自适应阈值化是针对较小区域计算阈值的方法。对于照明的变化, 这导致针对不同区域的不同阈值。我们用cv2.adaptiveThreshold为了这。

语法:cv2.adaptiveThreshold(source, maxVal, 自适应方法, thresholdType, 块大小, 常数)
参数:
->源:输入图像数组(单通道, 8位或浮点数)
-> maxVal:可以分配的最大值一个像素。
-> adaptiveMethod:自适应方法决定如何计算阈值。 cv2.ADAPTIVE_THRESH_MEAN_C:阈值=(邻域值的平均值–恒定值)。换句话说, 它是一个点的blockSize×blockSize邻域的平均值减去常数。 cv2.ADAPTIVE_THRESH_GAUSSIAN_C:阈值=(邻域值的高斯加权和–常数)。换句话说, 它是点减去常量的blockSize×blockSize邻域的加权和。
-> thresholdType:要应用的阈值类型。
-> blockSize:用于计算阈值的像素邻域的大小。
->常数:从附近像素的平均值或加权和中减去的常数值。

以下是Python实现:

# Python program to illustrate 

# adaptive thresholding type on an image

# organizing imports

import cv2

import numpy as np

# path to input image is specified and

# image is loaded with imread command

image1 = cv2.imread( 'input1.jpg' )

# cv2.cvtColor is applied over the

# image input with applied parameters

# to convert the image in grayscale

img = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)

# applying different thresholding

# techniques on the input image

thresh1 = cv2.adaptiveThreshold(img, 255 , cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 199 , 5 )

thresh2 = cv2.adaptiveThreshold(img, 255 , cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 199 , 5 )

# the window showing output images

# with the corresponding thresholding

# techniques applied to the input image

cv2.imshow( 'Adaptive Mean' , thresh1)

cv2.imshow( 'Adaptive Gaussian' , thresh2)

# De-allocate any associated memory usage

if cv2.waitKey( 0 ) & 0xff = = 27 :

cv2.destroyAllWindows()

输入图像:

Python使用OpenCV的阈值技术Set-2(自适应阈值)1

输出如下:

Python使用OpenCV的阈值技术Set-2(自适应阈值)2

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

以上是 Python使用OpenCV的阈值技术Set-2(自适应阈值) 的全部内容, 来源链接: utcz.com/p/204286.html

回到顶部