彩色图像 :三通道,像素值一般为0~255;
灰度图像:单通道,像素值一般为0~255;
二值图像:单通道,像素值一般为0(黑色)、255(白色);
彩色图像颜色范围查询表:
设原图的像素值分布为如下红色区域,蓝色的线表示像素值阈值T。
a)大于阈值T的像素点置满保留,其余置0。b)小于阈值T的像素点置满保留,其余去除。
a)大于阈值T的像素点值置为阈值。 b)小于阈值T的像素点值置0。
c)大于阈值T的像素点值置0。
API
threshold(灰度图, 输出图像, 阈值, 图像最大像素值, 阈值分割方法);
阈值分割方法:
void QuickDemo::zero_one_img(Mat& image)
{
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
namedWindow("gray", WINDOW_FREERATIO);
imshow("gray", gray);
Mat binary;
threshold(gray, binary, 127, 255, THRESH_BINARY);
namedWindow("THRESH_BINARY", WINDOW_FREERATIO);
imshow("THRESH_BINARY", binary);
threshold(gray, binary, 127, 255, THRESH_BINARY_INV);
namedWindow("THRESH_BINARY_INV", WINDOW_FREERATIO);
imshow("THRESH_BINARY_INV", binary);
threshold(gray, binary, 127, 255, THRESH_TRUNC);
namedWindow("THRESH_TRUNC", WINDOW_FREERATIO);
imshow("THRESH_TRUNC", binary);
threshold(gray, binary, 127, 255, THRESH_TOZERO);
namedWindow("THRESH_TOZERO", WINDOW_FREERATIO);
imshow("THRESH_TOZERO", binary);
threshold(gray, binary, 127, 255, THRESH_TOZERO_INV);
namedWindow("THRESH_TOZERO_INV", WINDOW_FREERATIO);
imshow("THRESH_TOZERO_INV", binary);
}
在图像阈值分割时,需要选取一个合适的阈值,基本分为全局阈值、自适应阈值两大类。
均值法:统计图像像素值的均值作为阈值。
void zero_one_img(Mat& image)
{
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
Scalar m = mean(gray);
//printf("means : %2f\n", m[0]);
Mat binary;
threshold(gray, binary, m[0], 255, THRESH_BINARY);
namedWindow("THRESH_BINARY", WINDOW_FREERATIO);
imshow("THRESH_BINARY", binary);
}
OTSU法(基于最小类内方差求阈值的方法):基于像素直方图的统计结果,统计各像素点值的方差值,选类内方差最小的像素点值作为阈值。
如下对像素点值为2的像素点求类内方差的过程:
a)分别 计算该像素点值左边、右边的比重,均值,方差。
b)计算类内方差:
void QuickDemo::zero_one_img(Mat& image)
{
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
namedWindow("gray", WINDOW_FREERATIO);
imshow("gray", gray);
Mat binary;
threshold(gray, binary, 0, 255, THRESH_BINARY|THRESH_OTSU);
namedWindow("THRESH_OTSU", WINDOW_FREERATIO);
imshow("THRESH_OTSU", binary);
}
Triangle法:获取如下直方图后,从最高点到最低点作一线段,以α和β为45°为前提,寻找右图上使得d最大的线段,将此时α角点的像素点值作为阈值。
注:在常规操作时,为了使得像素点值不过于靠近最多的像素点值,常常将其向右偏移20%的总像素距离。
void QuickDemo::zero_one_img(Mat& image)
{
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
namedWindow("gray", WINDOW_FREERATIO);
imshow("gray", gray);
Mat binary;
threshold(gray, binary, 0, 255, THRESH_BINARY|THRESH_TRIANGLE);
namedWindow("THRESH_OTSU", WINDOW_FREERATIO);
imshow("THRESH_OTSU", binary);
}
适用情况总结:OTSU法适用于有多峰直方图的图像,三角法使用有单峰直方图的图像。
采用卷积的方式,对图像逐个窗口的中心像素点值进行阈值处理,此时的每个窗口的阈值方法有窗口均值阈值法、高斯分布阈值法。
API:
CV_EXPORTS_W void adaptiveThreshold( InputArray src, OutputArray dst,
double maxValue, int adaptiveMethod,
int thresholdType, int blockSize, double C );
maxValue ——赋予给满足条件的像素的非零值。
AdaptiveMethod ——要使用的自适应阈值算法, 如下:
thresholdType ——阈值分割方法,这里只能使用THRESH_BINARY等方法,不能使用OTSU法。
blockSize——用于计算像素阈值的像素邻域的大小:3、5、7 等。
C ——计算后均值被减去的值,如 threshvalure - C,C常为正数。
代码:
void QuickDemo::zero_one_img(Mat& image)
{
Mat gray;
cvtColor(image, gray, COLOR_BGR2GRAY);
namedWindow("gray", WINDOW_FREERATIO);
imshow("gray", gray);
Mat binary;
adaptiveThreshold(gray, binary, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY,3,0);
namedWindow("adaptiveThreshold", WINDOW_FREERATIO);
imshow("adaptiveThreshold", binary);
}