/// <summary> /// Constructor /// </summary> /// <param name="Configuration">Search configuration parameters</param> public Board(SearchConfigurator Configuration) { this.ImageBlobs = new Emgu.CV.Cvb.CvBlobs(); this.BlobDetector = new Emgu.CV.Cvb.CvBlobDetector(); this.BoardSize = new Size(400, 400); this.dW = (int)(this.BoardSize.Width / 8); this.dH = (int)(this.BoardSize.Height / 8); this.BoxScaling = 15; this.OutputImageSize = new Size(640, 480); this.SC = Configuration; }
/// <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 board markers. /// </summary> /// <param name="InputImage"></param> /// <returns></returns> private PointF[] GetBoardMarkers(Bitmap InputImage, SearchConfigurator Configuration) { #region Local variables // Main image. Image <Gray, Byte> ModelImage; // Center of image PointF centerOfImage = new PointF((InputImage.Width / 2), (InputImage.Height / 2)); // For processing ModelImage = new Image <Gray, Byte>(InputImage); // Regionpoints PointF[] listOfEdges = new PointF[MARKERS_COUNT]; #endregion // Smooth the image, with kernel size 3. ModelImage = ModelImage.SmoothGaussian(Configuration.SmoothKernelSize); // Show unperspective image //CvInvoke.cvShowImage("Stage 1", ModelImage); // Adaptive tresh ModelImage = ModelImage.ThresholdAdaptive(new Gray(Configuration.TreshholdLevel), Emgu.CV.CvEnum.ADAPTIVE_THRESHOLD_TYPE.CV_ADAPTIVE_THRESH_GAUSSIAN_C, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY_INV, 7, new Gray(4)); // Dilate one iteration the image. ModelImage = ModelImage.Dilate(1); // Show unperspective image //CvInvoke.cvShowImage("Stage 2", ModelImage); // Get the numers of blobs on the image. uint numBlobsFound = BlobDetector.Detect(ModelImage, ImageBlobs); // Lets try and iterate the blobs foreach (Emgu.CV.Cvb.CvBlob targetBlob in ImageBlobs.Values) { // Calculate circularity MemStorage BlbobStorage = new MemStorage(); Contour <Point> blobContour = targetBlob.GetContour(BlbobStorage); double blobCircularity = this.GetCircularity(blobContour); // Calculate area double blobArea = targetBlob.Area; // Center image PointF blobCenter = targetBlob.Centroid; // Filter the marker by area if ((blobArea >= Configuration.MinimumMarkerArea) && (blobArea <= Configuration.MaximumMarkerArea) && (blobCircularity > Configuration.MarkerCircularity)) { Console.WriteLine("A: {0} P: {1} C: {2}", blobArea, 1, blobCircularity); #region Find msrker quadrant position if ((blobCenter.X < centerOfImage.X) && (blobCenter.Y < centerOfImage.Y)) { Console.WriteLine("Quadrant: I"); listOfEdges[0] = targetBlob.Centroid; } if ((blobCenter.X > centerOfImage.X) && (blobCenter.Y < centerOfImage.Y)) { Console.WriteLine("Quadrant: II"); listOfEdges[1] = targetBlob.Centroid; } if ((blobCenter.X > centerOfImage.X) && (blobCenter.Y > centerOfImage.Y)) { Console.WriteLine("Quadrant: III"); listOfEdges[2] = targetBlob.Centroid; } if ((blobCenter.X < centerOfImage.X) && (blobCenter.Y > centerOfImage.Y)) { Console.WriteLine("Quadrant: IV"); listOfEdges[3] = targetBlob.Centroid; } #endregion } } // If there is 4 markers return them if (listOfEdges.Length == 4) { return(listOfEdges); } // If not null return(null); }