Professional Documents
Culture Documents
Skin Detection
Skin Detection
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Namespace MachineVision
End Sub
Public Shared Sub DetectEdges(ByVal original As Bitmap, ByRef modified As Bitmap, ByVal c As Color,
ByVal noise As Integer)
Try
If totalCurrent > totalLeft + noise OrElse totalCurrent > totalRight + noise Then
Else
modified.SetPixel(x, y, Color.Black)
End If 'black
modified.SetPixel(x, y - 1, Color.White)
modified.SetPixel(x, y + 1, Color.White)
Else
modified.SetPixel(x, y, Color.Black)
End If
modified.SetPixel(x - 1, y - 1, Color.White)
modified.SetPixel(x + 1, y + 1, Color.White)
Else
modified.SetPixel(x, y, Color.Black)
End If
modified.SetPixel(x + 1, y - 1, Color.White)
modified.SetPixel(x - 1, y + 1, Color.White)
Else
modified.SetPixel(x, y, Color.Black)
End If
End Try
End If
Next
Next
End Sub
''' <summary>
Public Shared Sub OutLineEdges(ByVal original As Bitmap, ByRef modified As Bitmap, ByVal c As
Color, ByVal noise As Integer)
Try
If totalCurrent > totalLeft + noise OrElse totalCurrent > totalRight + noise Then
modified.SetPixel(x - 1, y, c)
modified.SetPixel(x + 1, y, c)
End If
If totalUpOne > totalCurrent + noise Then '|| totalDownOne > (totalCurrent + 75))
modified.SetPixel(x, y - 1, c)
modified.SetPixel(x, y + 1, c)
End If
modified.SetPixel(x - 1, y - 1, c)
modified.SetPixel(x + 1, y + 1, c)
End If
modified.SetPixel(x + 1, y - 1, c)
End If
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,
''' </summary>
' convert RGB color space to IRgBy color space using this formula:
Rg = L(R) - L(G)
'r = 255;
Else
modified.SetPixel(x, y, Color.Black)
End If
Next
Next
'SortPoints(ref points);
End Sub
'int x = 4;
Else
swap(pts, i - 1, i)
End If
Next
'--x;
End Sub
Private Shared Sub swap(ByRef pts As ArrayList, ByVal a As Integer, ByVal b As Integer)
temp = pA
pA = pB
pB = temp
End Sub
Return r
Return g
Else
Return b
End If
End Function
Return r
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);
}
}
}
}
///<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);
}
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++)
{
}
}
//SortPoints(ref points);
//PlotLines(Graphics.FromImage(modified), (Point)points[0],
(Point)points[points.Count - 1]);
}
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;
}
}
}
Imports System.Drawing.Drawing2D
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))
PreviewPictureBox.Image = bm_dest
tempCnt = True
Catch ex As Exception
End Try
End Sub
#End Region
cropX = e.X
cropY = e.Y
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
crobPictureBox.Refresh()
cropWidth = e.X - cropX
cropHeight = e.Y - cropY
crobPictureBox.CreateGraphics.DrawRectangle(cropPen, cropX, cropY,
cropWidth, cropHeight)
End If
' GC.Collect()
End Sub
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
Catch ex As Exception
End Try
End Function
End Sub
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