本节课介绍了图像又彩色图像转变为彩色图像转变为灰度图像转变为黑色图像的转化过程。
灰度图像-单通道-取值范围为0-255
二值图像-单通道-取值0(黑色)-255(白色)
二值分割
有五种分割方式
如图所示
第一种:大于阈值T则为255,else为0
第二种:小于阈值T则为255,else为0
第三种:小于阈值T则为保留原值,else为0
第四种:小于阈值T则为0,else为保留原值
第五种:大于阈值T则为0,else为原值
这意味着将会有五种方式来改变灰色图像的不同阈值的二值图像
THRESH_BINARY(二值化)
THRESH_BINARY_INV(二值化)
THRESH_TRUNC(阈值化)
THRESH_TOZERO(阈值化)
THRESH_TOZERO_INV(阈值化)
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main(int argc,char**argv)
{
Mat src = imread("C:/newword/image/1.jpg");
if (src.empty())
{
printf("没有识别到图像" );
return -1;
}
namedWindow("原图", WINDOW_AUTOSIZE);
imshow("原图",src );
Mat dst, gcc;
cvtColor(src, dst, COLOR_BGR2GRAY);
imshow("灰度图像", dst);
threshold(dst, gcc, 127, 255, THRESH_BINARY);
imshow("阈值图像", gcc);
threshold(dst, gcc, 127, 255, THRESH_BINARY_INV);
imshow("THRESH_BINARY_INV", gcc);
threshold(dst, gcc, 127, 255, THRESH_TRUNC);
imshow("THRESH_TRUNC", gcc);
threshold(dst, gcc, 127, 255, THRESH_TOZERO);
imshow("THRESH_TOZERO", gcc);
threshold(dst, gcc, 127, 255, THRESH_TOZERO_INV);
imshow("THRESH_TOZERO_INV", gcc);
//预设阈值
waitKey(0);
destroyAllWindows();
return -1;
}