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;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
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;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
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;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
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;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.


作者:꧁执笔小白꧂