[Image processing] Defog code collection (with halcon, python, C#, VB, matlab source code)
With the vigorous development of image processing technology and computer vision technology, scene detection and image processing under special weather has become an important research direction. Images taken in foggy days are easily affected by fog or haze, resulting in blurred pictures, low contrast, and loss of important image information. Therefore, it is necessary to dehaze the fogged image, process the image information, and ensure the normal operation of other computer vision tasks.
One, halcon algorithm
1.1 halcon algorithm source code
For the images used in this algorithm, see the resource link, which has been uploaded to the resource file
**********************************
*何凯明博士去雾算法代码实现
*论文:<<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 Effect diagram of halcon algorithm
Two, opencv algorithm
2.1 python source code
The algorithm of python is very simple. In fact, only the algorithm of opencv is called. A total of 4 lines of main code realize the function. However, compared with other algorithms, this algorithm has almost no adjustable parameters.
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 algorithm rendering
3. C# Algorithm
3.1 C# source code
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();
}
}
}
download link
http://files.cnblogs.com/Imageshop/HazeRemovalTest.rar
Four, VB source code
4.1 Screenshot
download link
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
Five, matlab source code
download link
https://link.csdn.net/?target=http%3A%2F%2Ffiles.cnblogs.com%2FImageshop%2Fcvpr09defog%2528matlab%2529.rar
Note: The pictures and python source code required for this article have been placed in my own resources, and you can download them yourself
https://download.csdn.net/download/sunnyrainflower/87952490?spm=1001.2014.3001.5503
6. Summary
Regarding the algorithm for dehazing, I prefer C# code, which has many adjustable parameters and is easy to implement. The python program has almost no adjustable parameters; the algorithm of halcon also needs fine-tuning parameters, which are more related to the size and definition of the image; The VB code has the most functions. The author of the source code has integrated 6 algorithms in it, and you can try them all. I won't explain too much about matlab, let's study it slowly.
More dehazing algorithms and paper descriptions.
Reference link
https://blog.csdn.net/huixingshao/article/details/42834939
https://zhuanlan.zhihu.com/p/489222309