You are on page 1of 15

Lampiran 1

Kode Program Perangkat Lunak Perbaikan Kualitas Citra berkabut (Haze)


Kelas Form.cs
using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dehazing
{
public partial class Form1 : Form
{
Image<Bgr, byte> a = null;
Control Controller = new Control();

public Form1()
{
InitializeComponent();
}
private void resetComponent()
{
darkChannelToolStripMenuItem.Enabled = false;
darkChannel3x3ToolStripMenuItem.Enabled = false;
darkChannel15x15ToolStripMenuItem.Enabled = false;
dehazeToolStripMenuItem.Enabled = false;
histogramToolStripMenuItem.Enabled = false;
button1.Enabled = false;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
resetComponent();
a = new Image<Bgr, byte>(openFileDialog1.FileName);
Controller.setImageOriginal(a);
pictureBox1.Image = a.ToBitmap();
pictureBox2.Image = null;
button1.Enabled = true;
darkChannelToolStripMenuItem.Enabled = true;
darkChannel3x3ToolStripMenuItem.Enabled = true;
darkChannel15x15ToolStripMenuItem.Enabled = true;

L-1

L-2

}
}
private void dehazeToolStripMenuItem_Click(object sender, EventArgs e)
{
Controller.getTransmission();
Controller.Dehaze_Image();
pictureBox2.Image = Controller.getDehaze_Image();
dehazeToolStripMenuItem.Enabled = false;
histogramToolStripMenuItem.Enabled = true;
}
private void dehazeButton_Click(object sender, EventArgs e)
{
Stopwatch st = new Stopwatch();
st.Start();
Controller.Dehaze_Image_All();
pictureBox2.Image = Controller.getDehaze_Image();
st.Stop();
label1.Text = "Time process " + st.ElapsedMilliseconds + "ms";
Controller.MSE();
label2.Text = "MSE Value = " + Controller.getMSEValue();
darkChannelToolStripMenuItem.Enabled = false;
dehazeToolStripMenuItem.Enabled = false;
histogramToolStripMenuItem.Enabled = true;
}
private void darkChannelToolStripMenuItem_Click(object sender, EventArgs e)
{
Controller.DCP_Image();
pictureBox2.Image = Controller.getDCP_Image();
darkChannelToolStripMenuItem.Enabled = false;
darkChannel3x3ToolStripMenuItem.Enabled = false;
darkChannel15x15ToolStripMenuItem.Enabled = false;
dehazeToolStripMenuItem.Enabled = true;
}
private void histogramToolStripMenuItem_Click(object sender, EventArgs e)
{
Controller.HistogramGraf();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp";
saveFileDialog1.Title = "Save an Image File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
System.IO.FileStream fs =

L-3

(System.IO.FileStream)saveFileDialog1.OpenFile();
switch (saveFileDialog1.FilterIndex)
{
case 1:
this.pictureBox2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case 2:
this.pictureBox2.Image.Save(fs,
System.Drawing.Imaging.ImageFormat.Bmp);
break;
}
fs.Close();
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Dini Triyama 2015");
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void darkChannel3x3ToolStripMenuItem_Click(object sender, EventArgs
e)
{
Controller.DCP_Image3x3();
pictureBox2.Image = Controller.getDCP_Image();
darkChannelToolStripMenuItem.Enabled = false;
darkChannel3x3ToolStripMenuItem.Enabled = false;
darkChannel15x15ToolStripMenuItem.Enabled = false;
dehazeToolStripMenuItem.Enabled = true;
}
private void darkChannel15x15ToolStripMenuItem_Click(object sender,
EventArgs e)
{
Controller.DCP_Image15x15();
pictureBox2.Image = Controller.getDCP_Image();
darkChannelToolStripMenuItem.Enabled = false;
darkChannel3x3ToolStripMenuItem.Enabled = false;
darkChannel15x15ToolStripMenuItem.Enabled = false;
dehazeToolStripMenuItem.Enabled = true;
}
}
}

L-4

Kelas Control.cs

L-5

using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Drawing;
Emgu.CV;
Emgu.CV.CvEnum;
Emgu.CV.Features2D;
Emgu.CV.Structure;
Emgu.CV.UI;
Emgu.CV.Util;

namespace Dehazing
{
public class Control
{
Nilai Value = new Nilai();
Dark_Channel_Prior DCP = new Dark_Channel_Prior();
Transmission T = new Transmission();
Dehaze D = new Dehaze();
Kualitas Quality = new Kualitas();
public void setImageOriginal(Image<Bgr,byte> ImgSrc)
{
Value.ImageOriginal = ImgSrc;
}
public void DCP_Image()
{
Value.ImageDCP = DCP.getMedianDarkChannel(Value.ImageOriginal, 5);
Value.AirLight = DCP.estimateA(Value.ImageDCP);
}
public void DCP_Image3x3()
{
Value.ImageDCP = DCP.getMedianDarkChannel3(Value.ImageOriginal, 5);
Value.AirLight = DCP.estimateA(Value.ImageDCP);
}
public void DCP_Image15x15()
{
Value.ImageDCP = DCP.getMedianDarkChannel15(Value.ImageOriginal, 5);
Value.AirLight = DCP.estimateA(Value.ImageDCP);
}
public Bitmap getDCP_Image()
{
return Value.ImageDCP.ToBitmap();
}
public void getTransmission()
{
Value.ImageTransmission= T.estimateTransmission(Value.ImageDCP,
Value.AirLight);
}
public void Dehaze_Image()

L-6

{
Value.ImageDehazing = D.getDehazed(Value.ImageOriginal,
Value.ImageTransmission, Value.AirLight);
}
public Bitmap getDehaze_Image()
{
return Value.ImageDehazing.ToBitmap();
}
public void MSE()
{
Value.MSEValue = Quality.MSE(Value.ImageOriginal, Value.ImageDehazing);
}
public double getMSEValue()
{
return Value.MSEValue;
}
public void Dehaze_Image_All()
{
DCP_Image3x3();
getTransmission();
Dehaze_Image();
}
public void HistogramGraf()
{
Value.HistogramInputBlue = Quality.HistogramValue(Value.ImageOriginal, 0);
Value.HistogramInputGreen = Quality.HistogramValue(Value.ImageOriginal, 1);
Value.HistogramInputRed = Quality.HistogramValue(Value.ImageOriginal, 2);
Value.HistogramOutputBlue = Quality.HistogramValue(Value.ImageDehazing, 0);
Value.HistogramOutputGreen = Quality.HistogramValue(Value.ImageDehazing,
1);
Value.HistogramOutputRed = Quality.HistogramValue(Value.ImageDehazing, 2);

HTG HTG_Form = new


HTG(Value.HistogramInputRed,Value.HistogramInputGreen,Value.HistogramInputBlue,
Value.HistogramOutputRed,Value.HistogramOutputGreen,Value.HistogramOutputBlue);
HTG_Form.DrawGraf();
HTG_Form.Show();
}
}
}

Kelas Nilai.cs

L-7

using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
Emgu.CV;
Emgu.CV.CvEnum;
Emgu.CV.Features2D;
Emgu.CV.Structure;
Emgu.CV.UI;
Emgu.CV.Util;

namespace Dehazing
{
public class Nilai
{
private int Airlight;
//airlight value
private double mse;
private const double maxValue = 16777215;
private
private
private
private
private
private

int[]
int[]
int[]
int[]
int[]
int[]

HInputR = new int[256];


HInputG = new int[256];
HInputB = new int[256];
HOutputR = new int[256];
HOutputG = new int[256];
HOutputB = new int[256];

private
private
private
private

Image<Bgr, byte> imgSource;


Image<Gray, byte> darkChannel;
Image<Gray, byte> transmission;
Image<Bgr, byte> dehaze;

public Image<Bgr,byte> ImageOriginal


{
get
{
return imgSource;
}
set
{
imgSource = value;
}
}
public Image<Gray, byte> ImageDCP
{
get
{
return darkChannel;
}
set
{
darkChannel = value;
}
}
public Image<Gray, byte> ImageTransmission

L-8

{
get
{
return transmission;
}
set
{
transmission = value;
}
}
public Image<Bgr, byte> ImageDehazing
{
get
{
return dehaze;
}
set
{
dehaze = value;
}
}
public int[] HistogramInputRed
{
get
{
return HInputR;
}
set
{
HInputR = value;
}
}
public int[] HistogramInputGreen
{
get
{
return HInputG;
}
set
{
HInputG = value;
}
}
public int[] HistogramInputBlue
{
get
{
return HInputB;
}
set
{
HInputB = value;
}
}

L-9

public int[] HistogramOutputRed


{
get
{
return HOutputR;
}
set
{
HOutputR = value;
}
}
public int[] HistogramOutputGreen
{
get
{
return HOutputG;
}
set
{
HOutputG = value;
}
}
public int[] HistogramOutputBlue
{
get
{
return HOutputB;
}
set
{
HOutputB = value;
}
}
public double MSEValue
{
get
{
return mse;
}
set
{
mse = value;
}
}
public double getPixelMaxValue()
{
return maxValue;
}
public int AirLight
{
get
{
return Airlight;
}
set
{

L-10

Airlight = value;
}
}
}
}

Kelas Dark_Channel_Prior.cs
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Drawing;
Emgu.CV;
Emgu.CV.CvEnum;
Emgu.CV.Features2D;
Emgu.CV.Structure;
Emgu.CV.UI;
Emgu.CV.Util;

namespace Dehazing
{
public class Dark_Channel_Prior
{
public Image<Gray, byte> getMedianDarkChannel(Image<Bgr, byte> src, int
patch)
{
Image<Gray, byte> rgbmin = new Image<Gray, byte>(src.Width, src.Height);
for (int m = 0; m < src.Width; m++)
{
for (int n = 0; n < src.Height; n++)
{
rgbmin.Data[n, m, 0] = Math.Min(Math.Min(src.Data[n, m, 0], src.Data[n, m,
1]), src.Data[n, m, 2]);
}
}
rgbmin = rgbmin.SmoothMedian(patch); //smoothing with median filter
return rgbmin;
}
public Image<Gray, byte> getMedianDarkChannel3(Image<Bgr, byte> src, int
patch)
{
Image<Gray, byte> rgbmin = new Image<Gray, byte>(src.Width, src.Height);
for (int m = 0; m < src.Width; m++)
{
for (int n = 0; n < src.Height; n++)
{
rgbmin.Data[n, m, 0] = (byte)getCenterPx3(m,n,src);
}
}

L-11

rgbmin = rgbmin.SmoothMedian(patch); //smoothing with median filter


return rgbmin;
}
public Image<Gray, byte> getMedianDarkChannel15(Image<Bgr, byte> src, int
patch)
{
Image<Gray, byte> rgbmin = new Image<Gray, byte>(src.Width, src.Height);
for (int m = 0; m < src.Width; m++)
{
for (int n = 0; n < src.Height; n++)
{
rgbmin.Data[n, m, 0] = (byte)getCenterPx15(m, n, src);
}
}
rgbmin = rgbmin.SmoothMedian(patch); //smoothing with median filter
return rgbmin;
}
private int getCenterPx3(int x, int y, Image<Bgr,byte> imgSource)
{
int i, j, r = 0, g = 0, b = 0;
int min = 255;
Color cMin = Color.White;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (i >= 0 && i < imgSource.Width && j >= 0 && j < imgSource.Height)
{
if (((imgSource.Data[j,i,0] + imgSource.Data[j,i,1] +
imgSource.Data[j,i,2]) / 3) < min)
{
min = ((imgSource.Data[j, i, 0] + imgSource.Data[j, i, 1] +
imgSource.Data[j, i, 2]) / 3);
r = imgSource.Data[j, i, 2];
g = imgSource.Data[j, i, 1];
b = imgSource.Data[j, i, 0];
}
}
}
}
min = 255;
if (r < min)
min = r;
if (b < min)
min = b;
if (g < min)
min = g;
return min;
}
private int getCenterPx15(int x, int y, Image<Bgr, byte> imgSource)

L-12

{
int i, j, r = 0, g = 0, b = 0;
int min = 255;
Color cMin = Color.White;
for (i = x - 7; i <= x + 7; i++)
{
for (j = y - 7; j <= y + 7; j++)
{
if (i >= 0 && i < imgSource.Width && j >= 0 && j < imgSource.Height)
{
if (((imgSource.Data[j, i, 0] + imgSource.Data[j, i, 1] + imgSource.Data[j,
i, 2]) / 3) < min)
{
min = ((imgSource.Data[j, i, 0] + imgSource.Data[j, i, 1] +
imgSource.Data[j, i, 2]) / 3);
r = imgSource.Data[j, i, 2];
g = imgSource.Data[j, i, 1];
b = imgSource.Data[j, i, 0];
}
}
}
}
min = 255;
if (r < min)
min = r;
if (b < min)
min = b;
if (g < min)
min = g;
return min;
}
public int estimateA(Image<Gray, byte> DC)
{
double[] minDC, maxDC;
Point[] minDC_Loc, maxDC_Loc;
DC.MinMax(out minDC, out maxDC, out minDC_Loc, out maxDC_Loc);
return (int)maxDC[0];
}
}
}

Kelas Transmission.cs
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Drawing;

using
using
using
using

Emgu.CV;
Emgu.CV.CvEnum;
Emgu.CV.Features2D;
Emgu.CV.Structure;

L-13

using Emgu.CV.UI;
using Emgu.CV.Util;
namespace Dehazing
{
public class Transmission
{
public Image<Gray, Byte> estimateTransmission(Image<Gray, Byte> DCP, int ac)
{
double w = 0.75;
Image<Gray, Byte> transmission = new Image<Gray, byte>(DCP.Width,
DCP.Height);
for (int m = 0; m < DCP.Width; m++)
{
for (int n = 0; n < DCP.Height; n++)
{
transmission.Data[n, m, 0] = (byte)((1 - w * (double)DCP.Data[n, m, 0] / ac)
* 255);
}
}
return transmission;
}
}
}

Kelas Dehaze.cs
#undef DEBUG
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Drawing;
Emgu.CV;
Emgu.CV.CvEnum;
Emgu.CV.Features2D;
Emgu.CV.Structure;
Emgu.CV.UI;
Emgu.CV.Util;

public class Dehaze


{
public Image<Bgr, Byte> getDehazed(Image<Bgr, Byte> source, Image<Gray,
Byte> t, int al)
{
double tmin = 0.1;
double tmax;
Image<Bgr, Byte> dehazed = source.CopyBlank(); //CV_8UC3 == Bgr image
for (int i = 0; i < source.Width; i++)
{
for (int j = 0; j < source.Height; j++)
{

L-14

tmax = ((double)t.Data[j, i, 0] / 255) < tmin ? tmin : ((double)t.Data[j, i, 0] /


255);
for (int k = 0; k < 3; k++)
{
dehazed.Data[j, i, 0] = (byte)(Math.Abs(((double)source.Data[j, i, 0] - al) /
tmax + al) > 255 ? 255 : Math.Abs(((double)source.Data[j, i, 0] - al) / tmax + al)); //blue
dehazed.Data[j, i, 1] = (byte)(Math.Abs(((double)source.Data[j, i, 1] - al) /
tmax + al) > 255 ? 255 : Math.Abs(((double)source.Data[j, i, 1] - al) / tmax + al)); //green
dehazed.Data[j, i, 2] = (byte)(Math.Abs(((double)source.Data[j, i, 2] - al) /
tmax + al) > 255 ? 255 : Math.Abs(((double)source.Data[j, i, 2] - al) / tmax + al)); //red
}
}
}
return dehazed;
}
}

Kelas Kualitas.cs
using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Drawing;

using
using
using
using
using
using

Emgu.CV;
Emgu.CV.CvEnum;
Emgu.CV.Features2D;
Emgu.CV.Structure;
Emgu.CV.UI;
Emgu.CV.Util;

namespace Dehazing
{
public class Kualitas
{
public double MSE(Image<Bgr,byte> ImgSource, Image<Bgr,byte> ImgOutput)
{
double pembilang = 0, r, g, b;
for (int i = 0; i < ImgSource.Width; i++)
{
for (int j = 0; j < ImgSource.Height; j++)
{
r = (double)ImgOutput.Data[j, i, 2] - (double)ImgSource.Data[j, i, 2];
g = (double)ImgOutput.Data[j, i, 1] - (double)ImgSource.Data[j, i, 1];
b = (double)ImgOutput.Data[j, i, 0] - (double)ImgSource.Data[j, i, 0];
pembilang = pembilang + ((r * r) + (g * g) + (b * b));
}
}
return (double)pembilang / (ImgSource.Width * ImgSource.Height);
}

L-15

public int[] HistogramValue(Image<Bgr,byte> src, int n)


{
int h = src.Height;
int w = src.Width;
src.ToBitmap();
int[] hist = new int[256];
for (int i = 0; i < 256; i++)
hist[i] = 0;
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
hist[src.Data[j, i, n]] = hist[src.Data[j, i, n]] + 1;
}
}
return hist;
}

}
}

You might also like