Professional Documents
Culture Documents
using
using
using
using
using
using
using
using
using
using
using
using
System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
Emgu.CV.Structure;
Emgu.CV;
HandGestureRecognition.SkinDetector;
System.Runtime.InteropServices;
namespace HandGestureRecognition
{
public partial class Form1 : Form
{
// ---PORT IO------------------[DllImport("inpout32.dll")]
private static extern UInt32 IsInpOutDriverOpen();
[DllImport("inpout32.dll")]
private static extern void Out32(short PortAddress,short
Data);
[DllImport("inpout32.dll")]
private static extern char Inp32(short PortAddress);
[DllImport("inpout32.dll")]
private static extern void DlPortWritePortUshort(short
PortAddress, ushort Data);
[DllImport("inpout32.dll")]
private static extern ushort DlPortReadPortUshort(short
PortAddress);
[DllImport("inpout32.dll")]
private static extern void DlPortWritePortUlong(int
PortAddress, uint Data);
[DllImport("inpout32.dll")]
private static extern uint DlPortReadPortUlong(int
PortAddress);
[DllImport("inpoutx64.dll")]
//---------------------------------------------------------------IColorSkinDetector skinDetector;
Image<Bgr, Byte> currentFrame;
Image<Bgr, Byte> currentFrameCopy;
Capture grabber;
AdaptiveSkinDetector detector;
int frameWidth;
int frameHeight;
Hsv
Hsv
Ycc
Ycc
hsv_min;
hsv_max;
YCrCb_min;
YCrCb_max;
Seq<Point> hull;
Seq<Point> filteredHull;
Seq<MCvConvexityDefect> defects;
MCvConvexityDefect[] defectArray;
Rectangle handRect;
MCvBox2D box;
Ellipse ellip;
public Form1() // Main Form Constructor
{
InitializeComponent(); // Initialize the Components
value = 0;
flag_dev1_on = false;
flag_dev2_on = false;
flag_dev1_off = true;
flag_dev2_off = true;
//grabber = new
Emgu.CV.Capture(@".\..\..\..\M2U00253.MPG");
// Use for Videos
grabber = new Emgu.CV.Capture();
// Object to
Capture the Frame
grabber.QueryFrame();
frameWidth = grabber.Width;
frameHeight = grabber.Height;
detector = new AdaptiveSkinDetector(1,
AdaptiveSkinDetector.MorphingMethod.NONE);
hsv_min = new Hsv(0, 45, 0); // HSV Color Space ..
hsv_max = new Hsv(20, 255, 255);
YCrCb_min = new Ycc(0, 131, 80); // YCrCb color Space
YCrCb_max = new Ycc(255, 185, 135);
box = new MCvBox2D();
ellip = new Ellipse();
Application.Idle += new EventHandler(FrameGrabber);
}
//Method for grabbing Frames captured from the camera
void FrameGrabber(object sender, EventArgs e)
{
currentFrame = grabber.QueryFrame(); // Capture
current frame
if (currentFrame != null)
{
currentFrameCopy = currentFrame.Copy();
// Uncomment if using opencv adaptive skin
detector
//Image<Gray,Byte> skin = new
Image<Gray,byte>(currentFrameCopy.Width,currentFrameCopy.Height);
//detector.Process(currentFrameCopy, skin);
skinDetector = new YCrCbSkinDetector(); //Skin
Color detector Object Initialization
Image<Gray, Byte> skin =
skinDetector.DetectSkin(currentFrameCopy,YCrCb_min,YCrCb_max); //
Skin region after Erosion & dilate
ExtractContourAndHull(skin);
Extraction
DrawAndComputeFingersNum();
// Contour
imageBoxSkin.Image = skin;
imageBoxFrameGrabber.Image = currentFrame;
int pauseTime = 1000;
System.Threading.Thread.Sleep(pauseTime);
}
}
private void ExtractContourAndHull(Image<Gray, byte>
skin)
{
using (MemStorage storage = new MemStorage())
{
Contour<Point> contours =
skin.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APP
ROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage);
Contour<Point> biggestContour = null;
Double Result1 = 0;
Double Result2 = 0;
while (contours != null)
{
Result1 = contours.Area;
if (Result1 > Result2)
{
Result2 = Result1;
biggestContour = contours;
}
contours = contours.HNext;
}
if (biggestContour != null)
{
//currentFrame.Draw(biggestContour, new Bgr(Color.DarkViolet),
2);
Contour<Point> currentContour =
biggestContour.ApproxPoly(biggestContour.Perimeter * 0.0025,
storage);
//currentFrame.Draw(currentContour, new Bgr(Color.LimeGreen), 2);
biggestContour = currentContour;
hull =
biggestContour.GetConvexHull(Emgu.CV.CvEnum.ORIENTATION.CV_CLOCKW
ISE);
box = biggestContour.GetMinAreaRect();
center,
int fingerNum = 0;
#region hull drawing
//for (int i = 0; i < filteredHull.Total; i++)
//{
//
PointF hullPoint = new
PointF((float)filteredHull[i].X,
//
(float)filteredHull[i].Y);
//
CircleF hullCircle = new CircleF(hullPoint, 4);
//
currentFrame.Draw(hullCircle, new
Bgr(Color.Aquamarine), 2);
//}
#endregion
#region defects drawing
if (defects != null)
{
for (int i = 0; i < defects.Total; i++)
{
PointF startPoint = new
PointF((float)defectArray[i].StartPoint.X,
(float)defectArray[i].StartPoint.Y);
PointF depthPoint = new
PointF((float)defectArray[i].DepthPoint.X,
(float)defectArray[i].DepthPoint.Y);
PointF endPoint = new
PointF((float)defectArray[i].EndPoint.X,
(float)defectArray[i].EndPoint.Y);
LineSegment2D startDepthLine = new
LineSegment2D(defectArray[i].StartPoint,
defectArray[i].DepthPoint);
LineSegment2D depthEndLine = new
LineSegment2D(defectArray[i].DepthPoint,
defectArray[i].EndPoint);
CircleF startCircle = new CircleF(startPoint,
5f);
{
//value = (int)Math.Pow(2, 1);
value = 1;
PortAccess.Output(888, value);
flag_dev1_on = true;
}
else if (flag_dev1_on == false && flag_dev2_on ==
true)
{
value = 3;
PortAccess.Output(888, value);
flag_dev1_on = true;
}
break;
}
case 2: // Turn OFF device 1
{
if (flag_dev1_on == true && flag_dev2_on ==
false)
{
//value = (int)Math.Pow(2, 1);
value = 0;
PortAccess.Output(888, value);
flag_dev1_on = false;
}
else if (flag_dev1_on == true && flag_dev2_on ==
true)
{
value = 2;
PortAccess.Output(888, value);
flag_dev1_on = false;
}
break;
}
case 3: //Turn ON device 2
{
if (flag_dev2_on == false && flag_dev1_on ==
false)
{
//value = (int)Math.Pow(2, 1);
value = 2;
PortAccess.Output(888, value);
flag_dev2_on = true;
}
FORM 2:
CustomYCrCbSkinDetector
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
Emgu.CV;
Emgu.CV.Structure;
namespace HandGestureRecognition.SkinDetector
{
class CustomYCrCbSkinDetector:IColorSkinDetector
{
public override Image<Gray, byte> DetectSkin(Image<Bgr,
byte> Img, IColor min, IColor max)
{
//Code adapted from here
//
http://blog.csdn.net/scyscyao/archive/2010/04/09/5468577.aspx
// Look at this paper for reference (Chinese!!!!!)
//
http://www.chinamca.com/UploadFile/200642991948257.pdf
* cb) / 32 + 51;
* cb) / 32 + 77;
* y1;
700) ? (byte)255 :
(byte)0;
else
skinData[i, j, 0] = (value < 850) ? (byte)255 :
(byte)0;
}
StructuringElementEx rect_6 = new
StructuringElementEx(6,
6, 3, 3,
Emgu.CV.CvEnum.CV_ELEMENT_SHAPE.CV_SHAPE_RECT);
CvInvoke.cvErode(skin, skin, rect_6, 1);
CvInvoke.cvDilate(skin, skin, rect_6, 2);
return skin;
}
}
}
FORM 3
HsvSkinDetector
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
Emgu.CV;
Emgu.CV.Structure;
namespace HandGestureRecognition.SkinDetector
{
public class HsvSkinDetector:IColorSkinDetector
{
FORM 4
IColorSkinDetector
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
Emgu.CV;
Emgu.CV.Structure;
namespace HandGestureRecognition.SkinDetector
{
public abstract class IColorSkinDetector
{
public abstract Image<Gray, Byte> DetectSkin(Image<Bgr,
Byte> Img, IColor min, IColor max);
}
}
FORM 5
YCrCbSkinDetector
using
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Text;
Emgu.CV.Structure;
Emgu.CV;
namespace HandGestureRecognition.SkinDetector
{