二値化法の比較。
import pylab as plt plt.rcParams['font.family'] = 'IPAexGothic' plt.rcParams['font.size'] = 12 import cv2 import numpy as np im = cv2.imread("cat.jpg") im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) #Adaptive Gaussian Thresholding th1 = cv2.adaptiveThreshold(im_gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\ cv2.THRESH_BINARY,11,2) #フィルター+大津 blur = cv2.bilateralFilter(im_gray,6,12,3) ret,th2 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU) #フィルターのみ ret,th3 = cv2.threshold(blur,70,255,cv2.THRESH_BINARY_INV) print ret plt.subplot(2,2,1),plt.imshow(im,'gray') plt.title('input image') plt.subplot(2,2,2),plt.imshow(th1,'gray') plt.title('Adaptive Gaussian Thresholding') plt.subplot(2,2,3),plt.imshow(th2,'gray') plt.title(u"フィルター+大津") plt.subplot(2,2,4),plt.imshow(th3,'gray') plt.title(u"フィルターのみ") plt.show() cv2.waitKey(0) cv2.destroyAllWindows()
Adaptive Gaussianの圧勝っすね。
参考(というかそのまま)
http://opencvpython.blogspot.jp/2013/05/thresholding.html