C#+OpenCV基础(十三)_图片白平衡矫正

1、白平衡矫正 -灰度幂律变换(伽马变换)

/// <summary>
/// 白平衡矫正 -灰度幂律变换(伽马变换)
/// 对过曝和过暗的图片进行矫正
/// </summary>
/// <param name="srcMat">图片</param>
/// <returns>返回黑白图片</returns>
public static Mat WhiteBalance_PowerLaw_Transformation(Mat srcMat)
{
    Mat dstMat = new Mat(srcMat.Size(), srcMat.Type());

    Mat grayMat = new Mat();
    Cv2.CvtColor(srcMat, grayMat, ColorConversionCodes.BGR2BGRA);

    for (int w = 0; w < grayMat.Width; w++)
    {
        for (int h = 0; h < grayMat.Height; h++)
        {
            byte gray = grayMat.Get<byte>(h, w);
            dstMat.At<byte>(h, w) = Convert.ToByte(Math.Pow(gray, 0.5));  // 将灰度开方
        }
    }
    Cv2.Normalize(dstMat, dstMat, 0, 255, NormTypes.MinMax);  // 归一化;将数据归一到0~255
    return dstMat;
}

2、白平衡矫正 -灰度世界(GrayworldWB)

/// <summary>
/// 白平衡矫正 -灰度世界(GrayworldWB)
/// https://docs.opencv.org/4.x/d7/d71/classcv_1_1xphoto_1_1GrayworldWB.html#details
/// </summary>
/// <param name="src">图片</param>
/// <returns>结果图片</returns>
public static Mat WhiteBalance_Correction_GrayworldWB(Mat mat)
{
    Mat dst = new Mat();

    // 灰度世界(GrayworldWB)
    WhiteBalancer wb = CvXPhoto.CreateGrayworldWB();
    wb.BalanceWhite(mat, dst);

    return dst;
}

3、白平衡矫正 -完美反射(SimpleWB)

/// <summary>
/// 白平衡矫正 -完美反射(SimpleWB)
/// https://docs.opencv.org/4.x/d1/d8b/classcv_1_1xphoto_1_1SimpleWB.html#details
/// </summary>
/// <param name="src">图片</param>
/// <returns>结果图片</returns>
/// <exception cref="Exception"></exception>
public static Mat WhiteBalance_Correction_SimpleWB(Mat mat)
{
    Mat dst = new Mat();

    // 完美反射(SimpleWB)
    WhiteBalancer wb = CvXPhoto.CreateSimpleWB();
    wb.BalanceWhite(mat, dst);

    return dst;
}

4、白平衡矫正 -基于学习的(LearningBasedWB)

/// <summary>
/// 白平衡矫正 -基于学习的(LearningBasedWB)
/// https://docs.opencv.org/4.x/dc/dcb/tutorial_xphoto_training_white_balance.html
/// </summary>
/// <param name="src">图片</param>
/// <returns>结果图片</returns>
/// <exception cref="Exception"></exception>
public static Mat WhiteBalance_Correction_Learning(Mat mat)
{
    Mat dst = new Mat();

    // 基于学习的(LearningBasedWB)
    string model = "model/LearningBasedWB";  // 模型路径
    WhiteBalancer wb = CvXPhoto.CreateLearningBasedWB(model);
    wb.BalanceWhite(mat, dst);

    return dst;
}
posted @   ꧁执笔小白꧂  阅读(199)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2021-07-10 layui的table.render使用
2019-07-10 html根据下拉框选中的值修改背景颜色
点击右上角即可分享
微信分享提示