private MCvBox2D GetMinAreaRect(Contour <Point> contour) { Point[] points = contour.ToArray(); PointF[] pointsF = new PointF[points.Length]; for (int i = 0; i < points.Length; i++) { pointsF[i] = new PointF( points[i].X, points[i].Y); } return(PointCollection.MinAreaRect(pointsF)); }
public static MCvBox2D SquareFittingWithAngle(PointF[] points) { MCvBox2D box = PointCollection.MinAreaRect(points); return(box); }
public static Rectangle SquareFitting(PointF[] points) { MCvBox2D box = PointCollection.MinAreaRect(points); return(box.MinAreaRect()); }
private void ExtractDoor(NiFile model, Matrix4 worldMatrix, int fixtureid) { // take everything that is under a doorX node and use it for the hull... if (model == null) { return; } var doorVertices = new Dictionary <string, List <Vector3> >(); // find all trimeshs and tristrips foreach (var obj in model.ObjectsByRef.Values) { var avNode = obj as NiAVObject; if (avNode == null) { continue; } if (!IsMatched(avNode, new[] { "visible", "collidee", "collide" })) { continue; } var doorName = FindMatchRegex(avNode, new[] { DoorRegex }); if (doorName == string.Empty) { continue; } Vector3[] vertices = null; Triangle[] triangles = null; TryExtractTriShape(obj, worldMatrix, false, false, ref vertices, ref triangles); if (vertices == null) { TryExtractTriStrips(obj, worldMatrix, false, false, ref vertices, ref triangles); } if (vertices == null) { continue; } if (!doorVertices.ContainsKey(doorName)) { doorVertices.Add(doorName, new List <Vector3>()); } doorVertices[doorName].AddRange(vertices); } foreach (var key in doorVertices.Keys) { float hullMinZ = float.MaxValue; float hullMaxZ = float.MinValue; var verts = doorVertices[key].ToArray(); var pts = new List <PointF>(); foreach (Vector3 vert in verts) { hullMinZ = Math.Min(vert.Z, hullMinZ); hullMaxZ = Math.Max(vert.Z, hullMaxZ); pts.Add(new PointF(vert.X, vert.Y)); } MCvBox2D box = PointCollection.MinAreaRect(pts.ToArray()); float maxSize = box.size.Width; maxSize = Math.Max(maxSize, box.size.Height); maxSize = Math.Max(maxSize, hullMaxZ - hullMinZ); // There are some weird door ids in e.g. Jordheim (z120): "door01:0" e.g. -- how do they translate to IDs? var doorID = Regex.Match(key.Replace(":", ""), "([0-9]+)").Groups[1].Value; var heading = ((box.angle + (box.size.Width < box.size.Height ? 0.0 : 90.0 + 90.0)) * DEGREES_TO_HEADING) % 0x1000; if (heading < 0) { heading += 0x1000; } DoorWriter.WriteDoor(Zone.ID * 1000000 + fixtureid * 100 + int.Parse(doorID), model.FileName, (int)box.center.X, (int)box.center.Y, (int)hullMinZ, (int)heading, (int)maxSize); // Make sure we have a min of 20f for doors on width/height box.size.Width = Math.Max(20f, box.size.Width); box.size.Height = Math.Max(20f, box.size.Height); // Make sure the door touches the ground... hullMinZ -= 16.0f; var boxVertices = box.GetVertices(); GeomSetWriter.WriteConvexVolume(boxVertices.Length, hullMinZ, hullMaxZ, CEM.GeomSetWriter.eAreas.Door); foreach (var vert in boxVertices) { GeomSetWriter.WriteConvexVolumeVertex(new Vector3(vert.X, vert.Y, hullMinZ)); // debug } } }
//----------------------------------------------------------------------------------------- private void Draw_Gesture_Features() { #region drawing PalmBoundingCircle try { // find bounding rec for PalmPointsCollection PalmBoundingCircle = PointCollection.MinEnclosingCircle(PalmPointsCollection); } catch { return; } // we treat center of the circle as the center of the palm imgMain.Draw(PalmBoundingCircle, new Bgr(Color.Violet), 2); imgMain.Draw(new CircleF(new PointF(PalmBoundingCircle.Center.X, PalmBoundingCircle.Center.Y), 2), new Bgr(Color.Violet), 5); #endregion #region drawing FingerBoundingBox // find bounding rec for FingerPointsCollection MCvBox2D box = PointCollection.MinAreaRect(FingerPointsCollection); FingerBoundingBox = box.MinAreaRect(); // we treat center of the circle as the center of the palm imgMain.Draw(FingerBoundingBox, new Bgr(Color.Cyan), 2); imgMain.Draw(new CircleF(new PointF(box.center.X, box.center.Y), 2), new Bgr(Color.Cyan), 5); #endregion #region drawing all finger points for (int i = 0; i < FingerPointsCollection.Count(); i++) { imgMain.Draw(new CircleF(FingerPointsCollection[i], 2), new Bgr(Color.Yellow), 5); } #endregion #region drawing all palm points for (int i = 0; i < PalmPointsCollection.Count(); i++) { imgMain.Draw(new CircleF(PalmPointsCollection[i], 2), new Bgr(Color.Red), 5); } #endregion //#region drawing lines from fingers to center //for (int i = 0; i < FingerPointsCollection.Count(); i++) //{ // imgMain.Draw(new LineSegment2DF(FingerPointsCollection[i], PalmBoundingCircle.Center), new Bgr(Color.Yellow), 1); //} //#endregion //#region drawing lines from palm to center //for (int i = 0; i < PalmPointsCollection.Count(); i++) //{ // imgMain.Draw(new LineSegment2DF(PalmPointsCollection[i], PalmBoundingCircle.Center), new Bgr(Color.Red), 1); //} //#endregion //#region Filtered drawing lines from fingers to center //for (int i = 0; i < FingerPointsCollection.Count(); i++) //{ // if (FingerPointsCollection[i].Y < PalmBoundingCircle.Center.Y) // { // imgMain.Draw(new LineSegment2DF(FingerPointsCollection[i], PalmBoundingCircle.Center), new Bgr(Color.Yellow), 1); // } //} //#endregion //#region Filtered drawing lines from palm to center //for (int i = 0; i < PalmPointsCollection.Count(); i++) //{ // if (PalmPointsCollection[i].Y < PalmBoundingCircle.Center.Y) // { // imgMain.Draw(new LineSegment2DF(PalmPointsCollection[i], PalmBoundingCircle.Center), new Bgr(Color.Red), 1); // } //} //#endregion }
public void Rotation() { //Bitmap src = new Bitmap(@"C:\Users\nhonarva\Documents\strabo-command-line-master\strabo-command-line-master\Strabo.CommandLine\data\intermediate\136_p_4_559_1292_s_0_525_1280_72_24.png"); //Rectangle rect = new Rectangle(0, 0, 100, 50); //Bitmap cropped = CropRotatedRect(src, rect, -30.5f, true); //cropped.Save(@"C:\Users\nhonarva\Documents\sample.png"); List <PointF> points = new List <PointF>(); PointF[] pts; //PointF[] pts=new PointF[4]; //pts[0] = new PointF(40.5f, 60.5f); //pts[1] = new PointF(34f, 44f); //pts[2] = new PointF(39f, 70f); //pts[3] = new PointF(14f, 45f); Bitmap sourceImage = new Bitmap(@"C:\Users\ronaldiscool\Documents\GitHub\strabo-command-line\Strabo.CommandLine\data\intermediate\138_p_6_1482_1009_s_0_1469_965_28_84.png"); /// this image belongs to map 1920-4 intermediate result Image <Bgr, byte> source = new Image <Bgr, byte>(@"C:\Users\ronaldiscool\Documents\GitHub\strabo-command-line\Strabo.CommandLine\data\intermediate\138_p_6_1482_1009_s_0_1469_965_28_84.png"); Image <Bgr, byte> source1 = new Image <Bgr, byte>(@"C:\Users\ronaldiscool\Documents\GitHub\strabo-command-line\Strabo.CommandLine\data\intermediate\SourceMapImage_ms.png"); Bitmap sourceImage1 = new Bitmap(@"C:\Users\ronaldiscool\Documents\GitHub\strabo-command-line\Strabo.CommandLine\data\intermediate\BinaryOutput.png"); Image <Bgr, byte> img = new Image <Bgr, byte>(400, 400, new Bgr(Color.White)); for (int i = 0; i < sourceImage.Width; i++) { for (int j = 0; j < sourceImage.Height; j++) { Color color = sourceImage.GetPixel(i, j); if (!sourceImage.GetPixel(i, j).Name.Equals("ffffffff")) { PointF point = new PointF(i, j); points.Add(point); } } } string filename = Path.GetFileNameWithoutExtension((@"C:\Users\ronaldiscool\Documents\GitHub\strabo-command-line\Strabo.CommandLine\data\intermediate\138_p_6_1482_1009_s_0_1469_965_28_84.png")); // string filename = Path.GetFileNameWithoutExtension(filePaths[i]); String[] splitTokens = filename.Split('_'); int numberOfLetters = 5; //if (splitTokens[6].Equals("0")) //{ int x = Convert.ToInt32(splitTokens[7]); int y = Convert.ToInt32(splitTokens[8]); // int width=Convert.ToInt32(splitTokens[9]); // int height=Convert.ToInt32(splitTokens[10]); // int estimatedSizeOfOneLetter = width/numberOfLetters; // double pixelToSizeRatio = (double)points.Count / (double)(width * height); // Point left = new Point(); // int leftPixelCount = 0; // double leftPixelToSizeRatio = 0; // left.X = x - estimatedSizeOfOneLetter; // left.Y = y; // Point right = new Point(); // int rightPixelCount = 0; // double rightPixelToSizeRatio = 0; // right.X = x + width; // right.Y = y; // do // { // for (int i = left.X; i < left.X+estimatedSizeOfOneLetter; i++) // { // for (int j = left.Y; j < left.Y+height; j++) // { // Color color = sourceImage1.GetPixel(i, j); // if (!sourceImage1.GetPixel(i, j).Name.Equals("ffffffff")) // { // leftPixelCount++; // // sourceImage1.SetPixel(i, j, Color.Green); // } // sourceImage1.SetPixel(i, j, Color.Green); // } // } // leftPixelToSizeRatio = (double)leftPixelCount / (double)(estimatedSizeOfOneLetter * height); // left.X -= estimatedSizeOfOneLetter; // leftPixelCount = 0; // } while (leftPixelToSizeRatio > 0.1); // do // { // for (int i = right.X; i < right.X + estimatedSizeOfOneLetter; i++) // { // for (int j = right.Y; j < right.Y + height; j++) // { // Color color = sourceImage1.GetPixel(i, j); // if (!sourceImage1.GetPixel(i, j).Name.Equals("ffffffff")) // { // rightPixelCount++; // // sourceImage1.SetPixel(i, j, Color.Green); // } // sourceImage1.SetPixel(i, j, Color.Green); // } // } // rightPixelToSizeRatio = (double)(rightPixelCount) / (double)(estimatedSizeOfOneLetter * height); // right.X += estimatedSizeOfOneLetter; // rightPixelCount = 0; // } while (rightPixelToSizeRatio > 0.2); //} // else // { pts = points.ToArray(); Emgu.CV.Structure.MCvBox2D box = PointCollection.MinAreaRect(pts); Rectangle boundingbox = PointCollection.BoundingRectangle(pts); img.Draw(box, new Bgr(Color.Red), 1); img.Draw(boundingbox, new Bgr(Color.Green), 2); source.Draw(box, new Bgr(Color.Red), 1); source.Draw(boundingbox, new Bgr(Color.Green), 2); Emgu.CV.Structure.MCvBox2D boxinoriginalImage = box; boxinoriginalImage.center.X += x - boundingbox.X; /// 63=originalboundingbox.x(1069)(I used result of intermediate image)-boundingbox.x(21) boxinoriginalImage.center.Y += y - boundingbox.Y; //// 175=originalboundingbox.x(1211)(I used result of intermediate image)-boundingbox.x(21) double angleInRadius = (((-1) * box.angle) * Math.PI) / 180; // double height = (box.size.Height) / Math.Sin(angleInRadius); // double width = (box.size.Width) / Math.Sin(angleInRadius); double addToX = (((box.size.Width) * 3) / 4) * Math.Cos(angleInRadius); // I have multiplied height to 3/4 because we have four letter in recognized image and I want to find the center of added part for boundingbox double addToY = (((box.size.Width) * 3) / 4) * Math.Sin(angleInRadius); double OriginalpixelToSizeRatio = points.Count() / (box.size.Height * box.size.Width); Emgu.CV.Structure.MCvBox2D addedArea = box; addedArea.center.X += x - boundingbox.X + (float)addToX; addedArea.center.Y += y - boundingbox.Y - (float)addToY; addedArea.size.Width = (float)((box.size.Width) / 2); source1.Draw(boxinoriginalImage, new Bgr(Color.Green), 2); source1.Draw(addedArea, new Bgr(Color.Red), 2); // PointF[] vertices = boxinoriginalImage.GetVertices(); PointF[] AddedAreaVertices = addedArea.GetVertices(); int AddedAreaPixelNumber = 0; double[] slope = new double[4]; double[] b = new double[4]; for (int i = 0; i < AddedAreaVertices.Length; i++) { if (i < AddedAreaVertices.Length - 1) { slope[i] = (AddedAreaVertices[i].Y - AddedAreaVertices[i + 1].Y) / (AddedAreaVertices[i].X - AddedAreaVertices[i + 1].X); } else { slope[i] = (AddedAreaVertices[i].Y - AddedAreaVertices[0].Y) / (AddedAreaVertices[i].X - AddedAreaVertices[0].X); } b[i] = -(AddedAreaVertices[i].Y - slope[i] * AddedAreaVertices[i].X); } for (int i = 0; i < sourceImage1.Width; i++) { for (int j = 0; j < sourceImage1.Height; j++) { Color color = sourceImage1.GetPixel(i, j); double[] result = new double[4]; for (int k = 0; k < result.Length; k++) { result[k] = j - slope[k] * i + b[k]; } if (result[0] < 0 && result[1] > 0 && result[2] > 0 && result[3] < 0) /// explore if pixel is in bounding box of Min Area Rectangle { // sourceImage1.SetPixel(i, j, Color.Green); if (!sourceImage1.GetPixel(i, j).Name.Equals("ffffffff")) { AddedAreaPixelNumber++; } sourceImage1.SetPixel(i, j, Color.Green); } } } double addedAreaSize = addedArea.size.Height * addedArea.size.Width; double AddedAreaPixelToSizeRatio = (double)(AddedAreaPixelNumber) / (addedArea.size.Height * addedArea.size.Width); // PointF[] vertices = boxinoriginalImage.GetVertices(); //// PointF[] otherVertices = addedArea.GetVertices(); // double[] slope = new double[4]; // double[] b=new double[4]; // for (int i = 0; i < vertices.Length;i++) // { // if (i < vertices.Length - 1) // slope[i] = (vertices[i].Y - vertices[i + 1].Y) / (vertices[i].X - vertices[i + 1].X); // else // slope[i] = (vertices[i].Y - vertices[0].Y) / (vertices[i].X - vertices[0].X); // b[i] = -(vertices[i].Y - slope[i] * vertices[i].X); // } // for (int i = 0; i < sourceImage.Width; i++) // { // for (int j = 0; j < sourceImage.Height; j++) // { // Color color = sourceImage.GetPixel(i, j); // double[] result = new double[4]; // for (int k = 0; k < result.Length;k++) // result[k] = j - slope[k] * i + b[k]; // if (result[0] < 0 && result[1] > 0 && result[2] > 0 && result[3] < 0) /// explore if pixel is in bounding box of Min Area Rectangle // { // sourceImage.SetPixel(i, j, Color.Green); // } // } // } foreach (PointF p in pts) { img.Draw(new CircleF(p, 2), new Bgr(Color.Green), 1); } // } sourceImage.Save(@"C:\Users\ronaldiscool\Documents\separationResult.png"); img.Save(@"C:\Users\ronaldiscool\Documents\sample.png"); source.Save(@"C:\Users\ronaldiscool\Documents\sample1.png"); source1.Save(@"C:\Users\ronaldiscool\Documents\sample2.png"); sourceImage1.Save(@"C:\Users\ronaldiscool\Documents\sample3.png"); }
public void AddNeighbor(List <TessResult> tessOcrResultList, string inputpath) { string file_name = ""; List <PointF> points = new List <PointF>(); Boolean Continue = true; int Counter = 0; SVM SVMForClassification = new SVM(@"C:\Users\nhonarva\Documents\MachineLearning\sampleTraintData\"); for (int k = 0; k < tessOcrResultList.Count; k++) /// add neighbors of each bounding box { points = new List <PointF>(); string TotalPixelToSizeRatio = ""; file_name = tessOcrResultList[k].fileName; Bitmap ResultOfTextDetection; string[] split = file_name.Split('_'); if (split[6].Equals("0")) { ResultOfTextDetection = new Bitmap(inputpath + file_name + ".png"); } else { string newFileName = ""; split[6] = "0"; for (int j = 0; j < split.Length; j++) { if (j < split.Length - 1) { newFileName += split[j] + "_"; } else { newFileName += split[j]; } } ResultOfTextDetection = new Bitmap(inputpath + newFileName + ".png"); } Bitmap OriginalBinaryImage = new Bitmap(inputpath + "BinaryOutput.png"); for (int i = 0; i < ResultOfTextDetection.Width; i++) //// scan image to find foreground pixels { for (int j = 0; j < ResultOfTextDetection.Height; j++) { Color color = ResultOfTextDetection.GetPixel(i, j); if (!ResultOfTextDetection.GetPixel(i, j).Name.Equals("ffffffff")) { PointF point = new PointF(i, j); points.Add(point); } } } String[] splitTokens = file_name.Split('_'); int numberOfRecognizedLetters = tessOcrResultList[k].tess_word3.Length - 2; /// -2 because we have \n\n at the end of word int x = Convert.ToInt32(tessOcrResultList[k].x); int y = Convert.ToInt32(tessOcrResultList[k].y); int width = Convert.ToInt32(tessOcrResultList[k].w); int height = Convert.ToInt32(tessOcrResultList[k].h); if (splitTokens[6].Equals("0")) /// bounding box with slope 0 (horizental) { int estimatedSizeOfOneLetter = width / numberOfRecognizedLetters; double pixelToSizeRatio = (double)(points.Count) / (double)(width * height); TotalPixelToSizeRatio = Convert.ToString(Math.Round((decimal)pixelToSizeRatio, 2)); Point left = new Point(); /// left coordinate for box at left hand side of blob int leftPixelCount = 0; double leftPixelToSizeRatio = 0; left.X = x - estimatedSizeOfOneLetter; left.Y = y; Point right = new Point(); //// right coordinate for box at right hand side of blob int rightPixelCount = 0; double rightPixelToSizeRatio = 0; right.X = x + width; right.Y = y; Continue = true; if (char.IsUpper(tessOcrResultList[k].tess_word3.ToCharArray()[0])) { Continue = false; } if (left.X < 0) { Continue = false; } // continue; int AddedAreaWidth = estimatedSizeOfOneLetter; Counter = 0; while (Continue) { if ((left.X - estimatedSizeOfOneLetter) < 0) { AddedAreaWidth = left.X; } int[,] AddedAreaPoints = new int[AddedAreaWidth, height]; for (int i = left.X; i < left.X + AddedAreaWidth; i++) { for (int j = left.Y; j < left.Y + height; j++) { if ((!OriginalBinaryImage.GetPixel(i, j).Name.Equals("ffffffff"))) { leftPixelCount++; AddedAreaPoints[i - left.X, j - left.Y] = 1; } else { AddedAreaPoints[i - left.X, j - left.Y] = 0; } } } Counter++; leftPixelToSizeRatio = (double)(leftPixelCount) / (double)(AddedAreaWidth * height); QGISJsonNeighbothood.AddFeature(left.X, left.Y, left.X + AddedAreaWidth, left.Y, left.X + AddedAreaWidth, left.Y + tessOcrResultList[k].h, left.X, left.Y + tessOcrResultList[k].h, tessOcrResultList[k].id + "-" + Convert.ToString(Counter), Convert.ToString(Math.Round((decimal)leftPixelToSizeRatio, 2)), -1); left.X -= estimatedSizeOfOneLetter; leftPixelCount = 0; if (left.X < 0) { Continue = false; } if (leftPixelToSizeRatio < 0.2) { Continue = false; } if (leftPixelToSizeRatio > 0.2) { SVMForClassification.Classification(AddedAreaPoints, tessOcrResultList[k].id + "-" + Convert.ToString(Counter)); } } Continue = true; if (right.X > OriginalBinaryImage.Width) { Continue = false; } AddedAreaWidth = estimatedSizeOfOneLetter; Counter = 0; while (Continue) { if ((right.X + estimatedSizeOfOneLetter) > OriginalBinaryImage.Width) { AddedAreaWidth = OriginalBinaryImage.Width - right.X; } int[,] AddedAreaPoints = new int[AddedAreaWidth, height]; for (int i = right.X; i < right.X + AddedAreaWidth; i++) { for (int j = right.Y; j < right.Y + height; j++) { if (!OriginalBinaryImage.GetPixel(i, j).Name.Equals("ffffffff")) { rightPixelCount++; AddedAreaPoints[i - right.X, j - right.Y] = 1; } else { AddedAreaPoints[i - right.X, j - right.Y] = 0; } } } Counter++; rightPixelToSizeRatio = (double)(rightPixelCount) / (double)(AddedAreaWidth * height); QGISJsonNeighbothood.AddFeature(right.X, right.Y, right.X + AddedAreaWidth, right.Y, right.X + AddedAreaWidth, right.Y + tessOcrResultList[k].h, right.X, right.Y + tessOcrResultList[k].h, tessOcrResultList[k].id + "-" + Convert.ToString(Counter), Convert.ToString(Math.Round((decimal)rightPixelToSizeRatio, 2)), -1); right.X += estimatedSizeOfOneLetter; rightPixelCount = 0; if (right.X > OriginalBinaryImage.Width) { Continue = false; } if (rightPixelToSizeRatio < 0.2) { Continue = false; } if (rightPixelToSizeRatio > 0.2) { SVMForClassification.Classification(AddedAreaPoints, tessOcrResultList[k].id + "-" + Convert.ToString(Counter)); } } // } QGISJsonNeighbothood.AddFeature(x, y, x + width, y, x + width, y + height, x, y + height, tessOcrResultList[k].id, TotalPixelToSizeRatio, -1); } else { PointF[] pts; pts = points.ToArray(); Emgu.CV.Structure.MCvBox2D box = PointCollection.MinAreaRect(pts); Rectangle boundingbox = PointCollection.BoundingRectangle(pts); Emgu.CV.Structure.MCvBox2D boxinoriginalImage = box; boxinoriginalImage.center.X += x - boundingbox.X; /// 63=originalboundingbox.x(1069)(I used result of intermediate image)-boundingbox.x(21) boxinoriginalImage.center.Y += y - boundingbox.Y; PointF[] vertices = boxinoriginalImage.GetVertices(); double pixelToSizeRatio = (double)(points.Count) / (double)(box.size.Width * box.size.Height); TotalPixelToSizeRatio = Convert.ToString(Math.Round((decimal)pixelToSizeRatio, 2)); QGISJsonNeighbothood.AddFeature(vertices[0].X, vertices[0].Y, vertices[1].X, vertices[1].Y, vertices[2].X, vertices[2].Y, vertices[3].X, vertices[3].Y, tessOcrResultList[k].id, TotalPixelToSizeRatio, -1); for (int m = 1; m < 4; m++) { Emgu.CV.Structure.MCvBox2D addedArea = box; if (box.size.Height > box.size.Width) ///Non horizental bounding boxes that slope of bounding box is less than zero { double angleInRadius = ((90 + box.angle) * Math.PI) / 180; double addToX = (((box.size.Height) * ((m - 1) * 2 + numberOfRecognizedLetters + 1)) / (numberOfRecognizedLetters * 2)) * Math.Cos(angleInRadius); // I have multiplied height to 3/4 because we have four letter in recognized image and I want to find the center of added part for boundingbox double addToY = (((box.size.Height) * ((m - 1) * 2 + numberOfRecognizedLetters + 1)) / (numberOfRecognizedLetters * 2)) * Math.Sin(angleInRadius); addedArea.center.X += (x - boundingbox.X) + (float)addToX; addedArea.center.Y += (y - boundingbox.Y) + (float)addToY; addedArea.size.Height = (float)((box.size.Height) / numberOfRecognizedLetters); } else ///Non horizental bounding boxes that slope of bounding box is greater than zero { double angleInRadius = (((-1) * box.angle) * Math.PI) / 180; double addToX = (((box.size.Width) * ((m - 1) * 2 + numberOfRecognizedLetters + 1)) / (numberOfRecognizedLetters * 2)) * Math.Cos(angleInRadius); // I have multiplied height to 3/4 because we have four letter in recognized image and I want to find the center of added part for boundingbox double addToY = (((box.size.Width) * ((m - 1) * 2 + numberOfRecognizedLetters + 1)) / (numberOfRecognizedLetters * 2)) * Math.Sin(angleInRadius); addedArea.center.X += (x - boundingbox.X) - (float)addToX; addedArea.center.Y += (y - boundingbox.Y) + (float)addToY; addedArea.size.Width = (float)((box.size.Width) / numberOfRecognizedLetters); } PointF[] AddedAreaVertices = addedArea.GetVertices(); PointF ExploreAddedArea_MinPoint = AddedAreaVertices[0]; PointF ExploreAddedArea_MaxPoint = AddedAreaVertices[0]; for (int i = 1; i < AddedAreaVertices.Length; i++) { if (AddedAreaVertices[i].X < ExploreAddedArea_MinPoint.X) { ExploreAddedArea_MinPoint.X = AddedAreaVertices[i].X; } if (AddedAreaVertices[i].Y < ExploreAddedArea_MinPoint.Y) { ExploreAddedArea_MinPoint.Y = AddedAreaVertices[i].Y; } if (AddedAreaVertices[i].X > ExploreAddedArea_MaxPoint.X) { ExploreAddedArea_MaxPoint.X = AddedAreaVertices[i].X; } if (AddedAreaVertices[i].Y > ExploreAddedArea_MaxPoint.Y) { ExploreAddedArea_MaxPoint.Y = AddedAreaVertices[i].Y; } } int AddedAreaPixelNumber = 0; double[] slope = new double[4]; double[] b = new double[4]; for (int i = 0; i < AddedAreaVertices.Length; i++) //// find four lines that create min area rectangle bounding box { if (i < AddedAreaVertices.Length - 1) { slope[i] = (AddedAreaVertices[i].Y - AddedAreaVertices[i + 1].Y) / (AddedAreaVertices[i].X - AddedAreaVertices[i + 1].X); } else { slope[i] = (AddedAreaVertices[i].Y - AddedAreaVertices[0].Y) / (AddedAreaVertices[i].X - AddedAreaVertices[0].X); } b[i] = -(AddedAreaVertices[i].Y - slope[i] * AddedAreaVertices[i].X); } for (int i = Convert.ToInt32(ExploreAddedArea_MinPoint.X); i < Convert.ToInt32(ExploreAddedArea_MaxPoint.X) + 1; i++) { for (int j = Convert.ToInt32(ExploreAddedArea_MinPoint.Y); j < Convert.ToInt32(ExploreAddedArea_MaxPoint.Y) + 1; j++) { if ((i < OriginalBinaryImage.Width) && (j < OriginalBinaryImage.Width) && (i > 0) && (j > 0)) { Color color = OriginalBinaryImage.GetPixel(i, j); double[] result = new double[4]; for (int l = 0; l < result.Length; l++) { result[l] = j - slope[l] * i + b[l]; } if (result[0] < 0 && result[1] > 0 && result[2] > 0 && result[3] < 0) /// explore if pixel is in bounding box of Min Area Rectangle { // sourceImage1.SetPixel(i, j, Color.Green); if (!OriginalBinaryImage.GetPixel(i, j).Name.Equals("ffffffff")) { AddedAreaPixelNumber++; } } } else { break; } } } double addedAreaSize = addedArea.size.Height * addedArea.size.Width; double AddedAreaPixelToSizeRatio = (double)(AddedAreaPixelNumber) / (addedArea.size.Height * addedArea.size.Width); QGISJsonNeighbothood.AddFeature(AddedAreaVertices[0].X, AddedAreaVertices[0].Y, AddedAreaVertices[1].X, AddedAreaVertices[1].Y, AddedAreaVertices[2].X, AddedAreaVertices[2].Y, AddedAreaVertices[3].X, AddedAreaVertices[3].Y, tessOcrResultList[k].id, Convert.ToString(Math.Round((decimal)AddedAreaPixelToSizeRatio, 2)), -1); } for (int m = 1; m < 4; m++) { Emgu.CV.Structure.MCvBox2D addedArea = box; if (box.size.Height > box.size.Width) ///Non horizental bounding boxes that slope of bounding box is less than zero { double angleInRadius = ((90 + box.angle) * Math.PI) / 180; double addToX = (((box.size.Height) * ((m - 1) * 2 + numberOfRecognizedLetters + 1)) / (numberOfRecognizedLetters * 2)) * Math.Cos(angleInRadius); // I have multiplied height to 3/4 because we have four letter in recognized image and I want to find the center of added part for boundingbox double addToY = (((box.size.Height) * ((m - 1) * 2 + numberOfRecognizedLetters + 1)) / (numberOfRecognizedLetters * 2)) * Math.Sin(angleInRadius); addedArea.center.X += (x - boundingbox.X) - (float)addToX; addedArea.center.Y += (y - boundingbox.Y) - (float)addToY; addedArea.size.Height = (float)((box.size.Height) / numberOfRecognizedLetters); } else ///Non horizental bounding boxes that slope of bounding box is greater than zero { double angleInRadius = (((-1) * box.angle) * Math.PI) / 180; double addToX = (((box.size.Width) * ((m - 1) * 2 + numberOfRecognizedLetters + 1)) / (numberOfRecognizedLetters * 2)) * Math.Cos(angleInRadius); // I have multiplied height to 3/4 because we have four letter in recognized image and I want to find the center of added part for boundingbox double addToY = (((box.size.Width) * ((m - 1) * 2 + numberOfRecognizedLetters + 1)) / (numberOfRecognizedLetters * 2)) * Math.Sin(angleInRadius); addedArea.center.X += (x - boundingbox.X) + (float)addToX; addedArea.center.Y += (y - boundingbox.Y) - (float)addToY; addedArea.size.Width = (float)((box.size.Width) / numberOfRecognizedLetters); } PointF[] AddedAreaVertices = addedArea.GetVertices(); PointF ExploreAddedArea_MinPoint = AddedAreaVertices[0]; PointF ExploreAddedArea_MaxPoint = AddedAreaVertices[0]; for (int i = 1; i < AddedAreaVertices.Length; i++) { if (AddedAreaVertices[i].X < ExploreAddedArea_MinPoint.X) { ExploreAddedArea_MinPoint.X = AddedAreaVertices[i].X; } if (AddedAreaVertices[i].Y < ExploreAddedArea_MinPoint.Y) { ExploreAddedArea_MinPoint.Y = AddedAreaVertices[i].Y; } if (AddedAreaVertices[i].X > ExploreAddedArea_MaxPoint.X) { ExploreAddedArea_MaxPoint.X = AddedAreaVertices[i].X; } if (AddedAreaVertices[i].Y > ExploreAddedArea_MaxPoint.Y) { ExploreAddedArea_MaxPoint.Y = AddedAreaVertices[i].Y; } } int AddedAreaPixelNumber = 0; double[] slope = new double[4]; double[] b = new double[4]; for (int i = 0; i < AddedAreaVertices.Length; i++) //// find four lines that create min area rectangle bounding box { if (i < AddedAreaVertices.Length - 1) { slope[i] = (AddedAreaVertices[i].Y - AddedAreaVertices[i + 1].Y) / (AddedAreaVertices[i].X - AddedAreaVertices[i + 1].X); } else { slope[i] = (AddedAreaVertices[i].Y - AddedAreaVertices[0].Y) / (AddedAreaVertices[i].X - AddedAreaVertices[0].X); } b[i] = -(AddedAreaVertices[i].Y - slope[i] * AddedAreaVertices[i].X); } for (int i = Convert.ToInt32(ExploreAddedArea_MinPoint.X); i < Convert.ToInt32(ExploreAddedArea_MaxPoint.X) + 1; i++) { for (int j = Convert.ToInt32(ExploreAddedArea_MinPoint.Y); j < Convert.ToInt32(ExploreAddedArea_MaxPoint.Y) + 1; j++) { if ((i < OriginalBinaryImage.Width) && (j < OriginalBinaryImage.Width) && (i > 0) && (j > 0)) { Color color = OriginalBinaryImage.GetPixel(i, j); double[] result = new double[4]; for (int l = 0; l < result.Length; l++) { result[l] = j - slope[l] * i + b[l]; } if (result[0] < 0 && result[1] > 0 && result[2] > 0 && result[3] < 0) /// explore if pixel is in bounding box of Min Area Rectangle { // sourceImage1.SetPixel(i, j, Color.Green); if (!OriginalBinaryImage.GetPixel(i, j).Name.Equals("ffffffff")) { AddedAreaPixelNumber++; } } } else { break; } } } double addedAreaSize = addedArea.size.Height * addedArea.size.Width; double AddedAreaPixelToSizeRatio = (double)(AddedAreaPixelNumber) / (addedArea.size.Height * addedArea.size.Width); QGISJsonNeighbothood.AddFeature(AddedAreaVertices[0].X, AddedAreaVertices[0].Y, AddedAreaVertices[1].X, AddedAreaVertices[1].Y, AddedAreaVertices[2].X, AddedAreaVertices[2].Y, AddedAreaVertices[3].X, AddedAreaVertices[3].Y, tessOcrResultList[k].id, Convert.ToString(Math.Round((decimal)AddedAreaPixelToSizeRatio, 2)), -1); } } } QGISJsonNeighbothood.WriteGeojsonFileByPixels(); }