public static Image DrawResultsOnImage(ImageSegmentationResult res, Image im) { if (res == null) { return(null); } foreach (ImageSegmentationResultSingleEntry obj in res.objects) { foreach (GenericPolygon poly in obj.segment.polygons) { string category = obj.Category; List <LineSegment> lines = new List <LineSegment>(); for (int i = 0; i < poly.vertices.Count; i++) { int x1 = poly.vertices[i][0]; int y1 = poly.vertices[i][1]; int j = (i + 1) % poly.vertices.Count; int x2 = poly.vertices[j][0]; int y2 = poly.vertices[j][1]; LineSegment line = new LineSegment(x1, y1, x2, y2); lines.Add(line); } Color c = ColorSet.getColorByObjectType(category); im = DrawingBoxesAndLinesOnImages.addLinesToImage(im, lines, c); } } return(im); }
public static Image DrawImageDetectionResult(MultiObjectLocalizationAndLabelingResult res, string image_url, List <string> Categories) { Image originalImage = ImageUtilities.getImageFromURI(image_url); List <Rectangle> rectangles = new List <Rectangle>(); List <Color> colors = new List <Color>(); List <string> ids = new List <string>(); List <bool> dashed = new List <bool>(); for (int j = 0; j < res.objects.Count; j++) { MultiObjectLocalizationAndLabelingResultSingleEntry box = res.objects[j]; int x = box.boundingBox.tlx; int y = box.boundingBox.tly; int width = box.boundingBox.brx - box.boundingBox.tlx; int height = box.boundingBox.bry - box.boundingBox.tly; Rectangle r = new Rectangle(x, y, width, height); rectangles.Add(r); string category = box.Category; int colorIndex = Categories.IndexOf(category); colors.Add(DrawingBoxesAndLinesOnImages.Colors[colorIndex]); string id = j + "-" + category; ids.Add(id); dashed.Add(false); } Image imageWithBoxes = DrawingBoxesAndLinesOnImages.addRectanglesToImage(originalImage, rectangles, colors, ids, dashed); return(imageWithBoxes); }
public static GenericPolygon GetAggregatedGenericPolygon_Relaxation(List <GenericPolygon> polyList, int ImageWidth, int ImageHeight, double PolygonBoundaryMajorityThreshold) { if (polyList.Count == 0) { return(null); } GenericPolygon startingPoly = polyList[0]; GenericPolygon ret = PolygonDeformationToMajorityEdge(startingPoly, polyList, PolygonBoundaryMajorityThreshold); int count = 0; for (int i = 1; i < polyList.Count; i++) { GenericPolygon adjustedPoly = PolygonDeformationToMajorityEdge(polyList[i], polyList, PolygonBoundaryMajorityThreshold); // insert the cardinal points from the adjusted poly to the existing ret poly foreach (int[] xy in adjustedPoly.vertices) { LineSegment closestEdge = new LineSegment(); int closestEdgeStartingPointIndex = -1; ret.getClosestPointsOnPolygonToAPoint(xy[0], xy[1], out closestEdge, out closestEdgeStartingPointIndex); if (closestEdgeStartingPointIndex != -1) { // don't insert if it is the same cardinal points of the polygon if (xy[0] == ret.vertices[closestEdgeStartingPointIndex][0] && xy[1] == ret.vertices[closestEdgeStartingPointIndex][1]) { continue; } int closestEdgeEndingPointIndex = (closestEdgeStartingPointIndex + 1) % ret.vertices.Count; if (xy[0] == ret.vertices[closestEdgeEndingPointIndex][0] && xy[1] == ret.vertices[closestEdgeEndingPointIndex][1]) { continue; } ret.vertices.Insert(closestEdgeEndingPointIndex, xy); //// debug List <LineSegment> lines = new List <LineSegment>(); int newIndex1 = closestEdgeStartingPointIndex; int newIndex2 = (closestEdgeStartingPointIndex + 1) % ret.vertices.Count; int newIndex3 = (closestEdgeStartingPointIndex + 2) % ret.vertices.Count; for (int k = 0; k < ret.vertices.Count; k++) { if (k == closestEdgeStartingPointIndex) { continue; } if (k == (closestEdgeStartingPointIndex + 1) % ret.vertices.Count) { continue; } //if (k == (closestEdgeStartingPointIndex + 2) % ret.vertices.Count) continue; int x1 = ret.vertices[k][0]; int y1 = ret.vertices[k][1]; int j = (k + 1) % ret.vertices.Count; int x2 = ret.vertices[j][0]; int y2 = ret.vertices[j][1]; LineSegment line = new LineSegment(x1, y1, x2, y2); lines.Add(line); } Color c = ColorSet.getColorByObjectType("cat"); //Image originalImage = ImageUtilities.getImageFromURI("https://satyamresearchjobstorage.blob.core.windows.net/kittisegmentation/test/4-cats-on-tree-fb-cover.jpg"); Image originalImage = ImageUtilities.getImageFromURI("https://satyamresearchjobstorage.blob.core.windows.net/kittisegmentation/testpascal/2007_000121.jpg"); Image im = DrawingBoxesAndLinesOnImages.addLinesToImage(originalImage, lines, c); List <LineSegment> newlines = new List <LineSegment>(); newlines.Add(new LineSegment(ret.vertices[newIndex1][0], ret.vertices[newIndex1][1], ret.vertices[newIndex2][0], ret.vertices[newIndex2][1])); newlines.Add(new LineSegment(ret.vertices[newIndex2][0], ret.vertices[newIndex2][1], ret.vertices[newIndex3][0], ret.vertices[newIndex3][1])); im = DrawingBoxesAndLinesOnImages.addLinesToImage(im, newlines, Color.Red); string filename = count.ToString(); ImageUtilities.saveImage(im, DirectoryConstants.defaultTempDirectory, filename); count++; } else { Console.WriteLine("Error:"); } } } return(ret); }
public static GenericPolygon GetAggregatedGenericPolygon_MajorityEdge( List <GenericPolygon> polyList, int ImageWidth, int ImageHeight, double PolygonBoundaryMajorityThreshold) { if (polyList.Count == 0) { return(null); } int MinMajorityCount = (int)((double)polyList.Count * PolygonBoundaryMajorityThreshold); //List<eventPoint> intersectingPoints = new List<eventPoint>(); //List<eventPoint> polygonPoints = new List<eventPoint>(); List <eventPoint> allEventPoints = new List <eventPoint>(); // add all intersecting points (must not be the existing point on any polygon) of all polygons to eventpoints set for (int i = 0; i < polyList.Count - 1; i++) { for (int j = i + 1; j < polyList.Count; j++) { // go thru every pair of polygons GenericPolygon poly1 = polyList[i]; GenericPolygon poly2 = polyList[j]; for (int k = 0; k < poly1.vertices.Count; k++) { int p = (k + 1) % poly1.vertices.Count; int x1 = poly1.vertices[k][0]; int y1 = poly1.vertices[k][1]; int x2 = poly1.vertices[p][0]; int y2 = poly1.vertices[p][1]; List <int[]> points2Add_poly1 = new List <int[]>(); for (int q = 0; q < poly2.vertices.Count; q++) { // for each line there could be at most one point for poly2 int m = (q + 1) % poly2.vertices.Count; int xx1 = poly2.vertices[q][0]; int yy1 = poly2.vertices[q][1]; int xx2 = poly2.vertices[m][0]; int yy2 = poly2.vertices[m][1]; double [] intersectCoords = new double[] { -1, -1 }; if (LineSegment.Intersect(x1, y1, x2, y2, xx1, yy1, xx2, yy2, out intersectCoords)) { int[] intersect = new int[] { (int)intersectCoords[0], (int)intersectCoords[1] }; if ((intersect[0] != xx1 || intersect[1] != yy1) && (intersect[0] != xx2 || intersect[1] != yy2)) { Console.WriteLine("inserting ({0},{1}) in between ({2},{3})[{7}] and ({4},{5})[{8}], on poly {6}", intersect[0], intersect[1], polyList[j].vertices[q][0], polyList[j].vertices[q][1], polyList[j].vertices[m][0], polyList[j].vertices[m][1], j, q, m); polyList[j].vertices.Insert(m, intersect); q++; } if ((intersect[0] != x1 || intersect[1] != y1) && (intersect[0] != x2 || intersect[1] != y2)) { bool exist = false; foreach (int[] xy in points2Add_poly1) { if (xy[0] == intersect[0] && xy[1] == intersect[1]) { exist = true; break; } } if (!exist) { points2Add_poly1.Add(intersect); } } } } // insert the intersection point into the polygon bool DescendSort = false; if (x1 < x2) { DescendSort = true; } else { if (x1 > x2) { } else { if (y1 < y2) { DescendSort = true; } else { } } } if (!DescendSort) { points2Add_poly1.Sort(delegate(int[] c1, int[] c2) { if (c1[0] != c2[0]) { return(c1[0].CompareTo(c2[0])); } else { return(c1[1].CompareTo(c2[1])); } }); } else { points2Add_poly1.Sort(delegate(int[] c1, int[] c2) { if (c1[0] != c2[0]) { return(c2[0].CompareTo(c1[0])); } else { return(c2[1].CompareTo(c1[1])); } }); } foreach (int[] xy in points2Add_poly1) { Console.WriteLine("inserting ({0},{1}) in between ({2},{3})[{7}] and ({4},{5})[{8}], on poly {6}", xy[0], xy[1], polyList[i].vertices[k][0], polyList[i].vertices[k][1], polyList[i].vertices[p][0], polyList[i].vertices[p][1], i, k, p); polyList[i].vertices.Insert(p, xy); } k += points2Add_poly1.Count; } } } // add all points of all polygons to polygonpoints set for (int i = 0; i < polyList.Count; i++) { for (int j = 0; j < polyList[i].vertices.Count; j++) { int[] xy = polyList[i].vertices[j]; eventPoint ep = new eventPoint(xy); // check if the point exist bool exist = false; foreach (eventPoint eep in allEventPoints) { if (eep.IsSamePoint(ep)) { ep = eep; exist = true; break; } } // for the following, it doesn't matter if duplicate ep.belongingPolygonID.Add(i); int next = (j + 1) % polyList[i].vertices.Count; int prev = (j - 1 + polyList[i].vertices.Count) % polyList[i].vertices.Count; ep.neightbors.Add(polyList[i].vertices[next]); ep.neightbors.Add(polyList[i].vertices[prev]); if (!exist) { //polygonPoints.Add(ep); allEventPoints.Add(ep); } else { Console.WriteLine("adding to existing point ({0},{1}) from poly {2}, index {3}", ep.x, ep.y, i, j); } } } //// add all intersecting points (must not be the existing point on any polygon) of all polygons to eventpoints set //for (int i = 0; i < polyList.Count-1; i++) //{ // for (int j = i+1; j < polyList.Count; j++) // { // // go thru every pair of polygons // GenericPolygon poly1 = polyList[i]; // GenericPolygon poly2 = polyList[j]; // for(int k = 0; k < poly1.vertices.Count; k++) // { // int p = (k + 1) % poly1.vertices.Count; // int x1 = poly1.vertices[k][0]; // int y1 = poly1.vertices[k][1]; // int x2 = poly1.vertices[p][0]; // int y2 = poly1.vertices[p][1]; // List<int[]> intersectingLineSegments = new List<int[]>(); // List<int[]> points = poly2.getAllIntersectionPointsOfLineSegment(x1, y1, x2, y2, out intersectingLineSegments); // // insert the intersection point into the polygon // for (int q=0;q<points.Count;q++) // { // int[] xy = points[q]; // eventPoint ep = new eventPoint(xy); // bool IsPolygonPoint = false; // foreach (eventPoint eep in polygonPoints) // { // if (eep.IsSamePoint(ep)) // { // IsPolygonPoint = true; // break; // } // } // if (IsPolygonPoint) continue; // // check if the point exist already // bool exist = false; // foreach (eventPoint eep in intersectingPoints) // { // if (eep.IsSamePoint(ep)) // { // ep = eep; // exist = true; // break; // } // } // ep.belongingPolygonID.Add(i); // ep.belongingPolygonID.Add(j); // ep.neightbors.Add(new int[] { x1, y1 }); // ep.neightbors.Add(new int[] { x2, y2 }); // int[] neighbor1 = new int[] { intersectingLineSegments[q][0], intersectingLineSegments[q][1] }; // int[] neighbor2 = new int[] { intersectingLineSegments[q][2], intersectingLineSegments[q][3] }; // ep.neightbors.Add(neighbor1); // ep.neightbors.Add(neighbor2); // if (!exist) // { // intersectingPoints.Add(ep); // allEventPoints.Add(ep); // } // eventPoint n1 = new eventPoint(neighbor1); // eventPoint n2 = new eventPoint(neighbor2); // eventPoint n3 = new eventPoint(x1, y1); // eventPoint n4 = new eventPoint(x2, y2); // // add this point to the neighbor list of neighbors as well // // break the intersecting line segment by removing the other end from the neighbor list // foreach (eventPoint eep in allEventPoints) // { // if (eep.IsSamePoint(n1) || eep.IsSamePoint(n2) || eep.IsSamePoint(n3) || eep.IsSamePoint(n4)) // { // eep.neightbors.Add(xy); // } // if (eep.IsSamePoint(n1)) { eep.removeNeighbor(n2.x, n2.y); } // if (eep.IsSamePoint(n2)) { eep.removeNeighbor(n1.x, n1.y); } // if (eep.IsSamePoint(n3)) { eep.removeNeighbor(n4.x, n4.y); } // if (eep.IsSamePoint(n4)) { eep.removeNeighbor(n3.x, n3.y); } // } // } // } // } //} //// sort by y to sweep line top town //eventPoints.Sort(delegate (eventPoint c1, eventPoint c2) { return c1.y.CompareTo(c2.y); }); //// record both a list of left facing edges and list of right facing edges //List<List<int[]>> leftFacingEdges = new List<List<int[]>>(); //List<List<int[]>> rightFacingEdges = new List<List<int[]>>(); //List<int> ThresholdEdgePointsIndex = new List<int>(); List <eventPoint> ThresholdEdgePoints = new List <eventPoint>(); Dictionary <int, bool> pointChecked = new Dictionary <int, bool>(); int count = 0; for (int i = 0; i < allEventPoints.Count; i++) { eventPoint ep = allEventPoints[i]; // calculate how many polygons is this event point in. int InteriorCount = 0; int OnCount = 0; List <int> InteriorPolyIndex = new List <int>(); List <int> OnPolyIndex = new List <int>(); //foreach (GenericPolygon poly in polyList) for (int j = 0; j < polyList.Count; j++) { GenericPolygon poly = polyList[j]; if (poly.PointIsOnPolygon(ep.x, ep.y)) { OnCount++; //InteriorCount++; OnPolyIndex.Add(j); //InteriorPolyIndex.Add(j); } else { if (poly.PointIsInPolygon(ep.x, ep.y)) { InteriorCount++; InteriorPolyIndex.Add(j); } } } //InteriorCount = InteriorCount - OnCount;// it is at least on one polygon, which actully should be counted. if (InteriorCount >= MinMajorityCount) { continue; } if (InteriorCount + OnCount < MinMajorityCount) { continue; } // an event point that is right on the boarder of majority threshold ThresholdEdgePoints.Add(ep); pointChecked.Add(count, false); count++; } // delete all neighbors thats not in majority edge foreach (eventPoint ep in ThresholdEdgePoints) { List <int[]> ValidNeighbor = new List <int[]>(); for (int i = 0; i < ep.neightbors.Count; i++) { eventPoint tp = new eventPoint(ep.neightbors[i]); foreach (eventPoint eep in ThresholdEdgePoints) { if (eep.IsSamePoint(tp)) { ValidNeighbor.Add(ep.neightbors[i]); } } } ep.neightbors.Clear(); ep.neightbors = ValidNeighbor; } string debugstr = JSonUtils.ConvertObjectToJSon(ThresholdEdgePoints); string polylistStr = JSonUtils.ConvertObjectToJSon(polyList); string allpointsstr = JSonUtils.ConvertObjectToJSon(allEventPoints); List <List <int[]> > outputPolygon = new List <List <int[]> >(); for (int p = 0; p < ThresholdEdgePoints.Count; p++) { if (pointChecked[p]) { continue; } List <int[]> polygon1 = new List <int[]>(); List <int[]> polygon2 = new List <int[]>(); polygon1.Add(new int[] { ThresholdEdgePoints[p].x, ThresholdEdgePoints[p].y }); polygon1 = addNeighbors(ThresholdEdgePoints[p], ThresholdEdgePoints, polygon1, pointChecked); if (!pointChecked[p]) { polygon2.Add(new int[] { ThresholdEdgePoints[p].x, ThresholdEdgePoints[p].y }); polygon2 = addNeighbors(ThresholdEdgePoints[p], ThresholdEdgePoints, polygon2, pointChecked); for (int i = polygon2.Count - 1; i >= 0; i--) { polygon1.Add(polygon2[i]); } } //loop closed outputPolygon.Add(polygon1); //debug string poly = ""; foreach (int [] xy in polygon1) { poly += "[" + xy[0].ToString() + "," + xy[1].ToString() + "] "; } Console.WriteLine(poly); } //// debug List <LineSegment> lines = new List <LineSegment>(); for (int k = 0; k < ThresholdEdgePoints.Count; k++) { int x1 = ThresholdEdgePoints[k].x; int y1 = ThresholdEdgePoints[k].y; foreach (int[] xy in ThresholdEdgePoints[k].neightbors) { int x2 = xy[0]; int y2 = xy[1]; LineSegment line = new LineSegment(x1, y1, x2, y2); lines.Add(line); } } Color c = ColorSet.getColorByObjectType("cat"); //Image originalImage = ImageUtilities.getImageFromURI("https://satyamresearchjobstorage.blob.core.windows.net/kittisegmentation/test/4-cats-on-tree-fb-cover.jpg"); Image originalImage = ImageUtilities.getImageFromURI("https://satyamresearchjobstorage.blob.core.windows.net/kittisegmentation/testpascal/2007_000121.jpg"); Image im = DrawingBoxesAndLinesOnImages.addLinesToImage(originalImage, lines, c); //string filename = count.ToString(); string filename = "majorityEdge"; ImageUtilities.saveImage(im, DirectoryConstants.defaultTempDirectory, filename); // TB changed to Segments return(new GenericPolygon(outputPolygon[0])); }
public static Image DrawResultStringOnImage(string resultString, Image originalImage) { CameraPoseAnnotationResult res = JSonUtils.ConvertJSonToObject <CameraPoseAnnotationResult>(resultString); List <LineSegment> xparallel = new List <LineSegment>(); List <LineSegment> yparallel = new List <LineSegment>(); List <LineSegment> zparallel = new List <LineSegment>(); int[] origin = new int[2]; int count = 0; foreach (int[] vertex in res.objects.caxis.vertices) { if (count == 0) { origin[0] = vertex[0]; origin[1] = vertex[1]; } LineSegment line = new LineSegment(origin[0], origin[1], vertex[0], vertex[1]); if (count == 1) { xparallel.Add(line); } if (count == 2) { yparallel.Add(line); } if (count == 3) { zparallel.Add(line); } count = count + 1; } count = 0; foreach (int[] vertex in res.objects.xvppoints.vertices) { if (count % 2 == 0) { origin[0] = vertex[0]; origin[1] = vertex[1]; } else { LineSegment line = new LineSegment(origin[0], origin[1], vertex[0], vertex[1]); xparallel.Add(line); } count = count + 1; } count = 0; foreach (int[] vertex in res.objects.yvppoints.vertices) { if (count % 2 == 0) { origin[0] = vertex[0]; origin[1] = vertex[1]; } else { LineSegment line = new LineSegment(origin[0], origin[1], vertex[0], vertex[1]); yparallel.Add(line); } count = count + 1; } count = 0; foreach (int[] vertex in res.objects.zvppoints.vertices) { if (count % 2 == 0) { origin[0] = vertex[0]; origin[1] = vertex[1]; } else { LineSegment line = new LineSegment(origin[0], origin[1], vertex[0], vertex[1]); zparallel.Add(line); } count = count + 1; } Image imageWithX = DrawingBoxesAndLinesOnImages.addLinesToImage(originalImage, xparallel, Color.Red, true); Image imageWithY = DrawingBoxesAndLinesOnImages.addLinesToImage(imageWithX, yparallel, Color.Blue, true); Image imageWithZ = DrawingBoxesAndLinesOnImages.addLinesToImage(imageWithY, zparallel, Color.Yellow, true); return(imageWithZ); }
public static void SaveResultImagesLocally(List <SatyamResultsTableEntry> entries, string directoryName) { if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } directoryName = directoryName + "\\Raw"; if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(directoryName); } for (int i = 0; i < entries.Count; i++) { SatyamResultsTableEntry entry = entries[i]; SatyamResult satyamResult = JSonUtils.ConvertJSonToObject <SatyamResult>(entry.ResultString); SatyamTask task = JSonUtils.ConvertJSonToObject <SatyamTask>(satyamResult.TaskParametersString); SatyamJob job = task.jobEntry; MultiObjectLocalizationAndLabelingResult res = JSonUtils.ConvertJSonToObject <MultiObjectLocalizationAndLabelingResult>(satyamResult.TaskResult); string ofilename = URIUtilities.filenameFromURI(task.SatyamURI); string[] fields = ofilename.Split('.'); string fileName = fields[0]; Image originalImage = ImageUtilities.getImageFromURI(task.SatyamURI); MultiObjectLocalizationAndLabelingSubmittedJob jobDefinition = JSonUtils.ConvertJSonToObject <MultiObjectLocalizationAndLabelingSubmittedJob>(job.JobParameters); Image imageWithBoundary = DrawingBoxesAndLinesOnImages.addLinesToImage(originalImage, jobDefinition.BoundaryLines, Color.Red, true); List <Rectangle> rectangles = new List <Rectangle>(); List <Color> colors = new List <Color>(); List <string> ids = new List <string>(); List <bool> dashed = new List <bool>(); for (int j = 0; j < res.objects.Count; j++) { MultiObjectLocalizationAndLabelingResultSingleEntry box = res.objects[j]; int x = box.boundingBox.tlx; int y = box.boundingBox.tly; int width = box.boundingBox.brx - box.boundingBox.tlx; int height = box.boundingBox.bry - box.boundingBox.tly; Rectangle r = new Rectangle(x, y, width, height); rectangles.Add(r); string category = box.Category; int colorIndex = jobDefinition.Categories.IndexOf(category); colors.Add(DrawingBoxesAndLinesOnImages.Colors[colorIndex]); string id = j + "-" + category; ids.Add(id); dashed.Add(false); } Image imageWithBoxesAndBoundary = DrawingBoxesAndLinesOnImages.addRectanglesToImage(imageWithBoundary, rectangles, colors, ids, dashed); fileName = fileName + "-Result"; if (satyamResult.amazonInfo.AssignmentID != "") { //fileName = fileName + "-" + satyamResult.amazonInfo.AssignmentID + "_" + entry.ID; fileName = fileName + "-" + entry.ID + "_" + satyamResult.PrevResultID; } ImageUtilities.saveImage(imageWithBoxesAndBoundary, directoryName, fileName); } }