/// <summary> /// Proces the image. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnProcessImage_Click(object sender, EventArgs e) { try { // Get image from image source. this.imageFromCamera = this.camera.Capture(); // Create search engine configuration. this.boardConfiguration = new SearchConfigurator(); // Create processor. this.playerVS = new VisionSystem.Board(this.boardConfiguration); // Process the image and get the board configuration. this.currentBoardConfiguration = playerVS.FindBoard(this.imageFromCamera, out this.outputImage); this.currentBoardConfiguration.Black = this.ComputerIndex; this.currentBoardConfiguration.White = this.HumanIndex; // Apply the processed image. pbMain.Image = new Bitmap(this.imageFromCamera, pbMain.Size); // Apply the processed image. pbProcessed.Image = this.outputImage; // Print configuration ti the console. Console.WriteLine(this.currentBoardConfiguration.GetBoardAsString()); // Upadate UI tsslblStatusProces.Text = "Last action: search for the board."; } catch (Exception exception) { tsslblStatusProces.Text = String.Format("Exception: {0}", exception.Message); Loging.Log.CreateRecord("MainForm.btnProcessImage_Click", String.Format("Exception: {0}", exception.Message), Loging.LogMessageTypes.Error); } }
/// <summary> /// Find the chessboard corners /// </summary> /// <param name="InputImage">Input image commpping from video camera.</param> /// <param name="OutputImage">Output image comming from image processor.</param> public static BoardConfiguration FindBoard(Bitmap InputImage, out Bitmap OutputImage) { #region Local variables // Figure Center Point figureCenter; // Figure BGR color Bgr bgrInFigureColor; // Vector image of figure CircleF circleFigure; // Color colorTransformedColor; // double hueChanel; double saturationChanel; double valueChanel; // Color circleColor; // Board configuration BoardConfiguration currentConfiguration = new BoardConfiguration(); #endregion #region Equalize the image // Convert a BGR image to HLS range Emgu.CV.Image <Hls, Byte> imageHsi = new Image <Hls, Byte>(InputImage); // Equalize the Intensity Channel imageHsi[1]._EqualizeHist(); imageHsi[2]._EqualizeHist(); // Convert the image back to BGR range Emgu.CV.Image <Bgr, Byte> DrawingImage = imageHsi.Convert <Bgr, Byte>(); // Geometric orientation image Emgu.CV.Image <Gray, Byte> BlobImage = imageHsi[1].Convert <Gray, Byte>(); // Create the font. MCvFont TextFont = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.5, 1.5); #endregion try { // Get bord marker points, MarkersList = Board.GetBoardMarkers(BlobImage.ToBitmap()); // If repers are 4 the proseed to describe the board. if (MarkersList.Length == MarkersCount) { // Set playboard size. Board.SetSize(BoardSize); #region Get perspective flat image. // Get perspective flat image. Image <Bgr, byte> newImage = SupportMethods.GetPerspectiveFlatImage(DrawingImage, BoardSize, MarkersList); #endregion for (int indexFigure = 0; indexFigure < 24; indexFigure++) { // Set the figure center figureCenter = Board.FigurePosition(indexFigure); // bgrInFigureColor = Board.GetFigureColor(newImage, Board.GetBoundingBox(figureCenter)); // colorTransformedColor = Color.FromArgb(255, (byte)bgrInFigureColor.Red, (byte)bgrInFigureColor.Green, (byte)bgrInFigureColor.Blue); // SupportMethods.ColorToHSV(colorTransformedColor, out hueChanel, out saturationChanel, out valueChanel); // Preset the color configuration valueChanel = 1.0; saturationChanel = 1.0; // Find the playr int figure = FigureColorDefinitions.WhoIsThePlayer((int)hueChanel); // Fill the board configuration currentConfiguration.Figure[indexFigure] = figure; #region Draw if (!((hueChanel > HueChanelMin) && (hueChanel < HueChanelMax)) && (figure != -1)) { circleColor = SupportMethods.ColorFromHSV(hueChanel, saturationChanel, valueChanel); // Create figure to draw. circleFigure = new CircleF(figureCenter, 10.0f); // Draw circle newImage.Draw(circleFigure, new Bgr(circleColor), 5); // Write text // #region Draw label to the processed image. newImage.Draw(String.Format("{0}", FigureColorDefinitions.WhoIsThePlayer((int)hueChanel)), ref TextFont, figureCenter, new Bgr(0, 0, 0)); //drawingImage.Draw(vectorWithSmallestY, RedColor, 2); #endregion } #endregion } // Show unperspective image //CvInvoke.cvShowImage("Perspective", newImage); DrawingImage = newImage; } } catch (Exception exception) { Console.WriteLine("Exception: {0}", exception.Message); } // Apply the output image. OutputImage = new Bitmap(DrawingImage.Resize(OutputImageWidth, OutputImageHeight, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR).ToBitmap()); return(currentConfiguration); }
/// <summary> /// Find the chessboard corners /// </summary> /// <param name="InputImage">Input image commpping from video camera.</param> /// <param name="OutputImage">Output image comming from image processor.</param> public BoardConfiguration FindBoard(Bitmap InputImage, out Bitmap OutputImage) { #region Local variables // Figure Center Point figureCenter; // Figure BGR color Color figureColor; // Figure index int figureIndex; // Vector image of figure CircleF circleFigure; // Board configuration BoardConfiguration currentConfiguration = new BoardConfiguration(); // Image ! Image <Bgr, Byte> WorkingImage; #endregion // Create working image. WorkingImage = new Image <Bgr, Byte>(InputImage); // Equlize the image. WorkingImage = this.Equalize(WorkingImage, this.SC.ImageLightnes); // Convert the image back to BGR range Emgu.CV.Image <Bgr, Byte> DrawingImage = WorkingImage; // Create the font. MCvFont TextFont = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_COMPLEX, 1.0, 1.0); try { // Get bord marker points, MarkersList = this.GetBoardMarkers(new Image <Hls, Byte>(InputImage)[1].Convert <Gray, Byte>().ToBitmap(), this.SC); // If repers are 4 the proseed to describe the board. if (MarkersList.Length == MARKERS_COUNT) { // Draw the points foreach (PointF markerPoint in MarkersList) { // Create figure to draw. circleFigure = new CircleF(markerPoint, 5.0f); // Draw circle DrawingImage.Draw(circleFigure, new Bgr(Color.Orange), 10); } // Show the image //CvInvoke.cvShowImage("Test", DrawingImage); #region Get perspective flat image. // Get perspective flat image. Image <Bgr, Byte> flatImage = this.GetPerspectiveFlatImage(WorkingImage, BoardSize, MarkersList); #endregion for (int indexFigure = 0; indexFigure < 24; indexFigure++) { // Set the figure center figureCenter = this.FigurePosition(indexFigure); // figureColor = this.GetFigureColor(flatImage, this.GetBoundingBox(figureCenter)); // Tell that there is no figure figureIndex = 0; // If the luminosity is greater then 0.5. if (figureColor.GetSaturation() > this.SaturationTreshFigureLevel) { // Find the playr figureIndex = FigureColorDefinitions.WhoIsThePlayer(figureColor); } // Fill the board configuration currentConfiguration.Figure[indexFigure] = figureIndex; #region Draw if (figureIndex != 0) { // Create figure to draw. //circleFigure = new CircleF(figureCenter, 10.0f); // Draw circle //newImage.Draw(circleFigure, bgrInFigureColor, 5); // Write text // #region Draw label to the processed image. flatImage.Draw(String.Format("{0}", FigureColorDefinitions.WhoIsThePlayer(figureColor)), ref TextFont, figureCenter, new Bgr(Color.DarkOrange)); //drawingImage.Draw(vectorWithSmallestY, RedColor, 2); #endregion } #endregion } // Show unperspective image //CvInvoke.cvShowImage("Perspective", flatImage); DrawingImage = flatImage; } } catch (Exception exception) { Console.WriteLine("Exception: {0}", exception.Message); Loging.Log.CreateRecord("VisionSystm.Board.FindBoard", String.Format("Exception: {0}", exception.Message), Loging.LogMessageTypes.Error); } // Apply the output image. DrawingImage.ROI = new Rectangle(new Point(0, 0), new Size(400, 400)); OutputImage = DrawingImage.ToBitmap(); return(currentConfiguration); }