1、获取图片亮度
/// <summary>
/// 获取图片的亮度(取HSV中V通道的均值)
/// </summary>
/// <param name="mat">图片</param>
/// <returns></returns>
public static Scalar GetVChannelMean(Mat mat)
{
// 转Hsv图片
Mat hsv = new Mat();
Cv2.CvtColor(mat, hsv, ColorConversionCodes.BGR2HSV);
// 取V通道
Cv2.Split(hsv, out Mat[] mats);
Mat vMat = mats[2];
// 求平均值
Scalar meanValue = vMat.Mean(); // 同 Cv2.Mean(vMat);
return meanValue;
}
2、修改图片亮度
/// <summary>
/// 修改图片的亮度(修改HSV中V通道的每个像素值;但不进行对比度补偿)
/// </summary>
/// <param name="mat">图片</param>
/// <param name="modifyValue">修改值</param>
/// <returns></returns>
public static Mat SetVChannelValue(Mat mat, int modifyValue)
{
// 转Hsv图片
Mat hsv = new Mat();
Cv2.CvtColor(mat, hsv, ColorConversionCodes.BGR2HSV);
// 取V通道并修改值
Cv2.Split(hsv, out Mat[] mats);
//mats[2] = new Mat(mats[2].Size(), mats[2].Type(), new Scalar(255)); // 修改值;new Scalar(modifyValue)
Mat mat2 = new Mat();
Cv2.Add(mats[2], new Scalar(modifyValue), mat2);
mats[2] = mat2;
// 合并
Mat dstMet = new Mat();
Cv2.Merge(mats, dstMet);
return dstMet;
}
标签:Mat,C#,Cv2,亮度,OpenCV,hsv,new,图片,mats
From: https://www.cnblogs.com/qq2806933146xiaobai/p/18294461