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/ide

代碼(提供項目下載):https://www.90pan.com/b1915123ui

public class DefogHelper
    {
        public DefogHelper() { }

        /// <summary>
        /// 實現功能:實現基於暗通道的去霧算法。(若是要用32位的將ImageMaster_64.dll改爲ImageMaster_32.dll便可)
        /// </summary>
        /// <param name="Src">圖像數據在內存的起始地址</param>
        /// <param name="Dest">目標數據在內存的起始地址</param>
        /// <param name="Width">圖像的寬度</param>
        /// <param name="Height">圖像的高度</param>
        /// <param name="Stride">圖像的掃描行大小</param>
        /// <param name="BlockSize">用於計算暗通道圖像時的矩形半徑</param>
        /// <param name="GuideRadius">導向濾波的半徑</param>
        /// <param name="MaxAtom">爲防止圖像天空部分出現holes,設置的最大大氣光值,默認240</param>
        /// <param name="Omega">控制去霧程度的一個參數,建議取值範圍[0.75,1],值越大,去霧越明顯,但可能出現局部過加強。</param>
        /// <param name="T0">用於控制最小透射率的一個參數,建議取值範圍[0.01,0.2]。</param>
        /// <param name="Gamma">調整亮度的參數,建議範圍[0.7,1]。</param>
        [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);

        /// <summary>
        /// 圖片緩存區
        /// </summary>
        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);// 申請內存

        /// <summary>
        /// 圖片去霧
        /// </summary>
        /// <param name="scrBmp"></param>
        /// <param name="info"></param>
        /// <param name="result"></param>
        /// <param name="ms"></param>
        /// <param name="isFreed"></param>
        /// <returns></returns>
        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();
            }
        }
        /// <summary>
        /// 數組轉爲Bitmap
        /// </summary>
        /// <param name="pixelData">數組</param>
        /// <returns>Bitmap圖像</returns>
        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; }
        }
        /// <summary>
        /// 從bitmap轉換成ImageSource
        /// </summary>
        /// <param name="icon"></param>
        /// <returns></returns>
        public ImageSource BitmapToImageSource(Bitmap bitmap)
        {
            return BitmapToBitmapImage(bitmap);
        }
        /// <summary>
        /// 從bitmap轉換成BitmapImage
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// 將數組轉化爲bitmap,前54個數據是格式
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// 去霧信息
        /// </summary>
        public class DefogInfo
        {
            /// <summary>
            /// 用於計算暗通道圖像時的矩形半徑,2-50
            /// </summary>
            public int BlockSize = 5;
            /// <summary>
            /// 導向濾波的半徑,2-200
            /// </summary>
            public int GuideRadius = 20;
            /// <summary>
            /// 爲防止圖像天空部分出現holes,設置的最大大氣光值,默認202,190-255
            /// </summary>
            public int MaxAtom = 198;
            /// <summary>
            /// 控制去霧程度的一個參數,建議取值範圍[0.6,1],值越大,去霧越明顯,但可能出現局部過加強。
            /// </summary>
            public float Omega = 0.7f;
            /// <summary>
            /// 用於控制最小透射率的一個參數,建議取值範圍[0.01,0.2]。
            /// </summary>
            public float T0 = 0.01f;
            /// <summary>
            /// 調整亮度的參數,建議範圍[0.5,1]。
            /// </summary>
            public float Gamma = 0.5f;
        }
    }
相關文章
相關標籤/搜索