C#界面设计之图像去雾算法

这篇博客介绍了如何使用C#结合何凯明的暗通道去雾算法来处理图像。作者提供了核心代码,并将其编译为DLL链接库,展示了实际效果。
摘要由CSDN通过智能技术生成

采用何凯明的经典暗通道图像去雾算法,核心代码以编译成DLL链接库,还是先上结果啦!GO!
这里写图片描述

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace HazeRemovalTest
{
    public unsafe partial class FrmTest : Form
    {
        // dll的代码中用的是StdCall,这里也要用StdCall,如果用Cdecl,则会出现对 PInvoke 函数“....”的调用导致堆栈不对称错误,再次按F5又可以运行

        [DllImport("HazeRemoval.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern void HazeRemovalUseDarkChannelPrior(byte* Src, byte* Dest, int Width, int Height, int Stride, int Radius,int GuideRadius, int MaxAtom, float Omega, float Epsilon, float T0);

        private bool Busy = false;

        public FrmTest()
        {
            InitializeComponent();
        }


        private void CmdOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                PicSrc.Image.Dispose();
                PicDest.Image.Dispose();
                PicSrc.Image = Bitmap.FromFile(openFileDialog.FileName);
                PicDest.Image = Bitmap.FromFile(openFileDialog.FileName);
                Application.DoEvents();
                ShowHazeRemovalResult();
            }
        }

        private void CmdHazeRemoval_Click(object sender, EventArgs e)
        {
            ShowHazeRemovalResult();
        }

        private void ShowHazeRemovalResult()
        {
            Busy = true;
            Bitmap SrcB = (Bitmap)PicSrc.Image;
            Bitmap DstB = (Bitmap)PicDest.Image;
            BitmapData SrcBmpData = SrcB.LockBits(new Rectangle(0, 0, SrcB.Width, SrcB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            BitmapData DstBmpData = DstB.LockBits(new Rectangle(0, 0, DstB.Width, DstB.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
            Stopwatch Sw = new Stopwatch();
            Sw.Start();
            HazeRemovalUseDarkChannelPrior((byte*)SrcBmpData.Scan0, (byte*)DstBmpData.Scan0, SrcBmpData.Width, SrcBmpData.Height, SrcBmpData.Stride, BlockSize.Value, GuideBlockSize.Value, MaxAtom.Value, Omega.Value * 0.01f, Epsilon.Value * 0.001f, T0.Value * 0.01f);
            Sw.Stop();
            this.Text = Sw.ElapsedMilliseconds.ToString();

            SrcB.UnlockBits(SrcBmpData);
            DstB.UnlockBits(DstBmpData);
            PicDest.Invalidate();
            Busy = false;
        }

        private void FrmTest_Load(object sender, EventArgs e)
        {
            ShowHazeRemovalResult();
        }

        private void BlockSize_Scroll(object sender, ScrollEventArgs e)
        {
            LblBlockSize.Text = BlockSize.Value.ToString();
            if (Busy==false) ShowHazeRemovalResult();
        }

        private void GuideBlockSize_Scroll(object sender, ScrollEventArgs e)
        {
            LblGuideBlockSize.Text = GuideBlockSize.Value.ToString();
            if (Busy == false) ShowHazeRemovalResult();
        }

        private void Omega_Scroll(object sender, ScrollEventArgs e)
        {
            LblOmega.Text = Omega.Value.ToString() + "%";
            if (Busy == false) ShowHazeRemovalResult();
        }

        private void MaxAtom_Scroll(object sender, ScrollEventArgs e)
        {
            LbLAtom.Text = MaxAtom.Value.ToString();
            if (Busy == false) ShowHazeRemovalResult();
        }

        private void Epsilon_Scroll(object sender, ScrollEventArgs e)
        {
            LblEpsilon.Text = (Epsilon.Value * 0.0001).ToString();
            if (Busy == false) ShowHazeRemovalResult();
        }

        private void T0_Scroll(object sender, ScrollEventArgs e)
        {
            LblT0.Text = (T0.Value * 0.01).ToString();
            if (Busy == false) ShowHazeRemovalResult();
        }


    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HazeRemovalTest
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmTest());
        }
    }
}
namespace HazeRemovalTest
{
    unsafe partial class FrmTest
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmTest));
            this.LblGuideBlockSize = new System.Windows.Forms.Label();
            this.导向半径 = new System.Windows.Forms.Label();
            this.GuideBlockSize = new System.Windows.Forms.HScrollBar();
            this.LbLAtom = new System.Windows.Forms.Label();
            this.label3 = new System.Windows.Forms.Label();
            this.MaxAtom = new System.Windows.Forms.HScrollBar();
            this.LblBlockSize = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.BlockSize = new System.Windows.Forms.HScrollBar();
            this.CmdHazeRemoval = new System
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值