具有阈值二进制逻辑的 Opencv 阈值 Otsu
Opencv Threshold Otsu with Threshold Binary Logic
在 opencv threshold page 处有这样的代码:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('noisy2.png',0)
# global thresholding
ret1,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img,(5,5),0)
ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
'Original Noisy Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in range(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()
我这里的问题是cv.THRESH_BINARY指的是0,cv.THRESH_OTSU指的是8。如果我把它们加起来0+8=8,为什么我用cv.THRESH_BINARY?我在没有 thresh 二进制文件的情况下尝试过,但我的眼睛看不出有什么不同。还有什么规则我不知道吗?
在cv.THRESH_BINARY+cv.THRESH_OTSU
中,cv.THRESH_BINARY+
是没有意义的。它只是一个占位符,因为您可以将 Otsu 与 cv.THRESH_BINARY_INV+cv.THRESH_OTSU
等其他方法结合使用。只需将 cv.THRESH_BINARY+
视为默认行为即可。此外,您不应该关心枚举的实际值:它是一个实现细节,可能会在未来的版本中发生变化。
在 opencv threshold page 处有这样的代码:
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img = cv.imread('noisy2.png',0)
# global thresholding
ret1,th1 = cv.threshold(img,127,255,cv.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv.threshold(img,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv.GaussianBlur(img,(5,5),0)
ret3,th3 = cv.threshold(blur,0,255,cv.THRESH_BINARY+cv.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1,
img, 0, th2,
blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
'Original Noisy Image','Histogram',"Otsu's Thresholding",
'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in range(3):
plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()
我这里的问题是cv.THRESH_BINARY指的是0,cv.THRESH_OTSU指的是8。如果我把它们加起来0+8=8,为什么我用cv.THRESH_BINARY?我在没有 thresh 二进制文件的情况下尝试过,但我的眼睛看不出有什么不同。还有什么规则我不知道吗?
在cv.THRESH_BINARY+cv.THRESH_OTSU
中,cv.THRESH_BINARY+
是没有意义的。它只是一个占位符,因为您可以将 Otsu 与 cv.THRESH_BINARY_INV+cv.THRESH_OTSU
等其他方法结合使用。只需将 cv.THRESH_BINARY+
视为默认行为即可。此外,您不应该关心枚举的实际值:它是一个实现细节,可能会在未来的版本中发生变化。