import cv2
import matplotlib.pyplot as plt
img = cv2.imread("Fig2.png", 0)
img = cv2.medianBlur(img, 5)
ret1, th1 = cv2.threshold(img, 127, 225, cv2.THRESH_BINARY)
ret2, th_1 = cv2.threshold(img, 0, 225, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
print ret2
th2 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
th3 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
titles = ["Original Image", "Global Thresholding(v = 127)", "Adaptive Mean Thresholding", "Adaptive Gaussian Thresholding", "Otsu Thresholding", "Histogram"]
images = [img, th1, th2, th3, th_1, ret2]
for i in xrange(6):
plt.subplot(3,2,i+1)
if i < 5:
plt.imshow(images[i], "gray")
plt.xticks([]), plt.yticks([])
if i == 5:
plt.hist(img.ravel(), 256)
plt.title(titles[i])
plt.savefig("Threshold_image")
cv2.imwrite('Fig2-.png',th3)
参考网址:
【Niblack and Sauvola Thresholding】http://scikit-image.org/docs/dev/auto_examples/segmentation/plot_niblack_sauvola.html#sphx-glr-download-auto-examples-segmentation-plot-niblack-sauvola-py