# 二、灰度变换的作用

1.改善图像是质量，显示更多的细节，提高图像的对比度
2.有选择的突出图像感兴趣的特征或者抑制图像中不需要的特征
3.可以有效的改变图像的直方图的分布，使像素的分布更加均匀

# 三、灰度变换的方法

1.线性灰度变换
2.非线性灰度变换（对数变换，幂律变换（伽马变换））

# 二、对彩色图进行灰度化

## 1.加权平均值法

D=0.299R+0.587G+0.114*B

``````#include<iostream>
#include<opencv.hpp>
using namespace std;
using namespace cv;

int main()
{
Mat img, img2;
imshow("原图", img);
img2.create(img.size(), 0);
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
img2.at<uchar>(i, j) = saturate_cast<uchar>(0.114*img.at<Vec3b>(i, j)[0] + 0.587*img.at<Vec3b>(i, j)[1] + 0.299*img.at<Vec3b>(i, j)[2]);
}
}
imshow("经验公式", img2);
waitKey(0);
}
``````

## 2.取最大值

``````int main()
{
Mat img, img2;
imshow("原图", img);
img2.create(img.size(), 0);
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
int max = img.at<Vec3b>(i, j)[0];
for (int x = 0; x < 3; x++)
{
if (max < img.at<Vec3b>(i, j)[x])
{
max = img.at<Vec3b>(i, j)[x];
}
}

img2.at<uchar>(i, j) = saturate_cast<uchar>(max);
}
}
imshow("最大值", img2);
waitKey(0);
}
``````

## 3.平均值

``````int main()
{
Mat img, img2;
imshow("原图", img);
img2.create(img.size(), 0);
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
img2.at<uchar>(i, j) = saturate_cast<uchar>((img.at<Vec3b>(i, j)[0] + img.at<Vec3b>(i, j)[1] + img.at<Vec3b>(i, j)[2])/3);
}
}
imshow("平均值", img2);
waitKey(0);
}
``````

# 灰度的线性变换

## 1.线性变换

y=kx+b;

``````int main()
{

Mat img1, img2;
img1 = imread("猫1.jpg", 1);
imshow("原图", img1);
img2 = Mat::zeros(img1.size(), 0);
for (int i = 0; i < img1.rows; i++)
{
for (int j = 0; j < img1.cols; j++)
{
for (int s = 0; s < 3; s++)
{
img2.at<uchar>(i, j) = saturate_cast<uchar>(1.1*img1.at<Vec3b>(i, j)[s] + 20);
}
}
}
imshow("线性", img2);
waitKey(0);
}
``````

## 2.分段线性变换

``````int main()
{

Mat img1, img2;
img1 = imread("猫1.jpg", 0);
imshow("原图", img1);
img2 = Mat::zeros(img1.size(), 0);
for (int i = 0; i < img1.rows; i++)
{
for (int j = 0; j < img1.cols; j++)
{

uchar temp = img1.at<uchar>(i, j);
if (temp <=70)
{
img2.at<uchar>(i, j) = saturate_cast<uchar>(0.5*temp + 20);
}
else if (temp > 70 && temp <= 150)
{
img2.at<uchar>(i, j) = saturate_cast<uchar>(1.2*temp + 100);
}
else if (temp > 150 && temp <= 255)
{
img2.at<uchar>(i, j) = saturate_cast<uchar>(0.9*temp + 55);
}
}
}
imshow("分段线性", img2);
waitKey(0);
}
``````

# 灰度的非线性变换

## 1.对数变换

``````int main()
{
double c = 1.2;
Mat img1, img2, img3;

img3 = Mat::ones(img1.size(), CV_32FC3);
img1.convertTo(img1, CV_32F);
log(img1, img3);
img3 = c*img3;

normalize(img3, img3, 0, 255, NORM_MINMAX);//归一化到0-255 NORM_MINMAX 线性归一化
convertScaleAbs(img3, img3);//转换成8bit通道显示
imshow("对数变换", img3);
waitKey(0);
}
``````

## 2.幂律变换

``````int main()
{
Mat img1, img2;
img2.create(img1.size(), img1.type());
for (int i = 0; i < img1.rows; i++)
{
for (int j = 0; j < img1.cols; j++)
{
int gray = img1.at<uchar>(i, j);
img2.at<uchar>(i, j) = saturate_cast<uchar>(pow(gray,0.5));
}
}
normalize(img2, img2, 0, 255, NORM_MINMAX);
imshow("幂律变换", img2);
waitKey(0);
}
``````

THE END