public DistanceTo ( Point anotherPoint ) : float | ||
anotherPoint | Point | Point to calculate distance to. |
Результат | float |
/// <summary> /// Gets two points defining the axis of the object. /// </summary> /// public LineSegment GetAxis(AxisOrientation axis) { double x1, y1; double x2, y2; if (axis == AxisOrientation.Horizontal) { y1 = Math.Cos(-Angle - Math.PI) * Rectangle.Height / 2.0; x1 = Math.Sin(-Angle - Math.PI) * Rectangle.Width / 2.0; y2 = Math.Cos(-Angle) * Rectangle.Height / 2.0; x2 = Math.Sin(-Angle) * Rectangle.Width / 2.0; } else { y1 = Math.Cos(-(Angle + Math.PI / 2) - Math.PI) * Rectangle.Height / 2.0; x1 = Math.Sin(-(Angle + Math.PI / 2) - Math.PI) * Rectangle.Width / 2.0; y2 = Math.Cos(-(Angle + Math.PI / 2)) * Rectangle.Height / 2.0; x2 = Math.Sin(-(Angle + Math.PI / 2)) * Rectangle.Width / 2.0; } Point start = new Point((float)(Center.X + x1), (float)(Center.Y + y1)); Point end = new Point((float)(Center.X + x2), (float)(Center.Y + y2)); if (start.DistanceTo(end) == 0) { return(null); } return(new LineSegment(start, end)); }
// ========================================================================================================== // Components: // ========================================================================================================== private List<Shapes.Component> FindComponentsFunct(Bitmap bitmap) { // Locating objects BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = 8; blobCounter.MinWidth = 8; blobCounter.ProcessImage(bitmap); Blob[] blobs = blobCounter.GetObjectsInformation(); // create convex hull searching algorithm GrahamConvexHull hullFinder = new GrahamConvexHull(); ClosePointsMergingOptimizer optimizer1 = new ClosePointsMergingOptimizer(); FlatAnglesOptimizer optimizer2 = new FlatAnglesOptimizer(); List<Shapes.Component> Components = new List<Shapes.Component>(); // process each blob foreach (Blob blob in blobs) { List<IntPoint> leftPoints, rightPoints, edgePoints = new List<IntPoint>(); if ((blob.Rectangle.Height > 400) && (blob.Rectangle.Width > 600)) { break; // The whole image could be a blob, discard that } // get blob's edge points blobCounter.GetBlobsLeftAndRightEdges(blob, out leftPoints, out rightPoints); edgePoints.AddRange(leftPoints); edgePoints.AddRange(rightPoints); // blob's convex hull List<IntPoint> Outline = hullFinder.FindHull(edgePoints); optimizer1.MaxDistanceToMerge = 4; optimizer2.MaxAngleToKeep = 170F; Outline = optimizer2.OptimizeShape(Outline); Outline = optimizer1.OptimizeShape(Outline); // find Longest line segment float dist = 0; LineSegment Longest = new LineSegment(Outline[0], Outline[1]); LineSegment line; dist = Longest.Length; int LongestInd = 0; for (int i = 1; i < Outline.Count; i++) { if (i != Outline.Count - 1) { line = new LineSegment(Outline[i], Outline[i + 1]); } else { // last iteration if (Outline[i] == Outline[0]) { break; } line = new LineSegment(Outline[i], Outline[0]); } if (line.Length > dist) { Longest = line; dist = line.Length; LongestInd = i; } } // Get the center point of it AForge.Point LongestCenter = new AForge.Point(); LongestCenter.X = (float)Math.Round((Longest.End.X - Longest.Start.X) / 2.0 + Longest.Start.X); LongestCenter.Y = (float)Math.Round((Longest.End.Y - Longest.Start.Y) / 2.0 + Longest.Start.Y); AForge.Point NormalStart = new AForge.Point(); AForge.Point NormalEnd = new AForge.Point(); // Find normal: // start= longest.start rotated +90deg relative to center // end= longest.end rotated -90deg and relative to center // If you rotate point (px, py) around point (ox, oy) by angle theta you'll get: // p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox // p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy // cos90 = 0, sin90= 1 => // p'x= -(py-oy) + ox= oy-py+ox, p'y= (px-ox)+ oy NormalStart.X = LongestCenter.Y - Longest.Start.Y + LongestCenter.X; NormalStart.Y = (Longest.Start.X - LongestCenter.X) + LongestCenter.Y; // cos-90=0, sin-90= -1 => // p'x= (py-oy) + ox // p'y= -(px-ox)+oy= ox-px+oy NormalEnd.X = (Longest.Start.Y - LongestCenter.Y) + LongestCenter.X; NormalEnd.Y = LongestCenter.X - Longest.Start.X + LongestCenter.Y; // Make line out of the points Line Normal = Line.FromPoints(NormalStart, NormalEnd); // Find the furthest intersection to the normal (skip the Longest) AForge.Point InterSection = new AForge.Point(); AForge.Point Furthest = new AForge.Point(); bool FurhtestAssinged = false; LineSegment seg; dist = 0; for (int i = 0; i < Outline.Count; i++) { if (i == LongestInd) { continue; } if (i != Outline.Count - 1) { seg = new LineSegment(Outline[i], Outline[i + 1]); } else { // last iteration if (Outline[i] == Outline[0]) { break; } seg = new LineSegment(Outline[i], Outline[0]); } if (seg.GetIntersectionWith(Normal) == null) { continue; } InterSection = (AForge.Point)seg.GetIntersectionWith(Normal); if (InterSection.DistanceTo(LongestCenter) > dist) { Furthest = InterSection; FurhtestAssinged = true; dist = InterSection.DistanceTo(LongestCenter); } } // Check, if there is a edge point that is close to the normal even further AForge.Point fPoint = new AForge.Point(); for (int i = 0; i < Outline.Count; i++) { fPoint.X = Outline[i].X; fPoint.Y = Outline[i].Y; if (Normal.DistanceToPoint(fPoint) < 1.5) { if (fPoint.DistanceTo(LongestCenter) > dist) { Furthest = fPoint; FurhtestAssinged = true; dist = fPoint.DistanceTo(LongestCenter); } } } AForge.Point ComponentCenter = new AForge.Point(); if (FurhtestAssinged) { // Find the midpoint of LongestCenter and Furthest: This is the centerpoint of component ComponentCenter.X = (float)Math.Round((LongestCenter.X - Furthest.X) / 2.0 + Furthest.X); ComponentCenter.Y = (float)Math.Round((LongestCenter.Y - Furthest.Y) / 2.0 + Furthest.Y); // Alignment is the angle of longest double Alignment; if (Math.Abs(Longest.End.X - Longest.Start.X) < 0.001) { Alignment = 0; } else { Alignment = Math.Atan((Longest.End.Y - Longest.Start.Y) / (Longest.End.X - Longest.Start.X)); Alignment = Alignment * 180.0 / Math.PI; // in deg. } Components.Add(new Shapes.Component(ComponentCenter, Alignment, Outline, Longest, NormalStart, NormalEnd)); } } return Components; }
/// <summary> /// Creates a new <see cref="Circle"/> from three non-linear points. /// </summary> /// /// <param name="p1">The first point.</param> /// <param name="p2">The second point.</param> /// <param name="p3">The third point.</param> /// public Circle(Point p1, Point p2, Point p3) { // ya = ma * (x - x1) + y1 // yb = mb * (x - x2) + y2 // // ma = (y2 - y1) / (x2 - x1) // mb = (y3 - y2) / (x3 - x2) double ma = (p2.Y - p1.Y) / (p2.X - p1.X); double mb = (p3.Y - p2.Y) / (p3.X - p2.X); // (ma * mb * (y1 - y3) + mb * (x1 + x2) - ma * (x2 + x3) // x = ---------------------------------------------------------- // 2 * (mb - ma) double x = (ma * mb * (p1.Y - p3.Y) + mb * (p1.X + p2.Y) - ma * (p2.X + p3.X)) / (2 * (mb - ma)); double y = ma * (x - p1.X) + p1.Y; Origin = new Point((float)x, (float)y); Radius = Origin.DistanceTo(p1); }
/// <summary> /// Gets two points defining the axis of the object. /// </summary> /// public LineSegment GetAxis(AxisOrientation axis) { double x1, y1; double x2, y2; if (axis == AxisOrientation.Horizontal) { y1 = Math.Cos(-Angle - Math.PI) * Rectangle.Height / 2.0; x1 = Math.Sin(-Angle - Math.PI) * Rectangle.Width / 2.0; y2 = Math.Cos(-Angle) * Rectangle.Height / 2.0; x2 = Math.Sin(-Angle) * Rectangle.Width / 2.0; } else { y1 = Math.Cos(-(Angle + Math.PI / 2) - Math.PI) * Rectangle.Height / 2.0; x1 = Math.Sin(-(Angle + Math.PI / 2) - Math.PI) * Rectangle.Width / 2.0; y2 = Math.Cos(-(Angle + Math.PI / 2)) * Rectangle.Height / 2.0; x2 = Math.Sin(-(Angle + Math.PI / 2)) * Rectangle.Width / 2.0; } Point start = new Point((float)(Center.X + x1), (float)(Center.Y + y1)); Point end = new Point((float)(Center.X + x2), (float)(Center.Y + y2)); if (start.DistanceTo(end) == 0) return null; return new LineSegment(start, end); }
public static List <Shapes.Component> FindComponents(VideoProcessing vp, Bitmap bitmap) { // Locating objects BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinHeight = 8; blobCounter.MinWidth = 8; blobCounter.ProcessImage(bitmap); Blob[] blobs = blobCounter.GetObjectsInformation(); // create convex hull searching algorithm GrahamConvexHull hullFinder = new GrahamConvexHull(); ClosePointsMergingOptimizer optimizer1 = new ClosePointsMergingOptimizer(); FlatAnglesOptimizer optimizer2 = new FlatAnglesOptimizer(); List <Shapes.Component> Components = new List <Shapes.Component>(); // process each blob foreach (Blob blob in blobs) { List <IntPoint> leftPoints, rightPoints, edgePoints = new List <IntPoint>(); if ((blob.Rectangle.Height > 400) && (blob.Rectangle.Width > 600)) { break; // The whole image could be a blob, discard that } // get blob's edge points blobCounter.GetBlobsLeftAndRightEdges(blob, out leftPoints, out rightPoints); edgePoints.AddRange(leftPoints); edgePoints.AddRange(rightPoints); // blob's convex hull List <IntPoint> Outline = hullFinder.FindHull(edgePoints); optimizer1.MaxDistanceToMerge = 4; optimizer2.MaxAngleToKeep = 170F; Outline = optimizer2.OptimizeShape(Outline); Outline = optimizer1.OptimizeShape(Outline); // find Longest line segment float dist = 0; LineSegment Longest = new LineSegment(Outline[0], Outline[1]); LineSegment line; dist = Longest.Length; int LongestInd = 0; for (int i = 1; i < Outline.Count; i++) { if (i != Outline.Count - 1) { line = new LineSegment(Outline[i], Outline[i + 1]); } else { // last iteration if (Outline[i] == Outline[0]) { break; } line = new LineSegment(Outline[i], Outline[0]); } if (line.Length > dist) { Longest = line; dist = line.Length; LongestInd = i; } } // Get the center point of it Point LongestCenter = new Point(); LongestCenter.X = (float)Math.Round((Longest.End.X - Longest.Start.X) / 2.0 + Longest.Start.X); LongestCenter.Y = (float)Math.Round((Longest.End.Y - Longest.Start.Y) / 2.0 + Longest.Start.Y); Point NormalStart = new Point(); Point NormalEnd = new Point(); // Find normal: // start= longest.start rotated +90deg relative to center // end= longest.end rotated -90deg and relative to center // If you rotate point (px, py) around point (ox, oy) by angle theta you'll get: // p'x = cos(theta) * (px-ox) - sin(theta) * (py-oy) + ox // p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy // cos90 = 0, sin90= 1 => // p'x= -(py-oy) + ox= oy-py+ox, p'y= (px-ox)+ oy NormalStart.X = LongestCenter.Y - Longest.Start.Y + LongestCenter.X; NormalStart.Y = (Longest.Start.X - LongestCenter.X) + LongestCenter.Y; // cos-90=0, sin-90= -1 => // p'x= (py-oy) + ox // p'y= -(px-ox)+oy= ox-px+oy NormalEnd.X = (Longest.Start.Y - LongestCenter.Y) + LongestCenter.X; NormalEnd.Y = LongestCenter.X - Longest.Start.X + LongestCenter.Y; // Make line out of the points Line Normal = Line.FromPoints(NormalStart, NormalEnd); // Find the furthest intersection to the normal (skip the Longest) Point InterSection = new Point(); Point Furthest = new Point(); bool FurhtestAssinged = false; LineSegment seg; dist = 0; for (int i = 0; i < Outline.Count; i++) { if (i == LongestInd) { continue; } if (i != Outline.Count - 1) { seg = new LineSegment(Outline[i], Outline[i + 1]); } else { // last iteration if (Outline[i] == Outline[0]) { break; } seg = new LineSegment(Outline[i], Outline[0]); } if (seg.GetIntersectionWith(Normal) == null) { continue; } InterSection = (Point)seg.GetIntersectionWith(Normal); if (InterSection.DistanceTo(LongestCenter) > dist) { Furthest = InterSection; FurhtestAssinged = true; dist = InterSection.DistanceTo(LongestCenter); } } // Check, if there is a edge point that is close to the normal even further Point fPoint = new Point(); for (int i = 0; i < Outline.Count; i++) { fPoint.X = Outline[i].X; fPoint.Y = Outline[i].Y; if (Normal.DistanceToPoint(fPoint) < 1.5) { if (fPoint.DistanceTo(LongestCenter) > dist) { Furthest = fPoint; FurhtestAssinged = true; dist = fPoint.DistanceTo(LongestCenter); } } } Point ComponentCenter = new Point(); if (FurhtestAssinged) { // Find the midpoint of LongestCenter and Furthest: This is the centerpoint of component ComponentCenter.X = (float)Math.Round((LongestCenter.X - Furthest.X) / 2.0 + Furthest.X); ComponentCenter.Y = (float)Math.Round((LongestCenter.Y - Furthest.Y) / 2.0 + Furthest.Y); // Alignment is the angle of longest double Alignment; if (Math.Abs(Longest.End.X - Longest.Start.X) < 0.001) { Alignment = 0; } else { Alignment = Math.Atan((Longest.End.Y - Longest.Start.Y) / (Longest.End.X - Longest.Start.X)); Alignment = Alignment * 180.0 / Math.PI; // in deg. } Components.Add(new Shapes.Component(ComponentCenter, Alignment, Outline, Longest, NormalStart, NormalEnd)); } } SetVideoProcessing(Components, vp); return(Components); }
// New frame received by the player private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image) { lock (this) { statistica++; for (int i = 0; i < ObjMot.Count; i++) { ObjMot[i].SostTime = false; if (ObjMot[i].kolvo > 60) { ObjMot[i].SostWork = false; } } float motionLevel = detector.ProcessFrame(image); if (motionLevel > motionAlarmLevel) { // flash for 2 seconds flash = (int)(2 * (1000 / alarmTimer.Interval)); } // check objects' count if (detector.MotionProcessingAlgorithm is BlobCountingObjectsProcessing) { BlobCountingObjectsProcessing countingDetector = (BlobCountingObjectsProcessing)detector.MotionProcessingAlgorithm; detectedObjectsCount = countingDetector.ObjectsCount; Rectangle[] kvadr = countingDetector.ObjectRectangles; // ObjectMotion OM = new ObjectMotion(); MyPoint point = new MyPoint(); for (int i = 0; i < detectedObjectsCount; i++) { point.X = kvadr[i].X + kvadr[i].Width / 2; point.Y = kvadr[i].Y + kvadr[i].Height / 2; float MinLeg = 150; //OM.Route.Add(point); //ObjMot.Add(OM); bool flag = true; for (int j = 0; j < ObjMot.Count; j++) { if ((MinLeg > point.DistanceTo(ObjMot[j].Route[ObjMot[j].Route.Count - 1])) && (point.DistanceTo(ObjMot[j].Route[ObjMot[j].Route.Count - 1]) < 50) && ObjMot[j].SostWork) { float aaaa = point.DistanceTo(ObjMot[j].Route[ObjMot[j].Route.Count - 1]); MinLeg = point.DistanceTo(ObjMot[j].Route[ObjMot[j].Route.Count - 1]); if (MinLeg < 25) { ObjMot[j].Route.Add(point); flag = false; } } } if ((i < detectedObjectsCount) && flag && ((kvadrat(point)))) { Car OM = new Car(); OM.Route.Add(point); ObjMot.Add(OM); continue; } } } else { detectedObjectsCount = -1; } for (int i = 0; i < ObjMot.Count; i++) { if (ObjMot[i].SostTime == false) { ObjMot[i].kolvo++; } } // accumulate history motionHistory.Add(motionLevel); if (motionHistory.Count > 300) { motionHistory.RemoveAt(0); } // int a = image.Width; if (showMotionHistoryToolStripMenuItem.Checked) { DrawMotionHistory(image); } // } // оa++; // } } }
private static Point? FindCamera(Point p, List<Blob> blobs) { var diff = 2; var antidiff = diff*-1; foreach (var blob in blobs) { if (p.DistanceTo(blob.CenterOfGravity) < 1) continue; if (InRange(((int) (blob.CenterOfGravity.Y - p.Y)), antidiff, diff)) { foreach (var blob2 in blobs) { if (blob.CenterOfGravity.DistanceTo(blob2.CenterOfGravity) < 1) continue; if (InRange(((int) (blob2.CenterOfGravity.X - blob.CenterOfGravity.X)), antidiff, diff)) { foreach (var blob3 in blobs) { if (blob2.CenterOfGravity.DistanceTo(blob3.CenterOfGravity) < 1) continue; if (InRange(((int) (blob3.CenterOfGravity.Y - blob2.CenterOfGravity.Y)), antidiff, diff)) { if (InRange(((int) (blob3.CenterOfGravity.X - p.X)), antidiff, diff)) { float xCent = 0, yCent = 0; var sum = p + blob.CenterOfGravity + blob2.CenterOfGravity + blob3.CenterOfGravity; xCent = sum.X/4; yCent = sum.Y/4; return new Point(xCent, yCent); } } } } } } } return null; }