C#完成圖片暗通道去霧算法
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;
}
}