You are on page 1of 23

Imports System

Imports System.Windows.Forms

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Collections

Namespace MachineVision

Friend Class Vision

'for now all methods are static

Private Sub New()

End Sub

Public Shared Sub DetectEdges(ByVal original As Bitmap, ByRef modified As Bitmap, ByVal c As Color,
ByVal noise As Integer)

Dim g As Graphics = Graphics.FromImage(original)

For x As Integer = 0 To original.Width - 1

For y As Integer = 0 To original.Height - 1

If x > 2 AndAlso y > 2 Then

Try

Dim current As Color = modified.GetPixel(x, y)

Dim right As Color = modified.GetPixel(x + 1, y)

Dim left As Color = modified.GetPixel(x - 1, y)

Dim totalCurrent As Integer = current.R + current.G + current.B

Dim totalRight As Integer = right.R + right.G + right.B

Dim totalLeft As Integer = left.R + left.G + left.B

If totalCurrent > totalLeft + noise OrElse totalCurrent > totalRight + noise Then

modified.SetPixel(x - 1, y, Color.White) 'white


ElseIf totalCurrent > totalRight + noise Then

modified.SetPixel(x + 1, y, Color.White) 'white

Else

modified.SetPixel(x, y, Color.Black)

End If 'black

Dim upOne As Color = modified.GetPixel(x, y - 1)

Dim downOne As Color = modified.GetPixel(x, y + 1)

Dim totalUpOne As Integer = upOne.R + upOne.G + upOne.B

Dim totalDownOne As Integer = downOne.R + downOne.G + downOne.B

If totalUpOne > totalCurrent + noise Then

modified.SetPixel(x, y - 1, Color.White)

ElseIf totalDownOne > totalCurrent + noise Then

modified.SetPixel(x, y + 1, Color.White)

Else

modified.SetPixel(x, y, Color.Black)

End If

Dim upLeft As Color = modified.GetPixel(x - 1, y - 1)

Dim downRight As Color = modified.GetPixel(x + 1, y + 1)

Dim totalUpLeft As Integer = upLeft.R + upLeft.G + upLeft.B

Dim totalDownRight As Integer = downRight.R + downRight.G + downRight.B

If totalUpLeft > totalCurrent + noise Then

modified.SetPixel(x - 1, y - 1, Color.White)

ElseIf totalDownRight > totalCurrent + noise Then

modified.SetPixel(x + 1, y + 1, Color.White)
Else

modified.SetPixel(x, y, Color.Black)

End If

Dim upRight As Color = modified.GetPixel(x + 1, y - 1)

Dim downLeft As Color = modified.GetPixel(x - 1, y + 1)

Dim totalupRight As Integer = upRight.R + upRight.G + upRight.B

Dim totalDownLeft As Integer = downLeft.R + downLeft.G + downLeft.B

If totalupRight > totalCurrent + noise Then

modified.SetPixel(x + 1, y - 1, Color.White)

ElseIf totalDownRight > totalCurrent + noise Then

modified.SetPixel(x - 1, y + 1, Color.White)

Else

modified.SetPixel(x, y, Color.Black)

End If

Catch __unusedArgumentException1__ As ArgumentException

End Try

End If

Next

Next

End Sub

''' <summary>

''' outlines the edges of an image


''' </summary>

Public Shared Sub OutLineEdges(ByVal original As Bitmap, ByRef modified As Bitmap, ByVal c As
Color, ByVal noise As Integer)

Dim g As Graphics = Graphics.FromImage(original)

For x As Integer = 0 To original.Width - 1

For y As Integer = 0 To original.Height - 1

If x > 2 AndAlso y > 2 Then

Try

Dim current As Color = modified.GetPixel(x, y)

Dim right As Color = modified.GetPixel(x + 1, y)

Dim left As Color = modified.GetPixel(x - 1, y)

Dim totalCurrent As Integer = current.R + current.G + current.B

Dim totalRight As Integer = right.R + right.G + right.B

Dim totalLeft As Integer = left.R + left.G + left.B

If totalCurrent > totalLeft + noise OrElse totalCurrent > totalRight + noise Then

modified.SetPixel(x - 1, y, c)

ElseIf totalCurrent > totalRight + noise Then

modified.SetPixel(x + 1, y, c)

End If

Dim upOne As Color = modified.GetPixel(x, y - 1)

Dim downOne As Color = modified.GetPixel(x, y + 1)

Dim totalUpOne As Integer = upOne.R + upOne.G + upOne.B


Dim totalDownOne As Integer = downOne.R + downOne.G + downOne.B

If totalUpOne > totalCurrent + noise Then '|| totalDownOne > (totalCurrent + 75))

modified.SetPixel(x, y - 1, c)

ElseIf totalDownOne > totalCurrent + noise Then

modified.SetPixel(x, y + 1, c)

End If

Dim upLeft As Color = modified.GetPixel(x - 1, y - 1)

Dim downRight As Color = modified.GetPixel(x + 1, y + 1)

Dim totalUpLeft As Integer = upLeft.R + upLeft.G + upLeft.B

Dim totalDownRight As Integer = downRight.R + downRight.G + downRight.B

If totalUpLeft > totalCurrent + noise Then

modified.SetPixel(x - 1, y - 1, c)

ElseIf totalDownRight > totalCurrent + noise Then

modified.SetPixel(x + 1, y + 1, c)

End If

Dim upRight As Color = modified.GetPixel(x + 1, y - 1)

Dim downLeft As Color = modified.GetPixel(x - 1, y + 1)

Dim totalupRight As Integer = upRight.R + upRight.G + upRight.B

Dim totalDownLeft As Integer = downLeft.R + downLeft.G + downLeft.B

If totalupRight > totalCurrent + noise Then

modified.SetPixel(x + 1, y - 1, c)

ElseIf totalDownRight > totalCurrent + noise Then


modified.SetPixel(x - 1, y + 1, c)

End If

Catch __unusedArgumentException1__ As ArgumentException

End Try

End If

Next

Next

End Sub

''' <summary>

''' detects skin. loops through the pixels in the image, if the pixel is skin then it leaves that pixel
alone,

''' or else it will color that pixel black.

''' </summary>

Public Shared Sub DetectSkin(ByVal original As Bitmap, ByRef modified As Bitmap)

Dim g As Graphics = Graphics.FromImage(original)

Dim points As ArrayList = New ArrayList()

For x As Integer = 0 To original.Width - 1


For y As Integer = 0 To original.Height - 1

Dim c As Color = modified.GetPixel(x, y)

' convert RGB color space to IRgBy color space using this formula:

I= [L(R) + L(B) + L(G)] / 3

Rg = L(R) - L(G)

By = L(B) - [L(G) +L(R)] / 2

to calculate the hue:

hue = atan2(Rg,By) * (180 / 3.141592654f)

Dim I As Double = (Math.Log(c.R) + Math.Log(c.B) + Math.Log(c.G)) / 3

Dim Rg As Double = Math.Log(c.R) - Math.Log(c.G)

Dim By As Double = Math.Log(c.B) - (Math.Log(c.G) + Math.Log(c.R)) / 2

Dim hue As Double = Math.Atan2(Rg, By) * (180 / Math.PI)

If I <= 5 AndAlso hue >= 4 AndAlso hue <= 255 Then

'r = 255;

points.Add(New Point(x, y))

Else

modified.SetPixel(x, y, Color.Black)

End If

Next

Next

'SortPoints(ref points);

'PlotLines(Graphics.FromImage(modified), (Point)points[0], (Point)points[points.Count - 1]);


End Sub

Private Shared Sub PlotLines(ByVal g As Graphics, ByVal p1 As Point, ByVal p2 As Point)

g.DrawLine(Pens.White, p1, p2)

End Sub

Private Shared Sub SortPoints(ByRef pts As ArrayList)

'int x = 4;

For i As Integer = 1 To pts.Count - 1

Dim thisPoint As Point = pts(i)

Dim lastPoint As Point = pts(i - 1)

'thisPoint is closer to 0,0

If thisPoint.X < lastPoint.X AndAlso thisPoint.Y < lastPoint.Y Then

Else

'lastPoint is closer to 0,0

'swap thisPoint and lastPoint

swap(pts, i - 1, i)

End If

Next

'--x;

'if(!(x == 0)) SortPoints(ref pts);

End Sub

Private Shared Sub swap(ByRef pts As ArrayList, ByVal a As Integer, ByVal b As Integer)

Dim temp As Point

Dim pA As Point = pts(a)


Dim pB As Point = pts(b)

temp = pA

pA = pB

pB = temp

End Sub

Private Shared Function max(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As


Integer

If r > g AndAlso r > b Then

Return r

ElseIf g > r AndAlso g > b Then

Return g

Else

Return b

End If

End Function

Private Shared Function min(ByVal r As Integer, ByVal g As Integer, ByVal b As Integer) As


Integer

If r < g AndAlso r < b Then

Return r

ElseIf g < r AndAlso g < b Then

Return g

Else

Return b

End If

End Function

End Class

End Namespace
C# Codeng

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.IO;

namespace MachineVision
{
class Vision
{
//for now all methods are static
private Vision()
{

///<summary>
///this method creates a black and white copy of the image
///</summary>
public static void DetectEdges(Bitmap original, ref Bitmap modified,
Color c, int noise)
{
Graphics g = Graphics.FromImage(original);
for(Int32 x = 0; x < original.Width; x++)
{
for(Int32 y = 0; y < original.Height; y++)
{
if(x > 2 && y > 2)
{
try
{
Color current = modified.GetPixel(x
, y);
Color right = modified.GetPixel(x +
1, y);
Color left = modified.GetPixel(x -
1, y);

int totalCurrent = current.R +


current.G + current.B;
int totalRight = right.R + right.G
+ right.B;
int totalLeft = left.R + left.G
+ left.B;

if(totalCurrent > (totalLeft + noise) ||


totalCurrent > (totalRight + noise))
{
modified.SetPixel(x - 1, y,
Color.White);//white
}
else if(totalCurrent > (totalRight +
noise))
{
modified.SetPixel(x + 1, y,
Color.White);//white
}
else
modified.SetPixel(x, y,
Color.Black);//black

Color upOne = modified.GetPixel(x, y -


1);
Color downOne = modified.GetPixel(x, y +
1);

int totalUpOne = upOne.R + upOne.G


+ upOne.B;
int totalDownOne = downOne.R + downOne.G
+ downOne.B;

if(totalUpOne > (totalCurrent + noise))


{
modified.SetPixel(x, y - 1,
Color.White);
}
else if(totalDownOne > (totalCurrent +
noise))
{
modified.SetPixel(x, y + 1,
Color.White);
}
else
modified.SetPixel(x, y, Color.Black);

Color upLeft = modified.GetPixel(x -


1, y - 1);
Color downRight = modified.GetPixel(x +
1, y + 1);

int totalUpLeft = upLeft.R +


upLeft.G + upLeft.B;
int totalDownRight = downRight.R +
downRight.G + downRight.B;
if(totalUpLeft > (totalCurrent + noise))
{
modified.SetPixel(x - 1, y - 1,
Color.White);
}
else if(totalDownRight > (totalCurrent +
noise))
{
modified.SetPixel(x + 1, y + 1,
Color.White);
}
else
modified.SetPixel(x, y, Color.Black);

Color upRight = modified.GetPixel(x + 1,


y - 1);
Color downLeft = modified.GetPixel(x - 1,
y + 1);
int totalupRight = upRight.R +
upRight.G + upRight.B;
int totalDownLeft = downLeft.R +
downLeft.G + downLeft.B;
if(totalupRight > (totalCurrent + noise))
{
modified.SetPixel(x + 1, y - 1,
Color.White);
}
else if(totalDownRight > (totalCurrent +
noise))
{
modified.SetPixel(x - 1, y + 1,
Color.White);
}
else
modified.SetPixel(x, y, Color.Black);
}
catch(System.ArgumentException)
{

}
}
}
}

///<summary>
///outlines the edges of an image
///</summary>
public static void OutLineEdges(Bitmap original, ref Bitmap modified,
Color c, int noise)
{
Graphics g = Graphics.FromImage(original);
for(Int32 x = 0; x < original.Width; x++)
{
for(Int32 y = 0; y < original.Height; y++)
{
if(x > 2 && y > 2)
{
try
{
Color current = modified.GetPixel(x
, y);
Color right = modified.GetPixel(x +
1, y);
Color left = modified.GetPixel(x -
1, y);

int totalCurrent = current.R +


current.G + current.B;
int totalRight = right.R + right.G
+ right.B;
int totalLeft = left.R + left.G
+ left.B;

if(totalCurrent > (totalLeft + noise) ||


totalCurrent > (totalRight + noise))
{
modified.SetPixel(x - 1, y, c);
}
else if(totalCurrent > (totalRight +
noise))
{
modified.SetPixel(x + 1, y, c);
}

Color upOne = modified.GetPixel(x, y -


1);
Color downOne = modified.GetPixel(x, y +
1);

int totalUpOne = upOne.R + upOne.G


+ upOne.B;
int totalDownOne = downOne.R + downOne.G
+ downOne.B;

if(totalUpOne > (totalCurrent + noise))


//|| totalDownOne > (totalCurrent + 75))
{
modified.SetPixel(x, y - 1, c);
}
else if(totalDownOne > (totalCurrent +
noise))
{
modified.SetPixel(x, y + 1, c);
}

Color upLeft = modified.GetPixel(x -


1, y - 1);
Color downRight = modified.GetPixel(x +
1, y + 1);

int totalUpLeft = upLeft.R +


upLeft.G + upLeft.B;
int totalDownRight = downRight.R +
downRight.G + downRight.B;
if(totalUpLeft > (totalCurrent + noise))
{
modified.SetPixel(x - 1, y - 1,
c);
}
else if(totalDownRight > (totalCurrent +
noise))
{
modified.SetPixel(x + 1, y + 1,
c);
}

Color upRight = modified.GetPixel(x + 1,


y - 1);
Color downLeft = modified.GetPixel(x - 1,
y + 1);

int totalupRight = upRight.R +


upRight.G + upRight.B;
int totalDownLeft = downLeft.R +
downLeft.G + downLeft.B;
if(totalupRight > (totalCurrent + noise))
{
modified.SetPixel(x + 1, y - 1,
c);
}
else if(totalDownRight > (totalCurrent +
noise))
{
modified.SetPixel(x - 1, y + 1,
c);
}

}
catch(System.ArgumentException)
{

}
}
}
}
}

///<summary>
///detects skin. loops through the pixels in the image, if the pixel is
skin then it leaves that pixel alone,
///or else it will color that pixel black.
///</summary>
public static void DetectSkin(Bitmap original, ref Bitmap modified)
{
Graphics g = Graphics.FromImage(original);
ArrayList points = new ArrayList();
for(Int32 x = 0; x < original.Width; x++)
{
for(Int32 y = 0; y < original.Height; y++)
{

Color c = modified.GetPixel(x, y);

/* convert RGB color space to IRgBy color space


using this formula:
I= [L(R) + L(B) + L(G)] / 3
Rg = L(R) - L(G)
By = L(B) - [L(G) +L(R)] / 2

to calculate the hue:


hue = atan2(Rg,By) * (180 / 3.141592654f)
*/
double I = (Math.Log(c.R) + Math.Log(c.B) +
Math.Log(c.G)) / 3;
double Rg = Math.Log(c.R) - Math.Log(c.G);
double By = Math.Log(c.B) - (Math.Log(c.G) +
Math.Log(c.R)) / 2;
double hue = Math.Atan2(Rg, By) * (180 / Math.PI);
if (I <= 5 && (hue >= 4 && hue <= 255))
{
//r = 255;
points.Add(new Point(x, y));
}
else
{
modified.SetPixel(x, y, Color.Black);
}

}
}
//SortPoints(ref points);
//PlotLines(Graphics.FromImage(modified), (Point)points[0],
(Point)points[points.Count - 1]);
}

private static void PlotLines(Graphics g, Point p1, Point p2)


{
g.DrawLine(Pens.White, p1, p2);
}

private static void SortPoints(ref ArrayList pts)


{
//int x = 4;
for(int i = 1; i < pts.Count; i++)
{
Point thisPoint = (Point)pts[i];
Point lastPoint = (Point)pts[i - 1];
if(thisPoint.X < lastPoint.X && thisPoint.Y < lastPoint.Y)
{
//thisPoint is closer to 0,0
}
else
{
//lastPoint is closer to 0,0
//swap thisPoint and lastPoint
swap(ref pts, i - 1, i);
}
}
//--x;
//if(!(x == 0)) SortPoints(ref pts);
}

private static void swap(ref ArrayList pts, int a, int b)


{
Point temp;
Point pA = (Point)pts[a];
Point pB = (Point)pts[b];

temp = pA;
pA = pB;
pB = temp;
}
private static int max(int r, int g, int b)
{
if(r > g && r > b)
return r;
else if(g > r && g > b)
return g;
else
return b;
}

private static int min(int r, int g, int b)


{
if(r < g && r < b)
return r;
else if(g < r && g < b)
return g;
else
return b;
}

}
}
Imports System.Drawing.Drawing2D

Public Class transpPrivewForm

Dim tempCnt As Boolean 'check weather the roller is used or not

Dim bm_dest As Bitmap


Dim bm_source As Bitmap
Dim i As Int16 = 0.5

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs)
Me.PreviewPictureBox.Image = Nothing
End Sub

'Trackbar untuk menyesuaikan ukuran citra hasil cropping di picturebox


#Region "Image Resizing"
Private Sub resizingTrackBar_ValueChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles resizingTrackBar.ValueChanged
Try

Dim scale_factor As Integer


Dim img1 As New PictureBox

scale_factor = Integer.Parse(resizingTrackBar.Value)
img1.Image = cropBitmap
bm_source = New Bitmap(img1.Image)
bm_dest = New Bitmap( _
CInt(bm_source.Width * scale_factor), _
CInt(bm_source.Height * scale_factor))

Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + i, bm_dest.Height + i)

PreviewPictureBox.Image = bm_dest

tempCnt = True
Catch ex As Exception

End Try
End Sub
#End Region

'Fungsi cropping citra dengan mousedown


#Region "Image Cropping"
Dim cropX As Integer
Dim cropY As Integer
Dim cropWidth As Integer
Dim cropHeight As Integer

Dim oCropX As Integer


Dim oCropY As Integer
Dim cropBitmap As Bitmap

Public cropPen As Pen


Public cropPenSize As Integer = 1 '2
Public cropDashStyle As Drawing2D.DashStyle = Drawing2D.DashStyle.Solid
Public cropPenColor As Color = Color.Yellow

Private Sub crobPictureBox_MouseDown(ByVal sender As System.Object, ByVal e As


System.Windows.Forms.MouseEventArgs) Handles crobPictureBox.MouseDown
Try

If e.Button = Windows.Forms.MouseButtons.Left Then

cropX = e.X
cropY = e.Y

cropPen = New Pen(cropPenColor, cropPenSize)


cropPen.DashStyle = DashStyle.DashDotDot
Cursor = Cursors.Cross

End If
crobPictureBox.Refresh()
Catch exc As Exception
End Try
End Sub
Dim tmppoint As Point
Private Sub crobPictureBox_MouseMove(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles crobPictureBox.MouseMove
Try

If crobPictureBox.Image Is Nothing Then Exit Sub

If e.Button = Windows.Forms.MouseButtons.Left Then

crobPictureBox.Refresh()
cropWidth = e.X - cropX
cropHeight = e.Y - cropY
crobPictureBox.CreateGraphics.DrawRectangle(cropPen, cropX, cropY,
cropWidth, cropHeight)
End If
' GC.Collect()

Catch exc As Exception

If Err.Number = 5 Then Exit Sub


End Try

End Sub

Private Sub crobPictureBox_MouseUp(ByVal sender As Object, ByVal e As


System.Windows.Forms.MouseEventArgs) Handles crobPictureBox.MouseUp
Try
Cursor = Cursors.Default
Try

If cropWidth < 1 Then


Exit Sub
End If

Dim rect As Rectangle = New Rectangle(cropX, cropY, cropWidth,


cropHeight)
Dim bit As Bitmap = New Bitmap(crobPictureBox.Image,
crobPictureBox.Width, crobPictureBox.Height)

cropBitmap = New Bitmap(cropWidth, cropHeight)


Dim g As Graphics = Graphics.FromImage(cropBitmap)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
PreviewPictureBox.Image = cropBitmap

Catch exc As Exception


End Try
Catch exc As Exception
End Try
End Sub
#End Region

'PictureBox menampilkan citra awal yang akan dicropping


Private Sub transpPrivewForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim str As String = Application.StartupPath & "\img1.gif"
PreviewPictureBox.Image = System.Drawing.Bitmap.FromFile(str)
crobPictureBox.Image = System.Drawing.Bitmap.FromFile(str)
End Sub

'Menyimpan Citra hasil cropping


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cropSaveBtn.Click
Dim tempFileName As String
Dim svdlg As New SaveFileDialog()
svdlg.Filter = "JPEG files (*.jpg)|*.jpg|All files (*.*)|*.*"
svdlg.FilterIndex = 1
svdlg.RestoreDirectory = True
If svdlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
tempFileName = svdlg.FileName 'check the file exist else save
the cropped image
Try
Dim img As Image = PreviewPictureBox.Image

SavePhoto(img, tempFileName, 225)


Catch exc As Exception
MsgBox("Error on Saving: " & exc.Message)
End Try
End If
End Sub

'Membersihkan (mengosongkan) citra yang ada di picturebox


Private Sub cropCancelBtn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cropCancelBtn1.Click
PreviewPictureBox.Image = Nothing
cropBitmap = Nothing
crobPictureBox.Image = Nothing
End Sub

'Fungsi untuk penyimpanan citra


Public Function SavePhoto(ByVal src As Image, ByVal dest As String, ByVal w As
Integer) As Boolean
Try
Dim imgTmp As System.Drawing.Image
Dim imgFoto As System.Drawing.Bitmap

imgTmp = src
imgFoto = New System.Drawing.Bitmap(w, 225)
Dim recDest As New Rectangle(0, 0, w, imgFoto.Height)
Dim gphCrop As Graphics = Graphics.FromImage(imgFoto)
gphCrop.SmoothingMode = SmoothingMode.HighQuality
gphCrop.CompositingQuality = CompositingQuality.HighQuality
gphCrop.InterpolationMode = InterpolationMode.High

gphCrop.DrawImage(imgTmp, recDest, 0, 0, imgTmp.Width, imgTmp.Height,


GraphicsUnit.Pixel)

Dim myEncoder As System.Drawing.Imaging.Encoder


Dim myEncoderParameter As System.Drawing.Imaging.EncoderParameter
Dim myEncoderParameters As System.Drawing.Imaging.EncoderParameters

Dim arrayICI() As System.Drawing.Imaging.ImageCodecInfo =


System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders()
Dim jpegICI As System.Drawing.Imaging.ImageCodecInfo = Nothing
Dim x As Integer = 0
For x = 0 To arrayICI.Length - 1
If (arrayICI(x).FormatDescription.Equals("JPEG")) Then
jpegICI = arrayICI(x)
Exit For
End If
Next
myEncoder = System.Drawing.Imaging.Encoder.Quality
myEncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
myEncoderParameter = New
System.Drawing.Imaging.EncoderParameter(myEncoder, 60L)
myEncoderParameters.Param(0) = myEncoderParameter
imgFoto.Save(dest, jpegICI, myEncoderParameters)
imgFoto.Dispose()
imgTmp.Dispose()

Catch ex As Exception

End Try
End Function

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs)
crobPictureBox.Enabled = True
End Sub

'Membuka citra dari sumbernya (komputer)


Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim openDlg As New System.Windows.Forms.OpenFileDialog
openDlg.Filter = "JPEG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif|Bitmap
Files (*.bmp)|*.bmp"
If openDlg.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
If Not openDlg.FileName Is Nothing Then
PreviewPictureBox.Image = System.Drawing.Bitmap.FromFile(openDlg.FileName)
crobPictureBox.Image = System.Drawing.Bitmap.FromFile(openDlg.FileName)
End If
End Sub
End Class

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click


Dim pic As Bitmap = New Bitmap(PictureBox2.Image)
Dim x As Integer
Dim y As Integer
x = pic.Width
y = pic.Height
Dim normal = New Bitmap(pic.Width, pic.Height)
Dim red As Integer
Dim green As Integer
Dim blue As Integer
Dim max, maxtemp As Integer
Dim min, mintemp As Integer
maxtemp = 0
mintemp = 255

'Mencari nilai max dan minimum serta normalisasi citra RGB


For x = 0 To (pic.Width) - 1
For y = 0 To (pic.Height) - 1
Dim r As Color = pic.GetPixel(x, y)
red = r.R
green = r.G
blue = r.B

'Mencari nilai minimum


If (red < green) And (red < blue) Then
mintemp = red
ElseIf (green < red) And (green < blue) Then
mintemp = green
ElseIf (blue < red) And (blue < green) Then
mintemp = blue
Else
min = mintemp
End If

'Mencari nilai maksimum


If (red > green) And (red > blue) Then
maxtemp = red
ElseIf (green > red) And (green > blue) Then
maxtemp = green
ElseIf (blue > red) And (blue > green) Then
maxtemp = blue
Else
max = maxtemp
End If

'Menormalkan Citra RGB


Dim rN, gN, bN As Integer
rN = CInt(255 * red / (red + green + blue + 1))
gN = CInt(255 * green / (red + green + blue + 1))
bN = CInt(255 * blue / (red + green + blue + 1))
normal.SetPixel(x, y, Color.FromArgb(rN, gN, bN))
Next
'PictureBox3.Visible = True
'PictureBox3.Image = normal
Next
'TextBox6.Text = min
'TextBox7.Text = max

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs)


Dim pic As Bitmap = New Bitmap(PictureBox3.Image)
Dim x As Integer
Dim y As Integer
x = pic.Width
y = pic.Height
Dim gray = New Bitmap(pic.Width, pic.Height)
Dim red As Integer
Dim green As Integer
Dim blue As Integer

Form1.Show()
Me.Hide()

For x = 0 To (pic.Width) - 1
For y = 0 To (pic.Height) - 1
Dim r As Color = pic.GetPixel(x, y)
red = r.R
green = r.G
blue = r.B
Dim d As Integer
d = CInt((red + green + blue) / 3)
Dim c1 As Integer = CInt(Math.Round(d))
gray.SetPixel(x, y, Color.FromArgb(c1, c1, c1))
Next
Form1.Label1.Show()
Form1.PictureBox2.Show()
Form1.PictureBox2.Image = gray
Form1.GroupBox1.Visible = True
'PictureBox2.Refresh()
Next

End Sub

You might also like