## opencv utilise la couche inférieure pour obtenir des niveaux de gris d'images (C++)
Sans plus tarder, passons directement aux choses sérieuses ! ! !
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Mat img;
img = imread("D:\\壁纸\\xx.jpg");
resize(img, img, Size(1000, 500));
imshow("img", img);
waitKey(0);
}
Voici un aperçu de la belle dame ! !
int channel = img.channels();//分割通道
int thright = img.rows; //图像的高
int weight = img.cols; //图像的宽
for (int i = 0; i < thright; i++)
{
for(int j = 0; j < weight; j++)
{
if (channel == 3) //判断一下是否是三个通道。
{
int r = img.at<Vec3b>(i, j)[0];
int g = img.at<Vec3b>(i, j)[1];
int b = img.at<Vec3b>(i, j)[2];
//将获取的彩色值转换成同一个值。
//这里的img.at<Vec3b>[],是利用循环遍历整个图片,
//从而获得每个位置的像素值,最后在更改像素值。
img.at<Vec3b>(i, j)[0] = 0.45 * r + 0.35 * g + 0.25 * b;
img.at<Vec3b>(i, j)[1] = 0.45 * r + 0.35 * g + 0.25 * b;
img.at<Vec3b>(i, j)[2] = 0.45 * r + 0.35 * g + 0.25 * b;
}
}
}
imshow("gray", img);
waitKey(0);
Ensuite, montrez les rendus.