C#实现图片暗通道去雾算法
代码实例下载地址:https://www.90pan.com/b1915123
在图像去雾这个领域,几乎没有人不vb.net教程知道《Single Image Haze Removal Using Dark Channel Prior》这篇文章,该文是2009年CVPR最佳论文。作者何凯明博士,2007年清华大学毕业,2011年香港中文大学博士毕业,可谓是功力深厚,感叹于国内一些所谓博士的水平,何这样c#教程的博士才可以真正叫做Doctor。
关于何博士的一些资料和论文,大家可以访问这里:http://research.microsoft.com/en-us/um/people/kahe/
代码(提供项目下载):https://www.90pan.com/b1915123
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;
}
}