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());
        }
    }
}

下载

源码下载

相关推荐
o独酌o7 分钟前
递归的‘浅’理解
java·开发语言
Book_熬夜!10 分钟前
Python基础(六)——PyEcharts数据可视化初级版
开发语言·python·信息可视化·echarts·数据可视化
loongloongz16 分钟前
联合条件概率 以及在语言模型中的应用
人工智能·语言模型·自然语言处理·概率论
lijfrank17 分钟前
情感计算领域期刊与会议
人工智能·人机交互
sp_fyf_202421 分钟前
计算机人工智能前沿进展-大语言模型方向-2024-09-18
人工智能·语言模型·自然语言处理
sp_fyf_202422 分钟前
计算机人工智能前沿进展-大语言模型方向-2024-09-14
人工智能·语言模型·自然语言处理
ybdesire28 分钟前
nanoGPT用红楼梦数据从头训练babyGPT-12.32M实现任意问答
人工智能·深度学习·语言模型
AI极客菌34 分钟前
Stable Diffusion绘画 | 生成高清多细节图片的各个要素
人工智能·ai·ai作画·stable diffusion·aigc·midjourney·人工智能作画
FOUR_A35 分钟前
【机器学习导引】ch2-模型评估与选择
人工智能·机器学习
m0_6312704037 分钟前
高级c语言(五)
c语言·开发语言