【图像处理】去雾代码收集(附halcon、python、C#、VB、matlab源码)
一、halcon算法
1.1 halcon算法源码
本算法用到的图像见资源链接,已上传至资源文件
**********************************
*何凯明博士去雾算法代码实现
*论文:<<Single Image Haze Removal Using Dark Channel Prior>>
*编写时间:2016-04-11
*作者:datiansong
**********************************
dev_update_off ()
dev_close_window ()
read_image (Image, 'fish')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
disp_message (WindowHandle, '原图像', 'window', 12, 12, 'red', 'false')
*转换图像类型,用于后续运算
convert_image_type (Image, IxImage, 'real')
*求取暗通道图像
decompose3 (IxImage, R, G, B)
min_image (R, G, ImageMin)
min_image (ImageMin, B, ImageMin1)
gray_erosion_rect (ImageMin1, DarkChannelImage,5, 5)
*计算全球大气光成分A的值
min_max_gray (DarkChannelImage, DarkChannelImage, 0.1, Min, Max, Range)
threshold (DarkChannelImage, Region, Max, 255)
min_max_gray (Region, IxImage, 0, Min1, A, Range1)
*计算透视率预估值tx
scale_image (IxImage, ImageScaled, 1/A, 0)
decompose3 (ImageScaled, R1, G1, B1)
min_image (R1, G1, ImageMin2)
min_image (ImageMin2, B1, ImageMin3)
*==================================================特别注意,下面的参数需要进行适当的,本人提供的图和参数直接用即可
*下面的尺寸如果是原来的15,那么楼房的边会出现涂抹的效果,很难看
gray_erosion_rect (ImageMin3, ImageMin4, 3, 3)
*下面的小数,绝对值越大,颜色越深,在这张图上,为-0.6效果相对较好,何博士的原来为-0.95很黑
scale_image (ImageMin4, txImage, -0.7, 1)
*设定阈值T0,如果t<T0,则t=T0
T0:=0.1
threshold (txImage, Region1, 0, T0)
paint_region (Region1, txImage, txImage, T0, 'fill')
*求取去雾后的图像
scale_image (IxImage, ImageScaled1, 1, -A)
decompose3 (ImageScaled1, R2, G2, B2)
div_image (R2, txImage, ImageResultR, 1, A)
div_image (G2, txImage, ImageResultG, 1, A)
div_image (B2, txImage, ImageResultB, 1, A)
compose3 (ImageResultR, ImageResultG, ImageResultB, JxImage)
dev_display (Image)
dev_open_window (0, 0+Width, Width, Height, 'black', WindowHandle1)
dev_display (JxImage)
disp_message (WindowHandle1, '去雾图', 'window', 12, 12, 'green', 'false')
1.2 halcon算法效果图
二、opencv算法
2.1 python源码
python的算法很简单,实际上仅调用了opencv的算法,一共4行主要代码就实现了功能,但是这个算法相对于其他的算法而言,可调的参数几乎没有。
import numpy as np
import cv2
if __name__ == '__main__':
img = cv2.imread('fog1.png')
# 实现去雾代码
b, g, r = cv2.split(img)
bx, gx, rx = cv2.equalizeHist(b), cv2.equalizeHist(g), cv2.equalizeHist(r)
img_enhance = cv2.merge((bx, gx, rx))
images = np.concatenate((img, img_enhance), axis=1)
cv2.imwrite('fog1_enhance.jpeg', images)
cv2.imshow('result', images)
cv2.waitKey()
cv2.destroyAllWindows()
2.2opencv算法效果图
三、C#算法
3.1 C#源码
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();
}
}
}
下载地址
http://files.cnblogs.com/Imageshop/HazeRemovalTest.rar
四、VB源码
4.1 截图
下载地址
http://files.cnblogs.com/Imageshop/%E5%9B%BE%E5%83%8F%E5%8E%BB%E9%9B%BE%E7%BB%BC%E5%90%88%E7%89%88%E6%9C%AC.rar
五、matlab源码
下载地址
https://link.csdn.net/?target=http%3A%2F%2Ffiles.cnblogs.com%2FImageshop%2Fcvpr09defog%2528matlab%2529.rar
说明:本文所需图片以及python源码已放置本人的资源中,自行下载
https://download.csdn.net/download/sunnyrainflower/87952490?spm=1001.2014.3001.5503
六、总结
更多的除雾算法及论文说明
参考链接
https://blog.csdn.net/huixingshao/article/details/42834939
https://zhuanlan.zhihu.com/p/489222309