C#实现图片暗通道去雾算法-Demo-提供代码实例下载地址

 C#实现图片暗通道去雾算法

代码实例下载地址:https://www.90pan.com/b1915123

在图像去雾这个领域,几乎没有人不知道《Single Image Haze Removal Using Dark Channel Prior》这篇文章,该文是2009年CVPR最佳论文。作者何凯明博士,2007年清华大学毕业,2011年香港中文大学博士毕业,可谓是功力深厚,感叹于国内一些所谓博士的水平,何这样的博士才可以真正叫做Doctor。

关于何博士的一些资料和论文,大家可以访问这里:http://research.microsoft.com/en-us/um/people/kahe/

代码(提供项目下载):https://www.90pan.com/b1915123

public class DefogHelper
    {
        public DefogHelper() { }

        /// 
        /// 实现功能:实现基于暗通道的去雾算法。(如果要用32位的将ImageMaster_64.dll改成ImageMaster_32.dll即可)
        /// 
        /// 图像数据在内存的起始地址
        /// 目标数据在内存的起始地址
        /// 图像的宽度
        /// 图像的高度
        /// 图像的扫描行大小
        /// 用于计算暗通道图像时的矩形半径
        /// 导向滤波的半径
        /// 为防止图像天空部分出现holes,设置的最大大气光值,默认240
        /// 控制去雾程度的一个参数,建议取值范围[0.75,1],值越大,去雾越明显,但可能出现局部过增强。
        /// 用于控制最小透射率的一个参数,建议取值范围[0.01,0.2]。
        /// 调整亮度的参数,建议范围[0.7,1]。
        [DllImport("ImageMaster_64.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int IM_HazeRemovalBasedOnDarkChannelPrior(IntPtr Src, IntPtr Dest, int Width, int Height, int Stride, int BlockSize = 5, int GuideRadius = 20, int MaxAtom = 220, float Omega = 0.9f, float T0 = 0.1f, float Gamma = 0.9f);

        /// 
        /// 图片缓存区
        /// 
        private readonly byte[] bmpBuffer = new byte[1024 * 1024 * 64];
        private readonly IntPtr srcPtr = Marshal.AllocHGlobal(1024 * 1024 * 64);// 申请内存
        private readonly IntPtr destPtr = Marshal.AllocHGlobal(1024 * 1024 * 64);// 申请内存

        /// 
        /// 图片去雾
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public Bitmap ImageDefog(Bitmap scrBmp, DefogInfo info, out int result, out double ms, bool isFreed = false)
        {
            result = -1;
            ms = -1;
            if (scrBmp == null || info == null) return null;
            int w = scrBmp.Width, h = scrBmp.Height;
            System.Drawing.Rectangle bitmapRec = new System.Drawing.Rectangle(0, 0, w, h);
            BitmapData bmpData = scrBmp.LockBits(bitmapRec, ImageLockMode.ReadWrite, scrBmp.PixelFormat);
            int img_size = bmpData.Stride * h;
            if (img_size > bmpBuffer.Length) { result = 10; return null; }
            int stride = bmpData.Stride;
            try
            {
                Marshal.Copy(bmpData.Scan0, bmpBuffer, 0, img_size);
                Marshal.Copy(bmpBuffer, 0, srcPtr, img_size);
                DateTime dateTime = DateTime.Now;
                result = IM_HazeRemovalBasedOnDarkChannelPrior(srcPtr, destPtr, w, h, stride, info.BlockSize, info.GuideRadius, info.MaxAtom, info.Omega, info.T0, info.Gamma);
                ms = DateTime.Now.Subtract(dateTime).TotalMilliseconds;
                Marshal.Copy(destPtr, bmpBuffer, 0, img_size);
                Bitmap outBmp = BytesToBitmap(bmpBuffer, img_size, w, h);
                return outBmp;
            }
            catch(Exception ex)
            {
                return null;
            }
            finally
            {
                scrBmp.UnlockBits(bmpData);
                //Marshal.FreeHGlobal(srcPtr);
                //Marshal.FreeHGlobal(destPtr);
                if (isFreed) scrBmp.Dispose();
            }
        }
        /// 
        /// 数组转为Bitmap
        /// 
        /// 数组
        /// Bitmap图像
        private Bitmap BytesToBitmap(byte[] pixelData, int length, int width, int height)
        {
            Bitmap img = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            try
            {
                BitmapData data = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                Marshal.Copy(pixelData, 0, data.Scan0, length);//输入颜色数据
                img.UnlockBits(data);//解锁

                return img;
            }
            catch { img.Dispose(); return null; }
        }
        /// 
        /// 从bitmap转换成ImageSource
        /// 
        /// 
        /// 
        public ImageSource BitmapToImageSource(Bitmap bitmap)
        {
            return BitmapToBitmapImage(bitmap);
        }
        /// 
        /// 从bitmap转换成BitmapImage
        /// 
        /// 
        /// 
        public BitmapImage BitmapToBitmapImage(Bitmap bitmap)
        {
            BitmapImage bitmapImage = new BitmapImage();
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = new MemoryStream(ms.GetBuffer());
                bitmapImage.EndInit();
                ms.Close();
            }
            return bitmapImage;
        }
        /// 
        /// 将数组转化为bitmap,前54个数据是格式
        /// 
        /// 
        /// 
        public byte[] BitmapToBytes(Bitmap bitmap)
        {
            byte[] bytes;
            using (MemoryStream ms = new MemoryStream())
            {
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                bytes = ms.GetBuffer();
                ms.Close();
            }
            return bytes;
        }

        /// 
        /// 去雾信息
        /// 
        public class DefogInfo
        {
            /// 
            /// 用于计算暗通道图像时的矩形半径,2-50
            /// 
            public int BlockSize = 5;
            /// 
            /// 导向滤波的半径,2-200
            /// 
            public int GuideRadius = 20;
            /// 
            /// 为防止图像天空部分出现holes,设置的最大大气光值,默认202,190-255
            /// 
            public int MaxAtom = 198;
            /// 
            /// 控制去雾程度的一个参数,建议取值范围[0.6,1],值越大,去雾越明显,但可能出现局部过增强。
            /// 
            public float Omega = 0.7f;
            /// 
            /// 用于控制最小透射率的一个参数,建议取值范围[0.01,0.2]。
            /// 
            public float T0 = 0.01f;
            /// 
            /// 调整亮度的参数,建议范围[0.5,1]。
            /// 
            public float Gamma = 0.5f;
        }
    }

 

你可能感兴趣的:(C#实现图片暗通道去雾算法-Demo-提供代码实例下载地址)