private void chessRecognitionToolStripMenuItem_Click(object sender, EventArgs e) { if (img == null) // verify if the image is already opened { return; } Cursor = Cursors.WaitCursor; // clock cursor //copy Undo Image imgUndo = img.Copy(); Rectangle rectangulo; string angle; string[,] Pieces; ImageClass.Chess_Recognition(img, img, out rectangulo, out angle, out Pieces); ImageViewer.Image = img.Bitmap; ImageViewer.Refresh(); // refresh image on the screen Cursor = Cursors.Default; // normal cursor }
private void chessRecognitionToolStripMenuItem_Click(object sender, EventArgs e) { if (img == null) // verify if the image is already opened { return; } Cursor = Cursors.WaitCursor; // clock cursor //copy Undo Image imgUndo = img.Copy(); Color red = Color.Red; Pen p = new Pen(red); p.Width = 4; ImageClass.Chess_Recognition(img, imgUndo, out Rectangle BD_Location, out string Angle, out string[,] Pieces); Graphics g = Graphics.FromImage(img.Bitmap); //g.DrawRectangle(p, BD_Location); float angle = float.Parse(Angle, System.Globalization.CultureInfo.InvariantCulture); using (Matrix m = new Matrix()) { m.RotateAt(angle, new PointF(BD_Location.Left, BD_Location.Top)); g.Transform = m; g.DrawRectangle(p, BD_Location); g.ResetTransform(); } ////Convert the image to grayscale and filter out the noise //Image<Gray, Byte> gray = img.Convert<Gray, Byte>().PyrDown().PyrUp(); //Gray cannyThreshold = new Gray(180); //Gray cannyThresholdLinking = new Gray(120); //Gray circleAccumulatorThreshold = new Gray(120); //CircleF[] circles = gray.HoughCircles( // cannyThreshold, // circleAccumulatorThreshold, // 5.0, //Resolution of the accumulator used to detect centers of the circles // 10.0, //min distance // 5, //min radius // 0 //max radius // )[0]; //Get the circles from the first channel //Image<Gray, Byte> cannyEdges = gray.Canny(cannyThreshold.Intensity, cannyThresholdLinking.Intensity); //LineSegment2D[] lines = cannyEdges.HoughLinesBinary( // 1, //Distance resolution in pixel-related units // Math.PI / 45.0, //Angle resolution measured in radians. // 20, //threshold // 30, //min Line width // 2 //gap between lines // )[0]; //Get the lines from the first channel //#region Find rectangles //List<MCvBox2D> boxList = new List<MCvBox2D>(); //using (MemStorage storage = new MemStorage()) //allocate storage for contour approximation // for (Contour<System.Drawing.Point> contours = cannyEdges.FindContours(); contours != null; contours = contours.HNext) // { // Contour<System.Drawing.Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage); // if (contours.Area > 250) //only consider contours with area greater than 250 // { // if (currentContour.Total == 4) //The contour has 4 vertices. // { // #region determine if all the angles in the contour are within the range of [80, 100] degree // bool isRectangle = true; // System.Drawing.Point[] pts = currentContour.ToArray(); // LineSegment2D[] edges = PointCollection.PolyLine(pts, true); // for (int i = 0; i < edges.Length; i++) // { // double angle = Math.Abs( // edges[(i + 1) % edges.Length].GetExteriorAngleDegree(edges[i])); // if (angle < 80 || angle > 100) // { // isRectangle = false; // break; // } // } // #endregion // if (isRectangle) boxList.Add(currentContour.GetMinAreaRect()); // } // } // } //#endregion //#region draw rectangles //Image<Bgr, Byte> RectangleImage = img.CopyBlank(); //foreach (MCvBox2D box in boxList) // RectangleImage.Draw(box, new Bgr(Color.DarkOrange), 2); //#endregion ImageViewer.Image = img.Bitmap; ImageViewer.Refresh(); // refresh image on the screen Cursor = Cursors.Default; // normal cursor }