大津法(Python实现)
cv2.threshold(img, th1, max_val, cv2.THRESH_OTSU)
import numpy as np def OTSU_enhance(img_gray, th_begin=0, th_end=256, th_step=1): assert img_gray.ndim == 2, "must input a gary_img" max_g = 0 suitable_th = 0 for threshold in xrange(th_begin, th_end, th_step): bin_img = img_gray > threshold bin_img_inv = img_gray <= threshold fore_pix = np.sum(bin_img) back_pix = np.sum(bin_img_inv) if 0 == fore_pix: break if 0 == back_pix: continue w0 = float(fore_pix) / img_gray.size u0 = float(np.sum(img_gray * bin_img)) / fore_pix w1 = float(back_pix) / img_gray.size u1 = float(np.sum(img_gray * bin_img_inv)) / back_pix # intra-class variance g = w0 * w1 * (u0 - u1) * (u0 - u1) if g > max_g: max_g = g suitable_th = threshold return suitable_th
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。