C# OpenCvSharp 利用白平衡技术进行图像修复

目录

效果

灰度世界(GrayworldWB)-白平衡算法

完美反射(SimpleWB)-白平衡算法

基于学习的(LearningBasedWB)-白平衡算法

代码

下载


C# OpenCvSharp 利用白平衡技术进行图像修复

OpenCV xphoto模块中提供了三种不同的白平衡算法,分别是:灰度世界(GrayworldWB)算法、完完美反射(SimpleWB)算法和基于学习的(LearningBasedWB)白平衡算法

效果

灰度世界(GrayworldWB)-白平衡算法

参考链接:https://docs.opencv.org/4.x/d7/d71/classcv_1_1xphoto_1_1GrayworldWB.html#details

完美反射(SimpleWB)-白平衡算法

参考链接:https://docs.opencv.org/4.x/d1/d8b/classcv_1_1xphoto_1_1SimpleWB.html#details

基于学习的(LearningBasedWB)-白平衡算法

参考链接:https://docs.opencv.org/4.x/dc/dcb/tutorial_xphoto_training_white_balance.html

代码

using OpenCvSharp;

using OpenCvSharp.XPhoto;

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Security.Cryptography;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace C__OpenCvSharp_利用白平衡技术进行图像修复

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";

string image_path = "";

Mat image;

Mat dst = new Mat();

private void button1_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Filter = fileFilter;

if (ofd.ShowDialog() != DialogResult.OK) return;

pictureBox1.Image = null;

image_path = ofd.FileName;

pictureBox1.Image = new Bitmap(image_path);

image = new Mat(image_path);

pictureBox2.Image = null;

}

private void Form1_Load(object sender, EventArgs e)

{

image_path = "1.jpg";

pictureBox1.Image = new Bitmap(image_path);

}

/// <summary>

/// 灰度世界(GrayworldWB)-白平衡算法

/// https://docs.opencv.org/4.x/d7/d71/classcv_1_1xphoto_1_1GrayworldWB.html#details

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button2_Click(object sender, EventArgs e)

{

if (image_path == "")

{

return;

}

pictureBox2.Image = null;

image = new Mat(image_path);

WhiteBalancer wb = CvXPhoto.CreateGrayworldWB();

wb.BalanceWhite(image, dst);

pictureBox2.Image = new Bitmap(dst.ToMemoryStream());

}

/// <summary>

/// 完美反射(SimpleWB)-白平衡算法

/// https://docs.opencv.org/4.x/d1/d8b/classcv_1_1xphoto_1_1SimpleWB.html#details

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button3_Click(object sender, EventArgs e)

{

if (image_path == "")

{

return;

}

pictureBox2.Image = null;

image = new Mat(image_path);

WhiteBalancer wb = CvXPhoto.CreateSimpleWB();

wb.BalanceWhite(image, dst);

pictureBox2.Image = new Bitmap(dst.ToMemoryStream());

}

/// <summary>

/// 基于学习的(LearningBasedWB)-白平衡算法

/// https://docs.opencv.org/4.x/dc/dcb/tutorial_xphoto_training_white_balance.html

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

private void button4_Click(object sender, EventArgs e)

{

if (image_path == "")

{

return;

}

pictureBox2.Image = null;

image = new Mat(image_path);

string model = "";//模型路径

WhiteBalancer wb = CvXPhoto.CreateLearningBasedWB(model);

wb.BalanceWhite(image, dst);

pictureBox2.Image = new Bitmap(dst.ToMemoryStream());

}

}

}

复制代码
using OpenCvSharp;
using OpenCvSharp.XPhoto;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace C__OpenCvSharp_利用白平衡技术进行图像修复
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        Mat image;
        Mat dst = new Mat();
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
            pictureBox2.Image = null;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            image_path = "1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
        }

        /// <summary>
        /// 灰度世界(GrayworldWB)-白平衡算法
        /// https://docs.opencv.org/4.x/d7/d71/classcv_1_1xphoto_1_1GrayworldWB.html#details
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            pictureBox2.Image = null;

            image = new Mat(image_path);
            WhiteBalancer wb = CvXPhoto.CreateGrayworldWB();
            wb.BalanceWhite(image, dst);
            pictureBox2.Image = new Bitmap(dst.ToMemoryStream());
        }

        /// <summary>
        /// 完美反射(SimpleWB)-白平衡算法
        /// https://docs.opencv.org/4.x/d1/d8b/classcv_1_1xphoto_1_1SimpleWB.html#details
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            pictureBox2.Image = null;

            image = new Mat(image_path);
            WhiteBalancer wb = CvXPhoto.CreateSimpleWB();
            wb.BalanceWhite(image, dst);
            pictureBox2.Image = new Bitmap(dst.ToMemoryStream());
        }

        /// <summary>
        /// 基于学习的(LearningBasedWB)-白平衡算法
        /// https://docs.opencv.org/4.x/dc/dcb/tutorial_xphoto_training_white_balance.html
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            pictureBox2.Image = null;

            image = new Mat(image_path);
            string model = "";//模型路径
            WhiteBalancer wb = CvXPhoto.CreateLearningBasedWB(model);
            wb.BalanceWhite(image, dst);
            pictureBox2.Image = new Bitmap(dst.ToMemoryStream());
        }
    }
}

下载

源码下载

相关推荐
每日摸鱼大王2 分钟前
互联网摸鱼日报(2025-07-01)
人工智能
rzl022 分钟前
java web5(黑马)
java·开发语言·前端
GIS小天11 分钟前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年7月4日第128弹
人工智能·算法·机器学习·彩票
jingling55514 分钟前
面试版-前端开发核心知识
开发语言·前端·javascript·vue.js·面试·前端框架
lx74160269816 分钟前
cd-agent更换cd模型(自用)
计算机视觉
我是小哪吒2.023 分钟前
书籍推荐-《对抗机器学习:攻击面、防御机制与人工智能中的学习理论》
人工智能·深度学习·学习·机器学习·ai·语言模型·大模型
慕婉030726 分钟前
深度学习前置知识全面解析:从机器学习到深度学习的进阶之路
人工智能·深度学习·机器学习
阿蒙Amon32 分钟前
【Python小工具】使用 OpenCV 获取视频时长的详细指南
python·opencv·音视频
m0_6873998433 分钟前
写一个Ununtu C++ 程序,调用ffmpeg API, 来判断一个数字电影的视频文件mxf 是不是Jpeg2000?
开发语言·c++·ffmpeg
爱上语文43 分钟前
Redis基础(5):Redis的Java客户端
java·开发语言·数据库·redis·后端