cv::threshold(GrayImg, Bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);//灰度圖像二值化
CV_THRESH_OTSU是提取圖像最佳閾值算法。該方法在類間方差最大的情況下是最佳的,就圖像的灰度值而言,OTSU給出最好的類間分離的閾值。
OpenCV閾值分割的幾種方法(types_c.h中的定義):
/* Threshold types */ enum { CV_THRESH_BINARY =0, /* value = value > threshold ? max_value : 0 */ CV_THRESH_BINARY_INV =1, /* value = value > threshold ? 0 : max_value */ CV_THRESH_TRUNC =2, /* value = value > threshold ? threshold : value */ CV_THRESH_TOZERO =3, /* value = value > threshold ? value : 0 */ CV_THRESH_TOZERO_INV =4, /* value = value > threshold ? 0 : value */ CV_THRESH_MASK =7, CV_THRESH_OTSU =8 /* use Otsu algorithm to choose the optimal threshold value; combine the flag with one of the above CV_THRESH_* values */ }