public Directions Optimize(Point[] points) { DBC.Assert(points.Length > 1, "Unable to optimize a route with less than 2 points."); Log(points); if (IsSingleRouteDirections(points)) return BuildSingleRouteDirections(points); var graph = new DirectedGraph(points); var graphNavigator = new GraphNavigator(graph, points.First(), points.Last(), roadPathService); IList<Directions> cycles = graphNavigator.GetCycles(); stitchingService = new StitchingService(cycles, graph); var stitched = stitchingService.Stitch(); stitched.Order(points.First(), points.Last()); return stitched; }
private IEnumerable<Shape> _addSeriesAsPolyline(Point[] points, Brush color, double storkeThickness, bool animate = true) { if (points.Length < 2) return Enumerable.Empty<Shape>(); var addedFigures = new List<Shape>(); var l = 0d; for (var i = 1; i < points.Length; i++) { var p1 = points[i - 1]; var p2 = points[i]; l += Math.Sqrt( Math.Pow(Math.Abs(p1.X - p2.X), 2) + Math.Pow(Math.Abs(p1.Y - p2.Y), 2) ); } var f = points.First(); var p = points.Where(x => x != f); var g = new PathGeometry { Figures = new PathFigureCollection(new List<PathFigure> { new PathFigure { StartPoint = f, Segments = new PathSegmentCollection( p.Select(x => new LineSegment {Point = new Point(x.X, x.Y)})) } }) }; var path = new Path { Stroke = color, StrokeThickness = storkeThickness, Data = g, StrokeEndLineCap = PenLineCap.Round, StrokeStartLineCap = PenLineCap.Round, StrokeDashOffset = l, StrokeDashArray = new DoubleCollection { l, l }, ClipToBounds = true }; var sp = points.ToList(); sp.Add(new Point(points.Max(x => x.X), ToPlotArea(Chart.Min.Y, AxisTags.Y))); Chart.Canvas.Children.Add(path); addedFigures.Add(path); var draw = new DoubleAnimationUsingKeyFrames { BeginTime = TimeSpan.FromSeconds(0), KeyFrames = new DoubleKeyFrameCollection { new SplineDoubleKeyFrame { KeyTime = TimeSpan.FromMilliseconds(1), Value = l }, new SplineDoubleKeyFrame { KeyTime = TimeSpan.FromMilliseconds(750), Value = 0 } } }; Storyboard.SetTarget(draw, path); Storyboard.SetTargetProperty(draw, new PropertyPath(Shape.StrokeDashOffsetProperty)); var sbDraw = new Storyboard(); sbDraw.Children.Add(draw); var animated = false; if (!Chart.DisableAnimation) { if (animate) { sbDraw.Begin(); animated = true; } } if (!animated) path.StrokeDashOffset = 0; return addedFigures; }
private void captureImageGrabberEvent(object sender, EventArgs e) { try { Mat m = new Mat(); videoCapture.Retrieve(m); imgInput = m.ToImage <Bgr, Byte>(); var imgGray = imgInput.Convert <Gray, byte>().Clone(); //Конвертируем в оттенки серого imgGray._EqualizeHist(); // выравниваем ярксоть (только для черно-белого изображения) //imgGray.SmoothGaussian(4); imgGray._SmoothGaussian(1); //гауусовский фильтр VectorOfRect faces = new VectorOfRect(faceFrontalDetector.DetectMultiScale(imgGray, 1.1, 3, new Size(100, 100), new Size(300, 300))); //находим все лица if (faces.Size > 0) { Rectangle[] facesRect = faces.ToArray();// приводим вектор к фигуре //facesRect[0] Array.Sort(facesRect, new Comparison <Rectangle>(CompareShapes)); //сортировка по высоте VectorOfVectorOfPointF landmarks = new VectorOfVectorOfPointF(); bool success = facemark.Fit(imgGray, faces, landmarks);//находим точки лица double k = (double)faces[0].Width / imgGray.Width; double s = (matrixCam * avarageFaceSize) / k; PointF[] h = landmarks[0].ToArray();//преобразовываем в массив точек //var point = h.Select(p => new Point(Convert.ToInt32(p.X), Convert.ToInt32(p.Y))); Point[] rightEye = new Point[6]; rightEye[0].X = Convert.ToInt32(Math.Round(h[36].X)); rightEye[0].Y = Convert.ToInt32(Math.Round(h[36].Y)); rightEye[1].X = Convert.ToInt32(Math.Round(h[37].X)); rightEye[1].Y = Convert.ToInt32(Math.Round(h[37].Y)); rightEye[2].X = Convert.ToInt32(Math.Round(h[38].X)); rightEye[2].Y = Convert.ToInt32(Math.Round(h[38].Y)); rightEye[3].X = Convert.ToInt32(Math.Round(h[39].X)); rightEye[3].Y = Convert.ToInt32(Math.Round(h[39].Y)); rightEye[4].X = Convert.ToInt32(Math.Round(h[40].X)); rightEye[4].Y = Convert.ToInt32(Math.Round(h[40].Y)); rightEye[5].X = Convert.ToInt32(Math.Round(h[41].X)); rightEye[5].Y = Convert.ToInt32(Math.Round(h[41].Y)); var rightEyeMinimalPoint = rightEye.First(x => x.Y == rightEye.Max(y => y.Y)); var rightEyeMaximalPoint = rightEye.First(x => x.Y == rightEye.Min(y => y.Y)); Point[] leftEye = new Point[6]; leftEye[0].X = Convert.ToInt32(Math.Round(h[42].X)); leftEye[0].Y = Convert.ToInt32(Math.Round(h[42].Y)); leftEye[1].X = Convert.ToInt32(Math.Round(h[43].X)); leftEye[1].Y = Convert.ToInt32(Math.Round(h[43].Y)); leftEye[2].X = Convert.ToInt32(Math.Round(h[44].X)); leftEye[2].Y = Convert.ToInt32(Math.Round(h[44].Y)); leftEye[3].X = Convert.ToInt32(Math.Round(h[45].X)); leftEye[3].Y = Convert.ToInt32(Math.Round(h[45].Y)); leftEye[4].X = Convert.ToInt32(Math.Round(h[46].X)); leftEye[4].Y = Convert.ToInt32(Math.Round(h[46].Y)); leftEye[5].X = Convert.ToInt32(Math.Round(h[47].X)); leftEye[5].Y = Convert.ToInt32(Math.Round(h[47].Y)); var leftEyeMinimalPoint = leftEye.First(x => x.Y == leftEye.Max(y => y.Y)); var leftEyeMaximalPoint = leftEye.First(x => x.Y == leftEye.Min(y => y.Y)); imgInput.DrawPolyline(leftEye, true, new Bgr(Color.Blue), 1); imgInput.DrawPolyline(rightEye, true, new Bgr(Color.Blue), 1); ////for (int i = 0; i < h.Length; ++i) ////{ //// mas[i].X = Convert.ToInt32(Math.Round(h[i].X)); //// mas[i].Y = Convert.ToInt32(Math.Round(h[i].Y)); ////} //imgInput.Draw(h[37], new Bgr(Color.Blue), 2); //imgInput.Draw(new CircleF(mas[41], 10), new Bgr(Color.Gray), 1); //imgInput.DrawPolyline(mas, true, new Bgr(Color.Blue), 2); //////if (success) //////{ ////// facesRect = faces.ToArray(); ////// for (int i = 0; i < facesRect.Length; i++) ////// { ////// imgInput.Draw(facesRect[0], new Bgr(Color.Blue), 2); ////// FaceInvoke.DrawFacemarks(imgInput, landmarks[i], new Bgr(Color.Blue).MCvScalar); ////// } ////// //return imgInput; //////} //return null; //int cropHeight = (int)Math.Round(facesRect[0].Height * 0.5, 0); //Rectangle eyesAreaRec = new Rectangle(facesRect[0].X, (int)Math.Round(facesRect[0].Y * 1.4), facesRect[0].Width, cropHeight); //for (int i = 0; i < facesRect.Length; i++) //{ // imgInput.Draw(facesRect[0], new Bgr(Color.Blue), 2); // //FaceInvoke.DrawFacemarks(imgInput, landmarks[i], new Bgr(Color.Blue).MCvScalar); //} //imgInput.Draw(eyesAreaRec, new Bgr(Color.Blue), 2); Invoke(new Action(() => { if (faces.Size > 0) { faceSizePixelEdit.Text = faces[0].Size.Height.ToString(); camLengthEdit.Text = s.ToString(); arcScaleComponent1.Value = faces[0].Size.Height; if (rightEyeMaximalPoint.Y > 0 && rightEyeMinimalPoint.Y > 0) { rightEyeEdit.Text = Convert.ToString(rightEyeMinimalPoint.Y - rightEyeMaximalPoint.Y); } } })); //faceSizePixelEdit.Text = Convert.ToString(k); //Writelabel(0); generalBox.Image = imgInput; } else if (faces.Size == 0) { //faces = new VectorOfRect(faceProfilDetector.DetectMultiScale(imgGray)); generalBox.Image = imgInput; } //VectorOfRect faces = new VectorOfRect(faceDetector.DetectMultiScale(grayImage)); //VectorOfVectorOfPointF landmarks = new VectorOfVectorOfPointF(); //bool success = facemark.Fit(grayImage, faces, landmarks); //PointF[][] f = landmarks.ToArrayOfArray(); //if (success) //{ // Rectangle[] facesRect = faces.ToArray(); // for (int i = 0; i < facesRect.Length; i++) // { // imgInput.Draw(facesRect[0], new Bgr(Color.Blue), 2); // FaceInvoke.DrawFacemarks(imgInput, landmarks[i], new Bgr(Color.Blue).MCvScalar); // } // return imgInput; //} //return null; //if (faces.Length > 0) // faceSizePixelEdit.Text = Convert.ToString(faces[0].Size.Height * faces[0].Size.Width); //faceSizePixelEdit.Text = faces[0].Size.Height.ToString(); //videoCapture.Pause(); //videoCapture.Start(); //////var imgGray = imgInput.Convert<Gray, byte>().Clone(); //////Rectangle[] faces = classifierFace.DetectMultiScale(imgGray, 1.1, 4); //////foreach (var face in faces) //////{ ////// imgInput.Draw(face, new Bgr(0, 0, 255), 2); ////// imgGray.ROI = face; ////// Rectangle[] eyes = classifierEye.DetectMultiScale(imgGray, 1.1, 4); ////// foreach (var eye in eyes) ////// { ////// var ec = eye; ////// ec.X += face.X; ////// ec.Y += face.Y; ////// imgInput.Draw(ec, new Bgr(0, 255, 0), 2); ////// } //////} Thread.Sleep(1); //imageInput = //pictureBox1.Image = m.ToImage<Bgr, byte>().Bitmap; } catch (Exception ex) { MessageBox.Show(ex.InnerException.Message); } }
public static void Main(string[] args) { bool inError = false; string[] xs = args[0].Replace("\"", string.Empty).Split(','); string[] ys = args[1].Replace("\"", string.Empty).Split(','); error |= (xs.Length != ys.Length); error |= (xs.Length < 3); // Checking data Point[] points = new Point[xs.Length]; for (int i = 0; i < points.Length && !error; i++) { int x = 0, y = 0; error |= !(int.TryParse(xs[i], out x)); error |= !(int.TryParse(ys[i], out y)); points[i] = new Point { X = x, Y = y }; } if (error) { ErrorAndExit(); } List <Point> convexHull = new List <Point>(); // Algorithm start (Jarvis' Algorithm) int minX = points.Min(pt => pt.X); Point leftmost = points.First(pt => pt.X == minX); // I don't care if there are more than one convexHull.Add(leftmost); // Jarvis' Algorithm states that to determine the next point in the convex hull you need // to find the rightmost point, compared to the last one identified. // i.e. given P the last point and Q the one in exam, there is no point R in the remaining // set that creates a clockwise triangle PQR Point p = leftmost; Point nextInHull = null; while (true) { nextInHull = null; foreach (Point q in points) { if (q == p) { continue; } // if I find at least one point clockwise, then it's not the rightmost one if (points.Any(r => { if (r == q || r == p) { return(false); } return(IsClockwise(p, q, r)); })) { continue; } nextInHull = q; break; // I found the right one, just stop here } if (nextInHull != leftmost) { convexHull.Add(nextInHull); p = nextInHull; } else { break; } } Console.WriteLine("Convex Hull:"); foreach (Point pt in convexHull) { Console.WriteLine("({0}, {1})", pt.X, pt.Y); } Console.ReadKey(); }
private void DrawLines(DrawingContext drawingContext, Pen pen, Point[] points) { var pathGeometry = new PathGeometry(); var figure = new PathFigure(points.First(), points.Skip(1).Select(p => new LineSegment(p, true)), true); pathGeometry.Figures = new PathFigureCollection { figure }; drawingContext.DrawGeometry(null, pen, pathGeometry); }
private void imageGraberCapture(object sender, EventArgs e) { try { #region Получение кадра и массива лиц Mat m = new Mat(); videoCapture.Retrieve(m); Image <Bgr, Byte> inputImage = m.ToImage <Bgr, Byte>(); var imgGray = inputImage.Convert <Gray, Byte>().Clone(); //Конвертируем в оттенки серого imgGray._EqualizeHist(); //выравниваем яркость imgGray._SmoothGaussian(1); //убираем шумы VectorOfRect faces = new VectorOfRect(faceFrontalDetector.DetectMultiScale(imgGray, 1.05, 5, new Size(120, 120))); //находим все лица #endregion if (faces.Size > 0) { Rectangle[] facesRect = faces.ToArray(); // приводим вектор к фигуре facesRect.OrderBy(ob => ob.Height); //сортировка по высоте VectorOfVectorOfPointF landmarks = new VectorOfVectorOfPointF(); bool success = facemark.Fit(imgGray, faces, landmarks); //находим точки лица #region Анализ расстояния до пользователя double k = ((double)faces[0].Height) / imgGray.Height; currentDistance = (2.5 * avarageFaceSize) / k; #endregion #region Анализ яркости imageBrightList = FrameBright(imgGray.Clone(), facesRect[0]); if ((imageBrightList[1] - imageBrightList[0]) > 55) { currentBright = 10; } else if ((imageBrightList[1] - imageBrightList[0]) > 45 && (imageBrightList[1] - imageBrightList[0]) < 55) { currentBright = 20; } else if ((imageBrightList[1] - imageBrightList[0]) > 35 && (imageBrightList[1] - imageBrightList[0]) < 45) { currentBright = 35; } else if ((imageBrightList[1] - imageBrightList[0]) > 25 && (imageBrightList[1] - imageBrightList[0]) < 35) { currentBright = 50; } else if ((imageBrightList[1] - imageBrightList[0]) > 15 && (imageBrightList[1] - imageBrightList[0]) < 25) { currentBright = 70; } else if ((imageBrightList[1] - imageBrightList[0]) > 10 && (imageBrightList[1] - imageBrightList[0]) < 15) { currentBright = 85; } else if ((imageBrightList[1] - imageBrightList[0]) > 5 && (imageBrightList[1] - imageBrightList[0]) < 10) { currentBright = 95; } //currentBright = (int)(((100.0/255)*imageBrightList[0]) + 0.5); #endregion #region Глазной анализатор PointF[] h = landmarks[0].ToArray();//преобразовываем в массив точек Point[] rightEye = new Point[6]; rightEye[0].X = Convert.ToInt32(Math.Round(h[36].X)); rightEye[0].Y = Convert.ToInt32(Math.Round(h[36].Y)); rightEye[1].X = Convert.ToInt32(Math.Round(h[37].X)); rightEye[1].Y = Convert.ToInt32(Math.Round(h[37].Y)); rightEye[2].X = Convert.ToInt32(Math.Round(h[38].X)); rightEye[2].Y = Convert.ToInt32(Math.Round(h[38].Y)); rightEye[3].X = Convert.ToInt32(Math.Round(h[39].X)); rightEye[3].Y = Convert.ToInt32(Math.Round(h[39].Y)); rightEye[4].X = Convert.ToInt32(Math.Round(h[40].X)); rightEye[4].Y = Convert.ToInt32(Math.Round(h[40].Y)); rightEye[5].X = Convert.ToInt32(Math.Round(h[41].X)); rightEye[5].Y = Convert.ToInt32(Math.Round(h[41].Y)); var rightEyeMinimalPoint = rightEye.First(x => x.Y == rightEye.Max(y => y.Y)); var rightEyeMaximalPoint = rightEye.First(x => x.Y == rightEye.Min(y => y.Y)); Point[] leftEye = new Point[6]; leftEye[0].X = Convert.ToInt32(Math.Round(h[42].X)); leftEye[0].Y = Convert.ToInt32(Math.Round(h[42].Y)); leftEye[1].X = Convert.ToInt32(Math.Round(h[43].X)); leftEye[1].Y = Convert.ToInt32(Math.Round(h[43].Y)); leftEye[2].X = Convert.ToInt32(Math.Round(h[44].X)); leftEye[2].Y = Convert.ToInt32(Math.Round(h[44].Y)); leftEye[3].X = Convert.ToInt32(Math.Round(h[45].X)); leftEye[3].Y = Convert.ToInt32(Math.Round(h[45].Y)); leftEye[4].X = Convert.ToInt32(Math.Round(h[46].X)); leftEye[4].Y = Convert.ToInt32(Math.Round(h[46].Y)); leftEye[5].X = Convert.ToInt32(Math.Round(h[47].X)); leftEye[5].Y = Convert.ToInt32(Math.Round(h[47].Y)); var leftEyeMinimalPoint = leftEye.First(x => x.Y == leftEye.Max(y => y.Y)); var leftEyeMaximalPoint = leftEye.First(x => x.Y == leftEye.Min(y => y.Y)); if (rightEyeMaximalPoint.Y > 0 && rightEyeMinimalPoint.Y > 0 && leftEyeMaximalPoint.Y > 0 && leftEyeMinimalPoint.Y > 0) { double bufer; inputImage.DrawPolyline(leftEye, true, new Bgr(Color.Red), 2); inputImage.DrawPolyline(rightEye, true, new Bgr(Color.Red), 2); bufer = rightEyeMinimalPoint.Y - rightEyeMaximalPoint.Y; double kk = ((double)bufer) / imgGray.Height; curentRightEye = (currentDistance * kk) / 2.5; //в сантиметрах if (curentRightEye > heightOpenRightEye) { curentRightEye = heightOpenRightEye; } else if (curentRightEye < heightCloseRightEye * chit) { curentRightEye = heightCloseRightEye; blink++; clipCounter++; } curentLeftEye = curentRightEye; } else { curentLeftEye = heightCloseRightEye; curentRightEye = heightCloseRightEye; } double?buffer = ((100 / (heightOpenLeftEye - heightCloseLeftEye)) * (curentRightEye - heightCloseLeftEye)); #endregion Invoke(new Action(() => { if (faces.Size > 0) { //brightGaugComponent.Value = (float)60; distanceGaugComponent.Value = (float)currentDistance; //график света linearScaleMarkerComponent3.Value = (float)currentBright; linearScaleRangeBarComponent2.Value = (float)currentBright; //lengthEdit.Text = test.ToString(); //faceSizeEdit.Text = curentRightEye.ToString(); //график глаза linearScaleRangeBarComponent3.Value = (float)buffer; linearScaleMarkerComponent4.Value = (float)buffer; //linearScaleRangeBarComponent1.Value = 60; } })); inputImage.Draw(faces[0], new Bgr(Color.Green), 1); generalTabBox.Image = inputImage; } else if (faces.Size == 0) { //faces = new VectorOfRect(faceProfilDetector.DetectMultiScale(imgGray)); generalTabBox.Image = inputImage; } Thread.Sleep(1); } catch (Exception ex) { MessageBox.Show(ex.InnerException.Message); } }