021-OpenCV(C++)-全局阈值,均值法、最小类内方差法OTSU、三角法Triangle求取二值化分割阈值

图像二值化分割,最重要的就是计算阈值,阈值计算方法有很多,基本分为两类全局阈值与自适应阈值,下面介绍全局阈值里的OTSU(最小类内方差求取最佳阈值)与Triangle(三角法求取最佳阈值)

一、均值法

代码演示:

Scalar m = mean(gray);
threshold(gray, binary, m[0], 255, THRESH_BINARY);
imshow("binary", binary);

二、OTSU:

代码演示:

threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);
imshow("binary2", binary);

三、Triangle:

代码演示:

threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_TRIANGLE);
imshow("binary3", binary);

你可能感兴趣的:(OpenCV初级,opencv,c++)