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

news/2025/4/26 13:09:57/

目录

效果

灰度世界(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());}}
}

下载

源码下载


http://www.ppmy.cn/news/1364132.html

相关文章

10 款数据恢复软件功能和有效性对比(2024 年更新)

数据丢失可能是一种痛苦的经历&#xff0c;无论是由于意外删除、硬件故障还是软件损坏。值得庆幸的是&#xff0c;数字时代带来了强大的数据恢复解决方案。 随着我们进入 2024 年&#xff0c;市场上充斥着旨在有效检索丢失数据的先进软件。在本文中&#xff0c;我们将探讨 2024…

分销小程序有哪些功能?

​分销类型的小程序在电商领域非常普遍受到欢迎。分销类型的小程序是指通过分销模式&#xff0c;让用户成为商品的分销商&#xff0c;通过分享商品链接或小程序码&#xff0c;推广商品并获取相应的佣金。我们开发的分销小程序的主要功能如下&#xff1a; 1. 商品管理&#xff…

Windows上基于名称快速定位文件和文件夹的免费工具Everything

在Windows上搜索文件时&#xff0c;使用windows上内置搜索会很慢&#xff0c;这里推荐使用Everything工具进行搜索。 "Everything"是Windows上一款搜索引擎&#xff0c;它能够基于文件名快速定位文件和文件夹位置。不像Windows内置搜索&#xff0c;"Everything&…

Maven jar 的查找及依赖版本确定

关于 jar 的查找&#xff0c;及使用版本的确定&#xff0c;及依赖的版本确认&#xff0c;避免 jar 冲突或版本不兼容 在使用 maven 构建项目时&#xff0c;需要的 jar 可以通过在 https://mvnrepository.com/ 可以找到部分需要的依赖&#xff0c;这里以查找 mybatis 依赖为例&…

LeetCode 刷题 [C++] 第438题.找到字符串中所有字母异位词

题目描述 给定两个字符串 s 和 p&#xff0c;找到 s 中所有 p 的 异位词 的子串&#xff0c;返回这些子串的起始索引。不考虑答案输出的顺序。 异位词 指由相同字母重排列形成的字符串&#xff08;包括相同的字符串&#xff09;。 题目分析 在字符串 s中使用同向双指针来查…

【设计模式】5种创建型模式详解

创建型模式提供创建对象的机制,能够提升已有代码的灵活性和复用性。 常用的有&#xff1a;单例模式、工厂模式&#xff08;工厂方法和抽象工厂&#xff09;、建造者模式。不常用的有&#xff1a;原型模式。 一、单例模式 1.1 单例模式介绍 1 ) 定义 单例模式&#xff08;Si…

Spring Cloud项目合规性注册之-(单元集成测试报告)

用于合规性注册&#xff0c;本文章仅提供模板 这个大纲涵盖了从单元测试到集成测试&#xff0c;再到自动化和持续集成的全方位测试过程。 一、引言 1. 项目概述 "xxxxxx"是一个先进的数据管理和展示平台&#xff0c;旨在提供高效、可靠的数据服务。该平台通过集成各…

hive内置函数--floor,ceil,rand三种取整函数

文中三种取整函数操作目录&#xff1a; 一、向下取整函数: floor ​​​​​​​二、向上取整函数: ceil ​​​​​​​​​​​​​​三、取随机数函数: rand ​​​​​​一、向下取整函数: floor 语法: floor(double a) 返回值: BIGINT 说明:返回等于或者小于该doubl…