private void RearrangeCorners(List <IntPoint> corners) { float[] pointDistances = new float[4]; for (int x = 0; x < corners.Count; x++) { IntPoint point = corners[x]; pointDistances[x] = point.DistanceTo((x == (corners.Count - 1) ? corners[0] : corners[x + 1])); } float shortestDist = float.MaxValue; Int32 shortestSide = Int32.MaxValue; for (int x = 0; x < corners.Count; x++) { if (pointDistances[x] < shortestDist) { shortestSide = x; shortestDist = pointDistances[x]; } } if (shortestSide != 0 && shortestSide != 2) { IntPoint endPoint = corners[0]; corners.RemoveAt(0); corners.Add(endPoint); } }
private bool FindNewCornersAndCheckAspectRatio(List <IntPoint> corners) { Point g = PointsCloud.GetCenterOfGravity(corners); IntPoint p1 = corners[0], p2 = corners[1], p3 = corners[2], p4 = corners[3]; List <IntPoint> leftPoints = new List <IntPoint>(); List <IntPoint> rightPoints = new List <IntPoint>(); foreach (IntPoint p in corners) { if (p.X < g.X) { leftPoints.Add(p); } else { rightPoints.Add(p); } } if (leftPoints.Count - rightPoints.Count != 0) { return(false); } // korekcija if (leftPoints[0].Y < leftPoints[1].Y) { p4 = leftPoints[0]; p1 = leftPoints[1]; } else { p4 = leftPoints[1]; p1 = leftPoints[0]; } if (rightPoints[0].Y < rightPoints[1].Y) { p3 = rightPoints[0]; p2 = rightPoints[1]; } else { p3 = rightPoints[1]; p2 = rightPoints[0]; } corners[0] = p4; corners[1] = p3; corners[2] = p2; corners[3] = p1; float topSideWidth = p1.DistanceTo(p2); float rightSideHeight = p3.DistanceTo(p2); float ar = topSideWidth / rightSideHeight; return(2 <= ar && ar <= 10); }
public bool IsBlobOnMe(IntPoint posOfBlob) { bool bRet = false; if (posOfBlob.DistanceTo(this.panelCenter) <= 40) { //MessageBox.Show("IsBlobOnMe, posOfBlob.DistanceTo: " + (posOfBlob.DistanceTo(this.panelCenter)).ToString()); bRet = true; } return(bRet); }
/* Fonction Robot */ private void mergePosition(List <PositionRobot> LstTmp) { if (LstTmp.Count == 0) { return; } List <PositionRobot> ListEnvoi = new List <PositionRobot>(); for (int i = 0; i < LstTmp.Count; i++) { bool Trouve = false; PositionRobot tmp = LstTmp[i]; tmp.Position.X = (int)(ratioCmParPixel[0] * tmp.Position.X); tmp.Position.Y = (int)(ratioCmParPixel[1] * tmp.Position.Y); for (int j = 0; j < LstRobot.Count; j++) { if (LstRobot[j].Identifiant == LstTmp[i].Identifiant) { IntPoint itmp = new IntPoint((int)(tmp.Position.X), (int)(tmp.Position.Y)); IntPoint p = new IntPoint(LstRobot[j].Position.X, LstRobot[j].Position.Y); if (p.DistanceTo(itmp) > 5) // Sueil a 2 cm { LstRobot[j] = tmp; ListEnvoi.Add(tmp); } tmp.DerniereModification = DateTime.Now; Trouve = true; break; } } if (Trouve == false) { tmp.DerniereModification = DateTime.Now; ListEnvoi.Add(tmp); LstRobot.Add(tmp); } } if (ListEnvoi.Count > 0) { //Logger.GlobalLogger.debug("" + ListEnvoi.Count); envoieListe(ListEnvoi); } for (int i = LstRobot.Count - 1; i >= 0; i--) { if (LstRobot[i].Position.X == -1) { LstRobot.RemoveAt(i); } } }
/// <summary> /// Find corners of quadrilateral or triangular area, which contains the specified collection of points. /// </summary> /// /// <param name="cloud">Collection of points to search quadrilateral for.</param> /// /// <returns>Returns a list of 3 or 4 points, which are corners of the quadrilateral or /// triangular area filled by specified collection of point. The first point in the list /// is the point with lowest X coordinate (and with lowest Y if there are several points /// with the same X value). The corners are provided in counter clockwise order /// (<a href="http://en.wikipedia.org/wiki/Cartesian_coordinate_system">Cartesian /// coordinate system</a>).</returns> /// /// <remarks><para>The method makes an assumption that the specified collection of points /// form some sort of quadrilateral/triangular area. With this assumption it tries to find corners /// of the area.</para> /// /// <para><note>The method does not search for <b>bounding</b> quadrilateral/triangular area, /// where all specified points are <b>inside</b> of the found quadrilateral/triangle. Some of the /// specified points potentially may be outside of the found quadrilateral/triangle, since the /// method takes corners only from the specified collection of points, but does not calculate such /// to form true bounding quadrilateral/triangle.</note></para> /// /// <para>See <see cref="QuadrilateralRelativeDistortionLimit"/> property for additional information.</para> /// </remarks> /// public static List <IntPoint> FindQuadrilateralCorners(IEnumerable <IntPoint> cloud) { // quadrilateral's corners List <IntPoint> corners = new List <IntPoint>( ); // get bounding rectangle of the points list IntPoint minXY, maxXY; PointsCloud.GetBoundingRectangle(cloud, out minXY, out maxXY); // get cloud's size IntPoint cloudSize = maxXY - minXY; // calculate center point IntPoint center = minXY + cloudSize / 2; // acceptable deviation limit float distortionLimit = quadrilateralRelativeDistortionLimit * (cloudSize.X + cloudSize.Y) / 2; // get the furthest point from (0,0) IntPoint point1 = PointsCloud.GetFurthestPoint(cloud, center); // get the furthest point from the first point IntPoint point2 = PointsCloud.GetFurthestPoint(cloud, point1); corners.Add(point1); corners.Add(point2); // get two furthest points from line IntPoint point3, point4; float distance3, distance4; PointsCloud.GetFurthestPointsFromLine(cloud, point1, point2, out point3, out distance3, out point4, out distance4); // ideally points 1 and 2 form a diagonal of the // quadrilateral area, and points 3 and 4 form another diagonal // but if one of the points (3 or 4) is very close to the line // connecting points 1 and 2, then it is one the same line ... // which means corner was not found. // in this case we deal with a trapezoid or triangle, where // (1-2) line is one of it sides. // another interesting case is when both points (3) and (4) are // very close the (1-2) line. in this case we may have just a flat // quadrilateral. if ( ((distance3 >= distortionLimit) && (distance4 >= distortionLimit)) || ((distance3 < distortionLimit) && (distance3 != 0) && (distance4 < distortionLimit) && (distance4 != 0))) { // don't add one of the corners, if the point is already in the corners list // (this may happen when both #3 and #4 points are very close to the line // connecting #1 and #2) if (!corners.Contains(point3)) { corners.Add(point3); } if (!corners.Contains(point4)) { corners.Add(point4); } } else { // it seems that we deal with kind of trapezoid, // where point 1 and 2 are on the same edge IntPoint tempPoint = (distance3 > distance4) ? point3 : point4; // try to find 3rd point PointsCloud.GetFurthestPointsFromLine(cloud, point1, tempPoint, out point3, out distance3, out point4, out distance4); bool thirdPointIsFound = false; if ((distance3 >= distortionLimit) && (distance4 >= distortionLimit)) { if (point4.DistanceTo(point2) > point3.DistanceTo(point2)) { point3 = point4; } thirdPointIsFound = true; } else { PointsCloud.GetFurthestPointsFromLine(cloud, point2, tempPoint, out point3, out distance3, out point4, out distance4); if ((distance3 >= distortionLimit) && (distance4 >= distortionLimit)) { if (point4.DistanceTo(point1) > point3.DistanceTo(point1)) { point3 = point4; } thirdPointIsFound = true; } } if (!thirdPointIsFound) { // failed to find 3rd edge point, which is away enough from the temp point. // this means that the clound looks more like triangle corners.Add(tempPoint); } else { corners.Add(point3); // try to find 4th point float tempDistance; PointsCloud.GetFurthestPointsFromLine(cloud, point1, point3, out tempPoint, out tempDistance, out point4, out distance4); if ((distance4 >= distortionLimit) && (tempDistance >= distortionLimit)) { if (tempPoint.DistanceTo(point2) > point4.DistanceTo(point2)) { point4 = tempPoint; } } else { PointsCloud.GetFurthestPointsFromLine(cloud, point2, point3, out tempPoint, out tempDistance, out point4, out distance4); if ((tempPoint.DistanceTo(point1) > point4.DistanceTo(point1)) && (tempPoint != point2) && (tempPoint != point3)) { point4 = tempPoint; } } if ((point4 != point1) && (point4 != point2) && (point4 != point3)) { corners.Add(point4); } } } // put the point with lowest X as the first for (int i = 1, n = corners.Count; i < n; i++) { if ((corners[i].X < corners[0].X) || ((corners[i].X == corners[0].X) && (corners[i].Y < corners[0].Y))) { IntPoint temp = corners[i]; corners[i] = corners[0]; corners[0] = temp; } } // sort other points in counter clockwise order float k1 = (corners[1].X != corners[0].X) ? ((float)(corners[1].Y - corners[0].Y) / (corners[1].X - corners[0].X)) : ((corners[1].Y > corners[0].Y) ? float.PositiveInfinity : float.NegativeInfinity); float k2 = (corners[2].X != corners[0].X) ? ((float)(corners[2].Y - corners[0].Y) / (corners[2].X - corners[0].X)) : ((corners[2].Y > corners[0].Y) ? float.PositiveInfinity : float.NegativeInfinity); if (k2 < k1) { IntPoint temp = corners[1]; corners[1] = corners[2]; corners[2] = temp; float tk = k1; k1 = k2; k2 = tk; } if (corners.Count == 4) { float k3 = (corners[3].X != corners[0].X) ? ((float)(corners[3].Y - corners[0].Y) / (corners[3].X - corners[0].X)) : ((corners[3].Y > corners[0].Y) ? float.PositiveInfinity : float.NegativeInfinity); if (k3 < k1) { IntPoint temp = corners[1]; corners[1] = corners[3]; corners[3] = temp; float tk = k1; k1 = k3; k3 = tk; } if (k3 < k2) { IntPoint temp = corners[2]; corners[2] = corners[3]; corners[3] = temp; float tk = k2; k2 = k3; k3 = tk; } } return(corners); }
/* fonction cubes */ private void mergePosition(List <Cub> lst) { List <bool> tab = new List <bool>(); for (int i = 0; i < LstCube.Count; i++) { tab.Add(false); } // Parcours de la liste des cubes recu par l'image for (int i = 0; i < lst.Count; i++) { bool trouv = false; // Si il y a déjà des cubes dans la liste if (LstCube.Count > 0) { for (int j = 0; j < LstCube.Count; j++) { // Verification de la couleur if (LstCube[j].Color == lst[i].Color) { IntPoint milieu = new IntPoint(lst[i].rec.X + lst[i].rec.Width / 2, lst[i].rec.Y + lst[i].rec.Height / 2); //milieu.X = (int)(milieu.X * ratioCmParPixel[0]); //milieu.Y = (int)(milieu.Y * ratioCmParPixel[1]); float distance = milieu.DistanceTo(new IntPoint(LstCube[j].contour.X + LstCube[j].contour.Width / 2, LstCube[j].contour.Y + LstCube[j].contour.Height / 2)); if (distance < (LstCube[j].contour.Width + LstCube[j].contour.Height) * 1.1) // Verification de la proximite { if (tab[j] == false) { LstCube[j].Update(lst[i].rec); tab[j] = true; } trouv = true; break; } } } } if (trouv == false) // Aucun cube correspondant => AJOUT DU CUBE { LstCube.Add(new ObjColor(lst[i].rec, lst[i].Color, lastIdCube++)); tab.Add(true); } } // Preparation de la liste pour envoie if (LstCube.Count > 0) { List <int> delete = new List <int>(); List <PositionCube> lstpos = new List <PositionCube>(); for (int i = 0; i < LstCube.Count; i++) { PositionCube po = new PositionCube(); po.ID = LstCube[i].Identifiant; po.IDZone = LstCube[i].Color; po.Position = new PositionElement(); // Position en pixel IntPoint milieu = new IntPoint(LstCube[i].contour.X + LstCube[i].contour.Width / 2, LstCube[i].contour.Y + LstCube[i].contour.Height / 2); TimeSpan t = DateTime.Now - LstCube[i].DerniereVisualisation; if (t.Seconds > 3) { po.Position.X = -1; po.Position.Y = -1; delete.Add(i); } else { po.Position.X = (int)(milieu.X * ratioCmParPixel[0]); po.Position.Y = (int)(milieu.Y * ratioCmParPixel[1]); } lstpos.Add(po); } envoieListe(lstpos); if (delete.Count > 0) { for (int i = delete.Count - 1; i >= 0; i--) { LstCube.RemoveAt(delete[i]); } Logger.GlobalLogger.debug("Suppression de " + delete.Count + " Cubes"); } } }
public static List <IntPoint> FindQuadrilateralCorners(IEnumerable <IntPoint> cloud) { List <IntPoint> list = new List <IntPoint>(); GetBoundingRectangle(cloud, out IntPoint minXY, out IntPoint maxXY); IntPoint point = maxXY - minXY; IntPoint referencePoint = minXY + point / 2; float num = quadrilateralRelativeDistortionLimit * (float)(point.X + point.Y) / 2f; IntPoint furthestPoint = GetFurthestPoint(cloud, referencePoint); IntPoint furthestPoint2 = GetFurthestPoint(cloud, furthestPoint); list.Add(furthestPoint); list.Add(furthestPoint2); GetFurthestPointsFromLine(cloud, furthestPoint, furthestPoint2, out IntPoint furthestPoint3, out float distance, out IntPoint furthestPoint4, out float distance2); if ((distance >= num && distance2 >= num) || (distance < num && distance != 0f && distance2 < num && distance2 != 0f)) { if (!list.Contains(furthestPoint3)) { list.Add(furthestPoint3); } if (!list.Contains(furthestPoint4)) { list.Add(furthestPoint4); } } else { IntPoint furthestPoint5 = (distance > distance2) ? furthestPoint3 : furthestPoint4; GetFurthestPointsFromLine(cloud, furthestPoint, furthestPoint5, out furthestPoint3, out distance, out furthestPoint4, out distance2); bool flag = false; if (distance >= num && distance2 >= num) { if (furthestPoint4.DistanceTo(furthestPoint2) > furthestPoint3.DistanceTo(furthestPoint2)) { furthestPoint3 = furthestPoint4; } flag = true; } else { GetFurthestPointsFromLine(cloud, furthestPoint2, furthestPoint5, out furthestPoint3, out distance, out furthestPoint4, out distance2); if (distance >= num && distance2 >= num) { if (furthestPoint4.DistanceTo(furthestPoint) > furthestPoint3.DistanceTo(furthestPoint)) { furthestPoint3 = furthestPoint4; } flag = true; } } if (!flag) { list.Add(furthestPoint5); } else { list.Add(furthestPoint3); GetFurthestPointsFromLine(cloud, furthestPoint, furthestPoint3, out furthestPoint5, out float distance3, out furthestPoint4, out distance2); if (distance2 >= num && distance3 >= num) { if (furthestPoint5.DistanceTo(furthestPoint2) > furthestPoint4.DistanceTo(furthestPoint2)) { furthestPoint4 = furthestPoint5; } } else { GetFurthestPointsFromLine(cloud, furthestPoint2, furthestPoint3, out furthestPoint5, out distance3, out furthestPoint4, out distance2); if (furthestPoint5.DistanceTo(furthestPoint) > furthestPoint4.DistanceTo(furthestPoint) && furthestPoint5 != furthestPoint2 && furthestPoint5 != furthestPoint3) { furthestPoint4 = furthestPoint5; } } if (furthestPoint4 != furthestPoint && furthestPoint4 != furthestPoint2 && furthestPoint4 != furthestPoint3) { list.Add(furthestPoint4); } } } int i = 1; for (int count = list.Count; i < count; i++) { if (list[i].X < list[0].X || (list[i].X == list[0].X && list[i].Y < list[0].Y)) { IntPoint value = list[i]; list[i] = list[0]; list[0] = value; } } float num2 = (list[1].X != list[0].X) ? ((float)(list[1].Y - list[0].Y) / (float)(list[1].X - list[0].X)) : ((list[1].Y > list[0].Y) ? float.PositiveInfinity : float.NegativeInfinity); float num3 = (list[2].X != list[0].X) ? ((float)(list[2].Y - list[0].Y) / (float)(list[2].X - list[0].X)) : ((list[2].Y > list[0].Y) ? float.PositiveInfinity : float.NegativeInfinity); if (num3 < num2) { IntPoint value2 = list[1]; list[1] = list[2]; list[2] = value2; float num4 = num2; num2 = num3; num3 = num4; } if (list.Count == 4) { float num5 = (list[3].X != list[0].X) ? ((float)(list[3].Y - list[0].Y) / (float)(list[3].X - list[0].X)) : ((list[3].Y > list[0].Y) ? float.PositiveInfinity : float.NegativeInfinity); if (num5 < num2) { IntPoint value3 = list[1]; list[1] = list[3]; list[3] = value3; float num6 = num2; num2 = num5; num5 = num6; } if (num5 < num3) { IntPoint value4 = list[2]; list[2] = list[3]; list[3] = value4; float num7 = num3; num3 = num5; num5 = num7; } } return(list); }