private void imgToArray() { //this takes into consideration the maximum size of the robot canvas //areas outside the robot canvas gets cropped as they are unreachable ratioWidthToPx = GetImgWidth / Img.PixelWidth; //calculates the ratio between picture width in mm and number of pixels in width ratioHeightToPx = GetImgHeight / Img.PixelHeight; double usableImgWidth = GetImgWidth; if (usableImgWidth - ImgMoveX > RobotWidth) { usableImgWidth = RobotWidth - ImgMoveX; } double usableImgHeight = GetImgHeight; if (usableImgHeight - ImgMoveY > RobotHeight) { usableImgHeight = RobotHeight - ImgMoveY; } pxArrayWidth = (int)(usableImgWidth / ratioWidthToPx); pxArrayHeight = (int)(usableImgHeight / ratioHeightToPx); int blackPixelThreshold = 60; pixelArray = new bool[pxArrayWidth, pxArrayHeight]; for (int x = 0; x < pxArrayWidth; x++) { for (int y = 0; y < pxArrayHeight; y++) { Color pixelColor = TempImg.GetPixel(x, y); //Img.CopyPixels() //if (pixelColor.R <= blackPixelThreshold && pixelColor.G <= blackPixelThreshold && pixelColor.B <= blackPixelThreshold) if (pixelColor.R == pixelColor.G && pixelColor.R == pixelColor.B && pixelColor.R <= blackPixelThreshold) { pixelArray[x, y] = true; //true = black pixel } else { pixelArray[x, y] = false; //false = white pixel } } } }
private void ProcessFrame(object sender, EventArgs arg) { Image <Bgr, Byte> frame = capture.QueryFrame().Resize(CamImageBox.Width, CamImageBox.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); // ImageFrame = ImageFrame.ThresholdBinary(new Gray(150), new Gray(255)); // Convert the image to hsv Image <Hsv, Byte> hsvFrame = frame.Convert <Hsv, Byte>(); Image <Gray, Byte>[] channels = hsvFrame.Split(); // split the hsv into components Image <Gray, Byte> imgHue = channels[0]; Image <Gray, Byte> imgSat = channels[1]; Image <Gray, Byte> imgVal = channels[2]; // Link to trackbars and filter Image <Gray, Byte> hueFilter = imgHue.InRange(new Gray(h_min), new Gray(h_max)); Image <Gray, Byte> satFilter = imgSat.InRange(new Gray(s_min), new Gray(s_max)); Image <Gray, Byte> valFilter = imgVal.InRange(new Gray(v_min), new Gray(v_max)); // Sum the images into one image Image <Gray, Byte> combined = hueFilter.And(satFilter).And(valFilter).SmoothMedian(5); // Display CamImageBox.Image = frame; imageBox3.Image = hueFilter.Resize(imageBox3.Width, imageBox3.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); imageBox4.Image = satFilter.Resize(imageBox4.Width, imageBox4.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); imageBox5.Image = valFilter.Resize(imageBox5.Width, imageBox4.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); imageBox2.Image = combined.Resize(imageBox2.Width, imageBox2.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); // Find the contour with the largest area double max_area = 0; Rectangle max_bounding_box = new Rectangle(); for (Contour <Point> contours = combined.FindContours(); contours != null; contours = contours.HNext) { if (contours.Area > max_area) { max_area = contours.Area; max_bounding_box = contours.BoundingRectangle; } } // Draw a bounding box on it frame.Draw(max_bounding_box, new Bgr(Color.Red), 4); combined.ROI = max_bounding_box; // Display imageBox1.Image = combined.Resize(imageBox1.Width, imageBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); //imageBox1.Image = frame.Resize(imageBox1.Width, imageBox1.Height, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); Image <Gray, Byte> TempImg; // temp image to get the black pixels TempImg = combined; // assign the captured image TempImg = TempImg.Resize(3, 5, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); // Recognize digit List <int> MyList; MyList = new List <int>(); int k = 0; // Get light pixels for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { if (TempImg.Data[i, j, 0] == 255) { MyList.Add(k); } k++; } } // Convert to an array of integers int[] results = MyList.ToArray <int>(); // Display Test //AllocConsole(); //for (int i = 0; i < results.Length; i++) // Console.Write(results[i] + " "); //Console.WriteLine(); // Check match sequence in List and get index if match (index is the digit 0->9) k = numbers.FindIndex(collection => collection.SequenceEqual(results)); // Display recognized number this.label1.Text = k.ToString(); }