您只能在图像区域上使用 Otsu 阈值。
首先,您可以通过阈值仅获得图像的前景(没有黑色背景)。然后您可以使用 Otsu 算法计算非 0 值的阈值。然后用这个值对图像进行阈值处理。
src = cv.imread(image_path)
#convert to gray scale
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
#thresholding to find only white region of image, without black backgorund
ret3, th3 = cv.threshold(gray, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
#erosion to delet some noises
cv.erode(th3, np.ones((5, 5), np.uint8), 1)
#get all values from image that are not equal 0 (are not background)
tempThresImg = gray[th3 != 0]
#get threshold value only for not black pixels
threshold, temp = cv.threshold(tempThresImg, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) # using otsu threshold
#use threshold value on whole image
ret, thresh = cv.threshold(gray, threshold, 255, cv.THRESH_BINARY)
我的结果(带有你的箭头):
阈值化后获得前景:
结果:
您可以根据算法检测到的过多或过少来更改阈值以获得结果。