EMGU CV real time Eye tracking using C# - c#

I am following Luca Del Tongo tutorial on youtube in order to track the eyes from face. I managed to do so using rectangle but I would like to track it using HoughCircle.
https://www.youtube.com/watch?v=07QAhRJmcKQ
I am using the following code to track my eyes and it is creating multiple circles around my eyes.
I only converted the image to gray scale as he told us to do in the tutorial. Can you please help? I am new to EMGU CV
grayFrame.ROI = possibleROI_leftEye;
MCvAvgComp[][] leftEyesDetected = grayFrame.DetectHaarCascade(_eyes, 1.15, 0, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
grayFrame.ROI = Rectangle.Empty;
grayFrame.ROI = possibleROI_rightEye;
MCvAvgComp[][] rightEyesDetected = grayFrame.DetectHaarCascade(_eyes, 1.15, 0, Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING, new Size(20, 20));
grayFrame.ROI = Rectangle.Empty;
//If we are able to find eyes inside the possible face, it should be a face, maybe we find also a couple of eyes
if (leftEyesDetected[0].Length != 0 && rightEyesDetected[0].Length != 0)
{
//draw the face
frame.Draw(face.rect, new Bgr(Color.Violet), 2);
#region Hough Circles Eye Detection
grayFrame.ROI = possibleROI_leftEye;
CircleF[] leftEyecircles = grayFrame.HoughCircles(new Gray(180), new Gray(70), 5.0, 10.0, 1, 200)[0];
grayFrame.ROI = Rectangle.Empty;
foreach (CircleF circle in leftEyecircles)
{
float x = circle.Center.X + startingLeftEyePointOptimized.X;
float y = circle.Center.Y + startingLeftEyePointOptimized.Y;
frame.Draw(new CircleF(new PointF(x, y), circle.Radius), new Bgr(Color.RoyalBlue), 4);
}
grayFrame.ROI = possibleROI_rightEye;
CircleF[] rightEyecircles = grayFrame.HoughCircles(new Gray(180), new Gray(70), 2.0, 20.0, 1, 5)[0];
grayFrame.ROI = Rectangle.Empty;
foreach (CircleF circle in rightEyecircles)
{
float x = circle.Center.X + startingPointSearchEyes.X;
float y = circle.Center.Y + startingPointSearchEyes.Y;
frame.Draw(new CircleF(new PointF(x, y), circle.Radius), new Bgr(Color.RoyalBlue), 4);
}
#endregion
Now I changed the part where it finds the eyes to
grayImageFrame.ROI = possibleROI_leftEye;
CircleF[] leftEyecircles = grayImageFrame.HoughCircles(new Gray(180), new Gray(70), 5.0, 10.0, 1, 20)[0];
if (leftEyecircles.Length > 0)
{
CircleF firstCircle = leftEyecircles[0]; // Pick first circle in list
float x = firstCircle.Center.X + startingPointSearchEyes.X;
float y = firstCircle.Center.Y + startingPointSearchEyes.Y;
ImageFrame.Draw(new CircleF(new PointF(x, y), firstCircle.Radius), new Bgr(Color.RoyalBlue), 4);
}
grayImageFrame.ROI = possibleROI_rightEye;
CircleF[] rightEyecircles = grayImageFrame.HoughCircles(new Gray(180), new Gray(70), 5.0, 10.0, 1, 20)[0];
grayImageFrame.ROI = Rectangle.Empty;
if (rightEyecircles.Length > 0)
{
CircleF firstCircle = rightEyecircles[0]; // Pick first circle in list
float x = firstCircle.Center.X + startingPointSearchEyes.X;
float y = firstCircle.Center.Y + startingPointSearchEyes.Y;
ImageFrame.Draw(new CircleF(new PointF(x, y), firstCircle.Radius), new Bgr(Color.RoyalBlue), 4);
}
Only one circle is showing but it is tracking parts around my eyes not my eyes :(

The reason you get multiple circles is simply because you are drawing all found circles with this for-loop
foreach (CircleF circle in rightEyecircles)
{
float x = circle.Center.X + startingPointSearchEyes.X;
float y = circle.Center.Y + startingPointSearchEyes.Y;
frame.Draw(new CircleF(new PointF(x, y), circle.Radius), new Bgr(Color.RoyalBlue), 4);
}
To draw only one circle, you must pick a single circle from the list (or possibly a composite estimate formed by a number of circles) and draw only that.
I am not really a C# guy, but I guess something like this would work
CircleF firstCircle = rightEyecircles[0]; // Pick first circle in list
float x = firstCircle.Center.X + startingPointSearchEyes.X;
float y = firstCircle.Center.Y + startingPointSearchEyes.Y;
frame.Draw(new CircleF(new PointF(x, y), firstCircle.Radius), new Bgr(Color.RoyalBlue), 4);

Unlike Hannes, I think this can be done using image processing methods to remove the amount of detections you receive, rather than just drawing one of the found circles.
Apply a Gaussian blur to reduce noise and avoid false circle detection:
GaussianBlur( src_gray, src_gray, Size(9, 9), 2, 2 );
Apply a min/max circle radius to the Hough Transform
min_radius = 0: Minimum radio to be detected. If unknown, put zero as default.
max_radius = 0: Maximum radius to be detected. If unknown, put zero as default

For detecting one eye ball in the specific region perform following tasks
*1- Use haarcascade to detect eye and select ROI of that eye and detect hough circle there.
2- convert into gray image
3- threshold the image*
grayFrame._ThresholdBinary(new Gray(33), new Gray(255));
4- now find eye ball from hough circles.
Happy coding.

Related

SkiaSharp Calc new point coordinates after applying 3d rotation

I am using a matrix to translate then rotate in 3d (x, y, z) using the xRotate, yRotate, zRotate, depth == 300 vars.
using (var bmp = new SKBitmap(800, 600))
using (var canvas = new SKCanvas(bmp))
using (var paint = new SKPaint())
{
canvas.Clear(SKColors.White);
paint.IsAntialias = true;
// Find center of canvas
var info = bmp.Info;
float xCenter = info.Width / 2;
float yCenter = info.Height / 2;
// Translate center to origin
SKMatrix matrix = SKMatrix.MakeTranslation(-xCenter, -yCenter);
// Use 3D matrix for 3D rotations and perspective
SKMatrix44 matrix44 = SKMatrix44.CreateIdentity();
matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(1, 0, 0, xRotate));
matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 1, 0, yRotate));
matrix44.PostConcat(SKMatrix44.CreateRotationDegrees(0, 0, 1, zRotate));
SKMatrix44 perspectiveMatrix = SKMatrix44.CreateIdentity();
perspectiveMatrix[3, 2] = -1 / depth;
matrix44.PostConcat(perspectiveMatrix);
// Concatenate with 2D matrix
SKMatrix.PostConcat(ref matrix, matrix44.Matrix);
// Translate back to center
SKMatrix.PostConcat(ref matrix,
SKMatrix.MakeTranslation(xCenter, yCenter));
// Set the matrix and display the bitmap
canvas.SetMatrix(matrix);
canvas.DrawBitmap(currentImage, 50, 25, paint);
pictureBox1.Image = bmp.ToBitmap();
}
If I have some Point in the original currentImage, I want to calculate its new location after drawing the transformed image. How can I do that? Would I reuse the matrix to calculate it?
Found the answer. Let the point be (1, 2) in the currentImage. Then simply:
var newPoint = matrix.MapPoint(1, 2);
newPoint =new SkPoint(50 + newPoint.X, 25 + newPoint.Y); // + offsets of DrawImage
Or to draw on a canvas that already mapped using canvas.SetMatrix
var newPoint = new SKPoint(1, 2);
canvas.DrawCircle(newPoint.X + 50, newPoint.Y + 25, 7, paint); // + offsets of DrawImage

How to increase the height of curve in windows forms

I want to increase the height of the curve but its left and right position should remain same. Just want to lift up from center to give it a shape like curve as height changes.
Pen blackPen = new Pen(Color.Black, 3);
// Create coordinates of rectangle to bound ellipse.
int x = 93;
int y = 136;
int width = 320;
int height = 50;
// Create start and sweep angles on ellipse.
int startAngle = 0;
int sweepAngle = -180;
// Draw arc to screen.
e.Graphics.DrawArc(blackPen, x, y, width, height, startAngle, sweepAngle);
In the most direct way your problem can be solved like this:
int change = 0;
e.Graphics.DrawArc(blackPen, x, y-change, width, height+change, startAngle, sweepAngle);
By increasing the variable change the ellipse will curve up more and more:
private void button1_Click(object sender, EventArgs e)
{
change += 10;
panel1.Invalidate();
}
But maybe you want more control over the shape? Let's have a look at the options:
Here are examples of the three 'curve' drawing methods:
Curves, Beziers & Ellipses
And here is the code to draw that image.
Please ignore the Graphics.xxxTransform calls! They only are meant to shift the curves a little bit upwards so they don't overlap too much to see them properly.
Also note that the curves in the first image are not completely convex. See the last part of the answer to see a DrawCurve call that avoids the concave segments!
The important part are the Points! And just as the comments suggest, in the third part the ellipses are being changed by making the height larger and moving the top of the Rectangle up by the same amount.
The complexity DrawArc of and DrawCurve is pretty much equal; both are controlled by four integers with a rather clear meaning: They either make one rectangle or the corners of a symmetrical triangle. (Plus one counterpoint for the convex call.)
DrawBezier is more complex, especially since the controls point(s) are not actually on the resulting curve. They can be thought of force vectors that pull the line into a curved shape and are harder to calculate.
private void panel1_Paint(object sender, PaintEventArgs e)
{
Point a = new Point(0, 200);
Point c = new Point(200, 200);
for (int i = 0; i< 10; i++)
{
e.Graphics.TranslateTransform(0, -5);
Point b = new Point(100, 50 + i * 10);
e.Graphics.DrawCurve(Pens.Maroon, new[] { a, b, c }, 0.7f);
}
e.Graphics.ResetTransform();
Point pa = new Point(250, 200);
Point pb = new Point(450, 200);
for (int i = 0; i < 10; i++)
{
e.Graphics.TranslateTransform(0, -5);
Point pc = new Point(350, 200 - i * 10);
e.Graphics.DrawBezier(Pens.ForestGreen, pa, pc, pc, pb);
}
e.Graphics.ResetTransform();
int x = 500;
int y0 = 200;
int w = 200;
for (int i = 0; i < 10; i++)
{
e.Graphics.TranslateTransform(0, -5);
Rectangle rect = new Rectangle(x, y0 - i * 10, w, 10 + i * 10);
e.Graphics.DrawArc(Pens.DarkBlue, rect, -0, -180);
}
e.Graphics.ResetTransform();
}
Notes:
The Curve (1st image) can be further controlled by the Tension parameter. The lower the tension the more pointed it gets, approaching 1f it makes the curve broader..
The Bezier curve (2nd image) is using only one control point. (Twice.) The curve gets a little pointed this way. You can make it broader and broader by using two different points the move apart little by little..
The Ellipse can't be controlled; it will always fill the bounding Rectangle.
Here is an example of varying the Curves and the Beziers:
The Curves are drawn with varying Tensions. Also I have used an overload that helps to get rid of the concave part at the start and end of the curve. The trick is to add a suitable extra point to the start and end and to tell the DrawCurve to leave out these 1st and last segments.
The simplest point to use (for both ends actually) is the counterpoint of the one at the top.
The Beziers are drawn using two control points, moving out and up a little.
Here is the code for the variations:
Point a = new Point(0, 200);
Point c = new Point(200, 200);
for (int i = 1; i < 10; i++)
{
e.Graphics.TranslateTransform(0, -5);
Point b = new Point(100, 50);
Point b0 = new Point(b.X, a.Y + (a.Y - b.Y));
e.Graphics.DrawCurve(Pens.Maroon, new[] { b0, a, b, c, b0 }, 1, 2, 0.1f * i);
}
e.Graphics.ResetTransform();
Point pa = new Point(250, 200);
Point pb = new Point(450, 200);
for (int i = 0; i < 10; i++)
{
e.Graphics.TranslateTransform(0, -5);
Point ca = new Point(350 - i * 9, 100 - i * 5);
Point cb = new Point(350 + i * 9, 100 - i * 5);
e.Graphics.DrawBezier(Pens.ForestGreen, pa, ca, cb, pb);
}
e.Graphics.ResetTransform();
here was the solution just increase value of y0 as u increase the value of
y0-i here i=20
int x = 96;
int y0 = 260;
int w = 320;
e.Graphics.TranslateTransform(0, -5);
Rectangle rect = new Rectangle(x, y0 - 20 * 10, w, 10 + 20 * 10);
e.Graphics.DrawArc(Pens.DarkBlue, rect, -0, -180);
e.Graphics.ResetTransform();

how to find edge of an image with most distance from center?

i writing a sample program in c# who drawing some point in page
i set center point with calculate distance of point
but how can found most distance point from center point ?
sample code :
void draw(string label,float x,float y)
{
Graphics g = panel1.CreateGraphics();
Pen p = new Pen(Color.YellowGreen, 5);
Random randomGen = new Random(Convert.ToInt32(label));
KnownColor[] names = (KnownColor[])Enum.GetValues(typeof(KnownColor));
KnownColor randomColorName = names[randomGen.Next(names.Length)];
Color randomColor = Color.FromKnownColor(randomColorName);
SolidBrush s = new SolidBrush(randomColor);
g.FillEllipse(s, x * 1, y * 1, 10, 10);
}
The best and simplest approach for this problem what I can think of is:
1) scan the image/coordinate system horizontally and vertically
2) For each row/column store the lowest and the highest coordinates with non-zero intensity
that will be your boundary points
You can define the furthest point of an angle. This can solve the problem of convex hull, but this technique fits well only in a circle space, very like yours.
for each angle you have to find the furthest point and then assign it the red color.
You can Use as much angle as you want.
PSEUDO...
npoints = 10;
furthestPts = zeros(npoints );//Initialize vectors with 0
distances = zeros(npoints );
for each pt in points
angle = atan((pt.y - c.y)/pt.x - c.x) * 360 / ( 2 * pi); //degres
angle = (int) (angle/npoints); //we will have only 10 points separated by 36 degrees
d = distance(pt,center);
if(distances[angle] < d){
distances[angle] = d; //Updating furthest point
furthestPts.[angle] = (pt); //Updating furthest point
}
You will see that this algoritm has a few problems if the point are to far from center or if the points are to far from each other.

How can I draw a circle with antialiasing and gaussian line intensity using EmguCV (OpenCV)?

I need to draw the most perfect circle possible. EmguCV seems to lack an anti-aliasing option.
I'm trying to use SmoothGaussian, but the circle still does not look good/smooth enough.
Also, the circle line intensity should have Gaussian shape (i.e.: brighter in the center).
How can I achieve that?
Here is what I'm doing now:
using (Image<Gray, Byte> img = new Image<Gray, byte>(800, 800, new Gray(0)))
{
PointF center = new PointF(img.Width / 2, img.Height / 2);
//Center line
float r = 200.0f;
CircleF circle = new CircleF(center, r);
img.Draw(circle, new Gray(255), 1);
img._SmoothGaussian(7, 7, 3, 3);
}
If you use Brg, Brga or Gray color you can use that hack:
using(var graph = Graphics.FromImage(image.Bitmap))
{
graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
graph.DrawEllipse(new Pen(color.BackColor), x, y, d`i`ameter, diameter);
}
`
Edit:
You can also use cvInvoke
var center = new Point(x, y);
var color = new Bgr(this.color.BackColor);
CvInvoke.cvCircle(image.Ptr, center, radius, color.MCvScalar, thickness, Emgu.CV.CvEnum.LINE_TYPE.CV_AA, 0);
`

Drawing a polygon according to the input coordinates

How can i draw a polygon according to the input coordinates which are given in C#.
You didn't show any code because based on those coordinate, you are applying some form of scaling to the image.
Using the Paint event of a PictureBox, here is an example using those coordinates on the screen. It fills in the polygon, then draws the border, then it loops through all the points to draw the red circle:
void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.Clear(Color.White);
// draw the shading background:
List<Point> shadePoints = new List<Point>();
shadePoints.Add(new Point(0, pictureBox1.ClientSize.Height));
shadePoints.Add(new Point(pictureBox1.ClientSize.Width, 0));
shadePoints.Add(new Point(pictureBox1.ClientSize.Width,
pictureBox1.ClientSize.Height));
e.Graphics.FillPolygon(Brushes.LightGray, shadePoints.ToArray());
// scale the drawing larger:
using (Matrix m = new Matrix()) {
m.Scale(4, 4);
e.Graphics.Transform = m;
List<Point> polyPoints = new List<Point>();
polyPoints.Add(new Point(10, 10));
polyPoints.Add(new Point(12, 35));
polyPoints.Add(new Point(22, 35));
polyPoints.Add(new Point(24, 22));
// use a semi-transparent background brush:
using (SolidBrush br = new SolidBrush(Color.FromArgb(100, Color.Yellow))) {
e.Graphics.FillPolygon(br, polyPoints.ToArray());
}
e.Graphics.DrawPolygon(Pens.DarkBlue, polyPoints.ToArray());
foreach (Point p in polyPoints) {
e.Graphics.FillEllipse(Brushes.Red,
new Rectangle(p.X - 2, p.Y - 2, 4, 4));
}
}
}
You may use Graphics.DrawPolygon. You can store the coordinates in an array of Point and then you can pass that to DrawPolygon method. You may wanna see:
Drawing with Graphics in WinForms using C#
private System.Drawing.Graphics g;
System.Drawing.Point[] p = new System.Drawing.Point[6];
p[0].X = 0;
p[0].Y = 0;
p[1].X = 53;
p[1].Y = 111;
p[2].X = 114;
p[2].Y = 86;
p[3].X = 34;
p[3].Y = 34;
p[4].X = 165;
p[4].Y = 7;
g = PictureBox1.CreateGraphics();
g.DrawPolygon(pen1, p);
This simple function is able to generate an array of PointF equal to the vertices of the regular polygon to be drawn, where "center" is the center of the polygon, "sides" is its number of sides, "sideLength" is the size of each side in pixels and "offset" is its slope.
public PointF[] GetRegularPolygonScreenVertex(Point center, int sides, int sideLength, float offset)
{
var points = new PointF[sides];
for (int i = 0; i < sides; i++)
{
points[i] = new PointF(
(float)(center.X + sideLength * Math.Cos((i * 360 / sides + offset) * Math.PI / 180f)),
(float)(center.Y + sideLength * Math.Sin((i * 360 / sides + offset) * Math.PI / 180f))
);
}
return points;
}
The result obtained can be used to draw a polygon, e.g. with the function:
GraphicsObject.DrawPolygon(new Pen(Brushes.Black, GetRegularPolygonScreenVertex(new Point(X, Y), 6, 30, 60f));
Which will generate a regular hexagon with a side of 30 pixels inclined by 30°.
hex

Categories

Resources