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

下载

源码下载

相关推荐
nbsaas-boot5 分钟前
Go语言生态成熟度分析:为何Go还无法像Java那样实现注解式框架?
java·开发语言·golang
mortimer9 分钟前
Whisper断句不够好?用AI LLM和结构化数据打造完美字幕
人工智能·openai
xiaocainiao88115 分钟前
Python 实战:构建可扩展的命令行插件引擎
开发语言·python
Perishell37 分钟前
无人机避障——感知篇(Ego_Planner_v2中的滚动窗口实现动态实时感知建图grid_map ROS节点理解与参数调整影响)
计算机视觉·无人机·slam·地图生成·建图感知·双目视觉
碧海蓝天202238 分钟前
C++法则21:避免将#include放在命名空间内部。
开发语言·c++
计算生物前沿43 分钟前
单细胞分析教程 | (二)标准化、特征选择、降为、聚类及可视化
人工智能·机器学习·聚类
兮动人1 小时前
Java应用全链路故障排查实战指南:从系统资源到JVM深度诊断
java·开发语言·jvm
bianguanyue1 小时前
SQLite密码修改故障排查:RSA加密随机性导致的数据库匹配问题
数据库·sqlite·c#
R-sz1 小时前
导出word并且插入图片
开发语言·c#·word
CodeWithMe1 小时前
【读书笔记】《C++ Software Design》第一章《The Art of Software Design》
开发语言·c++