OpenCV-Python實戰(9)——OpenCV用于影像分割的閾值技術(含大量示例,建議收藏)
- 0. 前言
- 1. 閾值技術簡介
- 2. 簡單的閾值技術
- 2.1 閾值型別
- 2.2 簡單閾值技術的實際應用
- 3. 自適應閾值演算法
- 4. Otsu 閾值演算法
- 5. Triangle 閾值演算法
- 6. 對彩色影像進行閾值處理
- 小結
- 系列鏈接
0. 前言
影像分割是許多計算機視覺應用中的關鍵處理步驟,通常用于將影像劃分為不同的區域,這些區域常常對應于真實世界的物件,因此,影像分割是影像識別和內容分析的重要步驟,影像閾值是一種簡單、有效的影像分割方法,其中像素根據其強度值進行磁區,在本文中,將介紹 OpenCV 所提供的主要閾值技術,可以將這些技術用作計算機視覺應用程式中影像分割的關鍵部分,
1. 閾值技術簡介
閾值處理是一種簡單、有效的將影像劃分為前景和背景的方法,影像分割通常用于根據物件的某些屬性(例如,顏色、邊緣或直方圖)從背景中提取物件,最簡單的閾值方法會利用預定義常數(閾值),如果像素強度小于閾值,則用黑色像素替換,如果像素強度大于閾值,則用白色像素替換,OpenCV 提供了 cv2.threshold() 函式來對影像進行閾值處理,
為了測驗 cv2.threshold() 函式,首次創建測驗影像,其包含一些填充了不同的灰色調的大小相同的區域,利用 build_sample_image() 函式構建此測驗影像:
def build_sample_image():
"""創建填充了不同的灰色調的大小相同的區域,作為測驗影像"""
# 定義不同區域
tones = np.arange(start=50, stop=300, step=50)
# 初始化
result = np.zeros((50, 50, 3), dtype="uint8")
for tone in tones:
img = np.ones((50, 50, 3), dtype="uint8") * tone
# 沿軸連接陣列
result = np.concatenate((result, img), axis=1)
return result
接下來將使用不同的預定義閾值: 0 、 50 、 100 、 150 、 200 和 250 呼叫 cv2.threshold() 函式,以查看不同預定義閾值對閾值影像影響,例如,使用閾值 thresh = 50 對影像進行閾值處理:
ret1, thresh1 = cv2.threshold(gray_image, 50, 255, cv2.THRESH_BINARY)
其中,thresh1 是僅包含黑白色的閾值影像,源影像 gray_image 中灰色強度小于 50 的像素為黑色,強度大于 50 的像素為白色,
使用多個不同閾值對影像進行閾值處理:
# 可視化函式
def show_img_with_matplotlib(color_img, title, pos):
img_RGB = color_img[:, :, ::-1]
ax = plt.subplot(7, 1, pos)
plt.imshow(img_RGB)
plt.title(title, fontsize=8)
plt.axis('off')
# 使用 build_sample_image() 函式構建測驗影像
image = build_sample_image()
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
for i in range(6):
# 使用多個不同閾值對影像進行閾值處理
ret, thresh = cv2.threshold(gray_image, 50 * i, 255, cv2.THRESH_BINARY)
# 可視化
show_img_with_matplotlib(cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR), "threshold = {}".format(i * 50), i + 2)
# 可視化測驗影像
show_img_with_matplotlib(cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR), "img with tones of gray - left to right: (0,50,100,150,200,250)", 1)
# 影像進行閾值處理后,常見的輸出是黑白影像
# 因此,為了更好的可視化效果,修改背景顏色
fig.patch.set_facecolor('silver')
plt.show()

從上圖可以看出,根據閾值和樣本影像灰度值的不同,閾值處理后生成的黑白影像的變化情況,
2. 簡單的閾值技術
上一節中,我們已經簡單介紹過了 OpenCV 中提供的簡單閾值處理函式——cv2.threshold(),該函式用法如下:
cv2.threshold(src, thresh, maxval, type, dst=None) -> retval, dst
cv2.threshold() 函式對 src 輸入陣列(可以為單通道或多通道影像)應用預定義常數 thresh 設定的閾值;type 引數用于設定閾值型別,閾值型別的可選值如下:cv2.THRESH_BINARY、cv2.THRESH_BINARY_INV、cv2.THRESH_TRUNC、cv2.THRESH_TOZERO、cv2.THRESH_TOZERO_INV、cv2.THRESH_OTSU 和 cv2.THRESH_TRIANGLE,
maxval 引數用于設定最大值,其僅在閾值型別為 cv2.THRESH_BINARY 和 cv2.THRESH_BINARY_INV 時有效;需要注意的是,在閾值型別為 cv2.THRESH_OTSU 和 cv2.THRESH_TRIANGLE 時,輸入影像 src 應為為單通道,
2.1 閾值型別
為了更好的了解閾值操作的不同型別,接下來給出每種閾值型別的具體公式,符號說明:src 是源(原始)影像,dst 對應于閾值化后的目標(結果)影像,因此,src(x, y) 對應于源影像像素 (x, y) 處的強度,而 dst(x, y) 對應于目標影像像素 (x, y) 處的強度,
閾值型別 cv2.THRESH_BINARY 公式如下:
d
s
t
(
x
,
y
)
=
{
m
a
x
v
a
l
,
s
r
c
(
x
,
y
)
>
t
h
r
e
s
h
0
,
s
r
c
(
x
,
y
)
≤
t
h
r
e
s
h
dst(x,y) = \begin{cases} maxval, & src(x,y)>thresh \\[2ex] 0, & src(x,y)\leq thresh \end{cases}
dst(x,y)=????maxval,0,?src(x,y)>threshsrc(x,y)≤thresh?
其表示,如果像素 src(x, y) 的強度高于 thresh,則目標影像像素強度 dst(x,y) 將被設為 maxval;否則,設為 0,
閾值型別 cv2.THRESH_BINARY_INV 公式如下:
d
s
t
(
x
,
y
)
=
{
0
,
s
r
c
(
x
,
y
)
>
t
h
r
e
s
h
m
a
x
v
a
l
,
s
r
c
(
x
,
y
)
≤
t
h
r
e
s
h
dst(x,y) = \begin{cases} 0, & src(x,y)>thresh \\[2ex] maxval, & src(x,y)\leq thresh \end{cases}
dst(x,y)=????0,maxval,?src(x,y)>threshsrc(x,y)≤thresh?
其表示,如果像素 src(x, y) 的強度高于 thresh,則目標影像像素強度 dst(x,y) 將被設為 0;否則,設為 maxval,
閾值型別 cv2.THRESH_TRUNC 公式如下:
d
s
t
(
x
,
y
)
=
{
t
h
r
e
s
h
o
l
d
,
s
r
c
(
x
,
y
)
>
t
h
r
e
s
h
s
r
c
(
x
,
y
)
,
s
r
c
(
x
,
y
)
≤
t
h
r
e
s
h
dst(x,y) = \begin{cases} threshold, & src(x,y)>thresh \\[2ex] src(x,y), & src(x,y)\leq thresh \end{cases}
dst(x,y)=????threshold,src(x,y),?src(x,y)>threshsrc(x,y)≤thresh?
其表示,如果像素 src(x, y) 的強度高于 thresh,則目標影像像素強度設定為 threshold;否則,設為 src(x, y),
閾值型別 cv2.THRESH_TOZERO 公式如下:
d
s
t
(
x
,
y
)
=
{
s
r
c
(
x
,
y
)
,
s
r
c
(
x
,
y
)
>
t
h
r
e
s
h
0
,
s
r
c
(
x
,
y
)
≤
t
h
r
e
s
h
dst(x,y) = \begin{cases} src(x,y), & src(x,y)>thresh \\[2ex] 0, & src(x,y)\leq thresh \end{cases}
dst(x,y)=????src(x,y),0,?src(x,y)>threshsrc(x,y)≤thresh?
其表示,如果像素 src(x, y) 的強度高于 thresh,則目標影像像素值將設定為 src(x, y);否則,設定為 0 ,
閾值型別 cv2.THRESH_TOZERO_INV 公式如下:
d
s
t
(
x
,
y
)
=
{
0
,
s
r
c
(
x
,
y
)
>
t
h
r
e
s
h
s
r
c
(
x
,
y
)
,
s
r
c
(
x
,
y
)
≤
t
h
r
e
s
h
dst(x,y) = \begin{cases} 0, & src(x,y)>thresh \\[2ex] src(x,y), & src(x,y)\leq thresh \end{cases}
dst(x,y)=????0,src(x,y),?src(x,y)>threshsrc(x,y)≤thresh?
其表示,如果像素 src(x, y) 的強度大于 thresh,則目標影像像素值將設定為 0;否則,設定為 src(x, y),
而 cv2.THRESH_OTSU 和 cv2.THRESH_TRIANGLE 屬于特殊的閾值型別,它們可以與上述閾值型別( cv2.THRESH_BINARY、cv2.THRESH_BINARY_INV、cv2.THRESH_TRUNC、cv2.THRESH_TOZERO 和 cv2.THRESH_TOZERO_INV)進行組合,組合后,閾值處理函式 cv2.threshold() 將只能處理單通道影像,且計算并回傳最佳閾值,而非指定閾值,
接下來使用不同閾值型別對同樣的測驗影像進行閾值處理,觀察不同閾值處理效果:
ret1, thresh1 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY)
ret2, thresh2 = cv2.threshold(gray_image, 100, 220, cv2.THRESH_BINARY)
ret3, thresh3 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_BINARY_INV)
ret4, thresh4 = cv2.threshold(gray_image, 100, 220, cv2.THRESH_BINARY_INV)
ret5, thresh5 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_TRUNC)
ret6, thresh6 = cv2.threshold(gray_image, 100, 255, cv2.THRESH_TOZERO)
ret7, thresh7 = cv2.threshold(gray_image,100,255, cv2.THRESH_TOZERO_INV)
# 可視化
show_img_with_matplotlib(cv2.cvtColor(thresh1, cv2.COLOR_GRAY2BGR), "THRESH_BINARY - thresh = 100 & maxValue = 255", 2)
show_img_with_matplotlib(cv2.cvtColor(thresh2, cv2.COLOR_GRAY2BGR), "THRESH_BINARY - thresh = 100 & maxValue = 220", 3)
show_img_with_matplotlib(cv2.cvtColor(thresh3, cv2.COLOR_GRAY2BGR), "THRESH_BINARY_INV - thresh = 100", 4)
# 其他影像可視化方法類似,不再贅述
# ...

如上圖所示,maxval 引數僅在使用 cv2.THRESH_BINARY 和 cv2.THRESH_BINARY_INV 閾值型別時有效,上例中將 cv2.THRESH_BINARY 和 cv2.THRESH_BINARY_INV 型別的 maxval 值設定為 255 及 220,以便查看閾值影像在這兩種情況下的變化情況,
2.2 簡單閾值技術的實際應用
了解 cv2.threshold() 不同引數的作業原理后,我們將 cv2.threshold() 應用于真實影像,并使用不同的閾值:
# 加載影像
image = cv2.imread('example.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 繪制灰度影像
show_img_with_matplotlib(cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR), "img", 1)
# 使用不同的閾值呼叫 cv2.threshold() 并進行可視化
for i in range(8):
ret, thresh = cv2.threshold(gray_image, 130 + i * 10, 255, cv2.THRESH_BINARY)
show_img_with_matplotlib(cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR), "threshold = {}".format(130 + i * 10), i + 2)

如上圖所示,閾值在使用 cv2.threshold() 對影像進行閾值處理時起著至關重要的作用,假設影像處理演算法用于識別影像中的物件,如果閾值較低,則閾值影像中缺少一些資訊,而如果閾值較高,則有一些資訊會被黑色像素遮擋,因此,為整個影像找到一個全域最優閾值是相當困難的,特別是如果影像受到不同光照條件的影響,找到全域最優閾值幾乎是不可能的,這就是為什么我們需要應用其他自適應閾值演算法來對影像進行閾值處理的原因,
3. 自適應閾值演算法
當由于影像不同區域的光照條件不同時,為了獲取更好的閾值結果,可以嘗試使用自適應閾值,在 OpenCV 中,自適應閾值由 cv2.adapativeThreshold() 函式實作,其用法如下:
adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C[, dst]) -> dst
此函式將自適應閾值應用于 src 陣列( 8 位單通道影像),maxValue 引數用于設定 dst 影像中滿足條件的像素值,adaptiveMethod 引數設定要使用的自適應閾值演算法,有以下兩個可選值:
cv2.ADAPTIVE_THRESH_MEAN_C:T(x, y)閾值為(x, y)的blockSize x blockSize鄰域的平均值減去引數Ccv2.ADAPTIVE_THRESH_GAUSSIAN_C:T(x, y)閾值為(x, y)的blockSize x blockSize鄰域的加權均值減去引數C
blockSize 引數用于計算像素閾值的鄰域區域的大小,C 引數是均值或加權均值需要減去的常數(取決于由 adaptiveMethod 引數設定的自適應方法),thresholdType 引數可選值包括 cv2.THRESH_BINARY 和 cv2.THRESH_BINARY_INV,
綜上所屬,cv2.adapativeThreshold() 函式的計算公式如下,其中 T(x, y) 是為每個像素計算的閾值:
當 thresholdType 引數為 cv2.THRESH_BINARY 時:
d
s
t
(
x
,
y
)
=
{
m
a
x
v
a
l
,
s
r
c
(
x
,
y
)
>
T
(
x
,
y
)
0
,
s
r
c
(
x
,
y
)
≤
t
h
r
e
s
h
dst(x,y) = \begin{cases} maxval, & src(x,y)>T(x,y) \\[2ex] 0, & src(x,y)\leq thresh \end{cases}
dst(x,y)=????maxval,0,?src(x,y)>T(x,y)src(x,y)≤thresh?
當thresholdType 引數為 cv2.THRESH_BINARY_INV 時:
d
s
t
(
x
,
y
)
=
{
0
,
s
r
c
(
x
,
y
)
>
T
(
x
,
y
)
m
a
x
v
a
l
,
s
r
c
(
x
,
y
)
≤
t
h
r
e
s
h
dst(x,y) = \begin{cases} 0, & src(x,y)>T(x,y) \\[2ex] maxval, & src(x,y)\leq thresh \end{cases}
dst(x,y)=????0,maxval,?src(x,y)>T(x,y)src(x,y)≤thresh?
應用不同自適應閾值方法,進行影像處理:
# 加載影像
image = cv2.imread('example.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 應用自適應閾值處理
thresh1 = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
thresh2 = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 31, 3)
thresh3 = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
thresh4 = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 3)
# 可視化
show_img_with_matplotlib(cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR), "gray img", 1)
show_img_with_matplotlib(cv2.cvtColor(thresh1, cv2.COLOR_GRAY2BGR), "method=THRESH_MEAN_C, blockSize=11, C=2", 2)
# 其他影像可視化方法類似,不再贅述
# ...

在上圖中,可以看到應用具有不同引數的 cv2.adaptiveThreshold() 處理后的輸出,自適應閾值可以提供更好的閾值影像,但是,影像中也會出現了很多噪點,為了減少噪點,可以應用一些平滑操作,例如雙邊濾波,因為它會在去除噪聲的同時保持銳利邊緣,因此,我們在對影像進行閾值處理之前首先應用 OpenCV 提供的雙邊濾波函式 cv2.bilateralFilter():
# 加載影像
image = cv2.imread('example.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 雙邊濾波
gray_image = cv2.bilateralFilter(gray_image, 15, 25, 25)
# 應用自適應閾值處理
thresh1 = cv2.adaptiveThreshold(gray_image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
# 之后代碼與上一示例想同,不再贅述
# ...

從上圖可以看到,應用平滑濾波器后,噪聲數量大幅減少,
4. Otsu 閾值演算法
簡單閾值演算法應用同一全域閾值,因此我們需要嘗試不同的閾值并查看閾值影像,以滿足我們的需要,但是,這種方法需要大量嘗試,一種改進的方法是使用 OpenCV 中的 cv2.adapativeThreshold() 函式計算自適應閾值,但是,計算自適應閾值需要兩個合適的引數:blockSize 和 C;另一種改進方法是使用 Otsu 閾值演算法,這在處理雙峰影像時是非常有效(雙峰影像其直方圖包含兩個峰值), Otsu 演算法通過最大化兩類像素之間的方差來自動計算將兩個峰值分開的最佳閾值,其等效于最佳閾值最小化類內方差,Otsu 閾值演算法是一種統計方法,因為它依賴于從直方圖獲得的統計資訊(例如,均值、方差或熵),在 OpenCV 中使用 cv2.threshold() 函式計算 Otsu 閾值的方法如下:
ret, th = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
使用 Otsu 閾值演算法,不需要設定閾值,因為 Otsu 閾值演算法會計算最佳閾值,因此引數 thresh = 0,cv2.THRESH_OTSU 標志表示將應用 Otsu 演算法,此標志可以與 cv2.THRESH_BINARY、cv2.THRESH_BINARY_INV、cv2.THRESH_TRUNC、cv2.THRESH_TOZERO 以及 cv2.THRESH_TOZERO_INV 結合使用,函式回傳閾值影像 th 和閾值 ret,
將此演算法應用于實際影像,并繪制一條線可視化閾值,確定閾值 th 坐標:
def show_hist_with_matplotlib_gray(hist, title, pos, color, t=-1):
ax = plt.subplot(2, 2, pos)
plt.xlabel("bins")
plt.ylabel("number of pixels")
plt.xlim([0, 256])
# 可視化閾值
plt.axvline(x=t, color='m', linestyle='--')
plt.plot(hist, color=color)
# 加載影像
image = cv2.imread('example.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 直方圖
hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
# otsu 閾值演算法
ret1, th1 = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# 可視化
show_img_with_matplotlib(image, "image", 1)
show_img_with_matplotlib(cv2.cvtColor(gray_image, cv2.COLOR_GRAY2BGR), "gray img", 2)
show_hist_with_matplotlib_gray(hist, "grayscale histogram", 3, 'm', ret1)
show_img_with_matplotlib(cv2.cvtColor(th1, cv2.COLOR_GRAY2BGR), "Otsu's binarization", 4)
plt.show()

在上圖圖中,可以看到源影像中沒有噪聲,因此演算法可以正常作業,接下來,我們手動為影像添加噪聲,以觀察噪聲對 Otsu 閾值演算法的影響,然后利用高斯濾波消除部分噪聲,以查看閾值影像變化情況:
import numpy as np
def gasuss_noise(image, mean=0, var=0.001):
'''
添加高斯噪聲
mean : 均值
var : 方差
'''
image = np.array(image/255, dtype=float)
noise = np.random.normal(mean, var ** 0.5, image.shape)
out = image + noise
if out.min() < 0:
low_clip = -1.
else:
low_clip = 0.
out = np.clip(out, low_clip, 1.0)
out = np.uint8(out*255)
return out
# 加載影像、添加噪聲,并將其轉換為灰度影像
image = cv2.imread('903183h98p0.png')
image = gasuss_noise(image,var=0.05)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 計算直方圖
hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
# 應用 Otsu 閾值演算法
ret1, th1 = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
# 高斯濾波
gray_image_blurred = cv2.GaussianBlur(gray_image, (25, 25), 0)
# 計算直方圖
hist2 = cv2.calcHist([gray_image_blurred], [0], None, [256], [0, 256])
# 高斯濾波后的影像,應用 Otsu 閾值演算法
ret2, th2 = cv2.threshold(gray_image_blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

如上圖所示,如果不應用平滑濾波,閾值影像中也充滿了噪聲,應用高斯濾波后可以正確過濾掉噪聲,同時可以看到濾波后得到的影像是雙峰的,
5. Triangle 閾值演算法
還有另一種自動閾值演算法稱為 Triangle 閾值演算法,它是一種基于形狀的方法,因為它分析直方圖的結構或形狀(例如,谷、峰和其他直方圖形狀特征),該演算法可以分為三步:第一步,計算直方圖灰度軸上最大值
b
m
a
x
b_{max}
bmax? 和灰度軸上最小值
b
m
i
n
b_{min}
bmin? 之間的直線;第二步,計算
b
[
b
m
i
n
,
b
m
a
x
]
b [b_{min}, b_{max}]
b[bmin?,bmax?] 范圍內直線(在第一步中計算)到直方圖的距離;最后,選擇直方圖與直線距離最大的水平作為閾值,
OpenCV 中 Triangle 閾值演算法的使用方式與 Otsu 的演算法非常相似,只需要將 cv2.THRESH_OTSU 標志修改為 cv2.THRESH_TRIANGLE:
# 加載影像
image = cv2.imread('example.png')
image = gasuss_noise(image,var=0.05)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
ret1, th1 = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_TRIANGLE)
# 高斯濾波
gray_image_blurred = cv2.GaussianBlur(gray_image, (25, 25), 0)
hist2 = cv2.calcHist([gray_image_blurred], [0], None, [256], [0, 256])
ret2, th2 = cv2.threshold(gray_image_blurred, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_TRIANGLE)
在下圖中,可以看到將 Triangle 閾值演算法應用于噪聲影像時的輸出:

6. 對彩色影像進行閾值處理
cv2.threshold() 函式也可以應用于多通道影像,在此情況下, cv2.threshold() 函式在 BGR 影像的每個通道中應用閾值操作:
image = cv2.imread('example.png')
ret1, thresh1 = cv2.threshold(image, 150, 255, cv2.THRESH_BINARY)
這與在每個通道中應用 cv2.threshold() 函式并合并閾值通道相同的結果:
(b, g, r) = cv2.split(image)
ret2, thresh2 = cv2.threshold(b, 150, 255, cv2.THRESH_BINARY)
ret3, thresh3 = cv2.threshold(g, 150, 255, cv2.THRESH_BINARY)
ret4, thresh4 = cv2.threshold(r, 150, 255, cv2.THRESH_BINARY)
bgr_thresh = cv2.merge((thresh2, thresh3, thresh4))
為了進行對比,我們同時使用其他閾值型別對彩色影像進行閾值處理:
ret5, thresh5 = cv2.threshold(image, 120, 255, cv2.THRESH_TOZERO)
# Apply cv2.threshold():
(b, g, r) = cv2.split(image)
ret2, thresh2 = cv2.threshold(b, 120, 255, cv2.THRESH_TOZERO)
ret3, thresh3 = cv2.threshold(g, 120, 255, cv2.THRESH_TOZERO)
ret4, thresh4 = cv2.threshold(r, 120, 255, cv2.THRESH_TOZERO)
bgr_thresh = cv2.merge((thresh2, thresh3, thresh4))


小結
閾值技術可用于許多計算機視覺任務(例如,文本識別和影像分割等),在本文中,我們介紹了可用于對影像進行閾值處理的主要閾值技術,介紹了簡單和自適應閾值技術,同時也了解了如何應用 Otsu 閾值演算法和triangle 閾值演算法來自動選擇一個全域閾值來對影像進行閾值處理,
系列鏈接
OpenCV-Python實戰(1)——OpenCV簡介與影像處理基礎(內含大量示例,📕建議收藏📕)
OpenCV-Python實戰(2)——影像與視頻檔案的處理(兩萬字詳解,?📕建議收藏📕)
OpenCV-Python實戰(3)——OpenCV中繪制圖形與文本(萬字總結,?📕建議收藏📕)
OpenCV-Python實戰(4)——OpenCV常見影像處理技術(??萬字長文,含大量示例??)
OpenCV-Python實戰(5)——OpenCV影像運算(??萬字長文,含大量示例??)
OpenCV-Python實戰(6)——OpenCV中的色彩空間和色彩映射(??萬字長文,含大量示例??)
OpenCV-Python實戰(7)——直方圖詳解(??含大量示例,建議收藏??)
OpenCV-Python實戰(8)——直方圖均衡化(??萬字長文,含大量示例??)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/303885.html
標籤:python
