图像处理:灰度化

图像灰度化就是图像中的每一像素点的分量都满足R=G=B=V的关系,此时的V就是灰度值

为什么要灰度化?

灰度是指含有亮度信息,不含彩色信息的图像。

使用灰度图的好处:

1、RGB的值都一样

2、图像数据 = 调色板索引值 = RGB的值 = 亮度值

3、调色板为256色,所以图像数据中的一个字节代表一个像素

一般做图像处理之前都需要先把图像进行灰度化。

在这之前要访问图像中的每一个像素:

Mat inputImage = imread("C:\\Users\\asus\\Desktop\\MouseWithoutBorders\\11.jpg");
Mat outoutImage = inputImage.clone();
int rowN = outoutImage.rows; //行数和列数
int cloN = outoutImage.cols;
for (int i = 0; i < rowN; i++)
{
	for (int j = 0; j < cloN; j++)
	{
		outoutImage.at<cv::Vec3b>(i, j)[0] ;  //B
		outoutImage.at<cv::Vec3b>(i, j)[1] ;  //G
		outoutImage.at<cv::Vec3b>(i, j)[2] ;  //R
	}
}

原图,以便对比:

灰度化的处理方法大致有以下几种方法:

1、平均值法:

double RGB = outoutImage.at<cv::Vec3b>(i, j)[0] + 
            outoutImage.at<cv::Vec3b>(i, j)[1] + 
	    outoutImage.at<cv::Vec3b>(i, j)[2];
outoutImage.at<cv::Vec3b>(i, j)[0] = RGB / 3;  //B
outoutImage.at<cv::Vec3b>(i, j)[1] = RGB / 3;  //G
outoutImage.at<cv::Vec3b>(i, j)[2] = RGB / 3;  //R

2、最大值法

double a = outoutImage.at<cv::Vec3b>(i, j)[0];
double b = outoutImage.at<cv::Vec3b>(i, j)[1];
double c = outoutImage.at<cv::Vec3b>(i, j)[2];
double max;
max = a > b ? a : b;
max = max > c ? max : c;
outoutImage.at<cv::Vec3b>(i, j)[0] = max;
outoutImage.at<cv::Vec3b>(i, j)[1] = max;
outoutImage.at<cv::Vec3b>(i, j)[2] = max;

3、加权平均法:

double a = outoutImage.at<cv::Vec3b>(i, j)[0]; //B
double b = outoutImage.at<cv::Vec3b>(i, j)[1]; //G
double c = outoutImage.at<cv::Vec3b>(i, j)[2]; //R
outoutImage.at<cv::Vec3b>(i, j)[0] = a * 0.3 + b * 0.59 + c * 0.11;
outoutImage.at<cv::Vec3b>(i, j)[1] = a * 0.3 + b * 0.59 + c * 0.11;
outoutImage.at<cv::Vec3b>(i, j)[2] = a * 0.3 + b * 0.59 + c * 0.11;

个人感觉均值灰度化的效果比较好~