/// <summary> /// Saves an image to file. /// If image is holed, converts hole to white space. /// </summary> /// <param name="suppliedPath">If no path is specified, will overwrite original file</param> public void SaveChanges(string suppliedPath = null) { for (int i = 0; i < _rows; i++) { for (int j = 0; j < _cols; j++) { var val = Matrix.GetArrayElement(i, j).Value; // we cannot put -1 in the real image if (val == -1) { val = 1; } _img.Data[i, j, 0] = Convert.ToByte(val * 255); } } var t = _img.ToBitmap(); if (suppliedPath == null) { t.Save(_path); } else { t.Save(suppliedPath); } }
/// <summary> /// Process rocks in the image. /// </summary> private void ProcessSand() { // TODO: Make image be saved in MyDocs/Pictures. if (this.inputImage == null) { return; } Thread workerThread = new Thread(() => { /* * string path = @"C:\Users\POLYGONTeam\Documents\GitHub\Androbot\Androbot\Androbot\bin\Debug\Images\2D_20160728170358.PNG"; */ //Emgu.CV.Image<Bgr, byte> inpImg = new Emgu.CV.Image<Bgr, byte>(this.inputImage); Emgu.CV.Image <Bgr, byte> inpImg = new Emgu.CV.Image <Bgr, byte>(inputImage); Emgu.CV.Image <Gray, byte> water = inpImg.InRange(new Bgr(0, 100, 0), new Bgr(255, 255, 255)); //TODO: To check does we need mask? //water = water.Add(mask); //water._Dilate(1); // Create the blobs. Emgu.CV.Cvb.CvBlobs blobs = new Emgu.CV.Cvb.CvBlobs(); // Create blob detector. Emgu.CV.Cvb.CvBlobDetector dtk = new Emgu.CV.Cvb.CvBlobDetector(); // Detect blobs. uint state = dtk.Detect(water, blobs); foreach (Emgu.CV.Cvb.CvBlob blob in blobs.Values) { //Console.WriteLine("Center: X:{0:F3} Y:{1:F3}", blob.Centroid.X, blob.Centroid.Y); //Console.WriteLine("{0}", blob.Area); if (blob.Area >= 4500 && blob.Area < 34465) { //Console.WriteLine("{0}", blob.Area); inpImg.Draw(new CircleF(blob.Centroid, 5), new Bgr(Color.Red), 2); inpImg.Draw(blob.BoundingBox, new Bgr(Color.Blue), 2); } } if (this.outputImage != null) { this.outputImage.Dispose(); } // Dump the image. this.outputImage = inpImg.ToBitmap(); // Show the nwe mage. this.pbMain.Image = this.FitImage(this.outputImage, this.pbMain.Size); }); workerThread.Start(); }
public void bilinearfillHoles(Form1 form1) { Emgu.CV.Image <Gray, ushort> filledsurface = sc.image; for (int i = 1; i < sc.image.Height - 1; i++) { form1.progressBar1.Value = i / filledsurface.Height; for (int j = 1; j < sc.image.Width - 1; j++) { if (sc.image.Data[i, j, 0] == 0) { if (sc.image.Data[i + 1, j + 1, 0] != 0 && sc.image.Data[i - 1, j - 1, 0] != 0 && sc.image.Data[i + 1, j - 1, 0] != 0 && sc.image.Data[i - 1, j + 1, 0] != 0) { ushort a11 = sc.image.Data[i - 1, j - 1, 0]; ushort a12 = sc.image.Data[i + 1, j - 1, 0]; ushort a21 = sc.image.Data[i - 1, j + 1, 0]; ushort a22 = sc.image.Data[i + 1, j + 1, 0]; float r1 = a11 / 2 + a12 / 2; float r2 = a21 / 2 + a22 / 2; float p = r1 / 2 + r2 / 2; filledsurface.Data[i, j, 0] = (ushort)p; } } } for (int j = 1; j < sc.image.Width - 1; j++) { form1.progressBar1.Value = i / filledsurface.Height; if (sc.image.Data[i, j, 0] == 0) { if (sc.image.Data[i + 1, j, 0] != 0 && sc.image.Data[i - 1, j, 0] != 0 && sc.image.Data[i, j - 1, 0] != 0 && sc.image.Data[i, j + 1, 0] != 0) { ushort a11 = sc.image.Data[i - 1, j, 0]; ushort a12 = sc.image.Data[i, j + 1, 0]; ushort a21 = sc.image.Data[i, j - 1, 0]; ushort a22 = sc.image.Data[i + 1, j, 0]; float r1 = a11 / 2 + a12 / 2; float r2 = a21 / 2 + a22 / 2; float p = r1 / 2 + r2 / 2; filledsurface.Data[i, j, 0] = (ushort)p; } } } } form1.progressBar1.Value = 100; filledsurface.Save(form1.SavePath + "\\" + "surface_bilin_filled.png"); filledsurface.Save(form1.SavePath + "\\" + "surface.png"); form1.pictureBox1.Image = filledsurface.ToBitmap(); sc.image = filledsurface; }
//wyrownywanie //rozciaganie public static Bitmap Equalization(Bitmap image) { // Convert a BGR image to HLS range Emgu.CV.Image <Hls, Byte> imageHsi = new Image <Hls, Byte>(image); // Equalize the Intensity Channel Image <Gray, Byte> imageL = imageHsi[1]; imageL._EqualizeHist(); imageHsi[1] = imageL; // Convert the image back to BGR range Emgu.CV.Image <Bgr, Byte> imageBgr = imageHsi.Convert <Bgr, Byte>(); return(imageBgr.ToBitmap()); }
public static Mat RGBtoHSV(Mat img) { Mat tmp = new Mat(); try { Emgu.CV.Image <Bgr, Byte> image = img.ToImage <Bgr, Byte>(); Bitmap b = image.ToBitmap(); Emgu.CV.Image <Hsv, Byte> imgHsv = new Image <Hsv, Byte>(b); tmp = imgHsv.Mat; } catch (Exception ex) { MessageBox.Show(ex.Message); } return(tmp); }
static void Main() { // download sample photo WebClient client = new WebClient(); Bitmap image = null; using (MemoryStream ms = new MemoryStream(client.DownloadData(SAMPLE_IMAGE))) image = new Bitmap(Image.FromStream(ms)); // convert to Emgu image, convert to grayscale and increase brightness/contrast Emgu.CV.Image <Bgr, byte> emguImage = new Emgu.CV.Image <Bgr, byte>(image); var grayScaleImage = emguImage.Convert <Gray, byte>(); grayScaleImage._EqualizeHist(); // load eye classifier data string eye_classifier_local_xml = @"c:\temp\haarcascade_eye.xml"; client.DownloadFile(@EYE_DETECTION_XML, eye_classifier_local_xml); CascadeClassifier eyeClassifier = new CascadeClassifier(eye_classifier_local_xml); // perform detection which will return rectangles of eye positions var eyes = eyeClassifier.DetectMultiScale(grayScaleImage, 1.1, 4); // draw those rectangles on original image foreach (Rectangle eye in eyes) { emguImage.Draw(eye, new Bgr(255, 0, 0), 3); } // save image and show it string output_image_location = @"c:\temp\output.png"; emguImage.ToBitmap().Save(output_image_location, ImageFormat.Png); Process.Start(output_image_location); }
/// <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); }
private bool elaboraFoto(byte[] img) { try { Emgu.CV.Image <Bgr, byte> image; using (var byteStream = new MemoryStream(img)) { image = new Emgu.CV.Image <Bgr, byte>(new Bitmap(byteStream)); } long detectionTime; List <System.Drawing.Rectangle> faces = new List <System.Drawing.Rectangle>(); List <System.Drawing.Rectangle> eyes = new List <System.Drawing.Rectangle>(); //The cuda cascade classifier doesn't seem to be able to load "haarcascade_frontalface_default.xml" file in this release //disabling CUDA module for now bool tryUseCuda = false; /*Per vedere se sono state rilevate facce o occhi eventualmente possiamo mettere * un contatore che ci dice quante facce o occhi ci sono */ bool face_detect = false; bool eye_detect = false; DetectFace.Detect( image.Mat, "haarcascade_frontalface_default.xml", "haarcascade_eye.xml", faces, eyes, tryUseCuda, out detectionTime); foreach (System.Drawing.Rectangle face in faces) { CvInvoke.Rectangle(image, face, new Bgr(System.Drawing.Color.Red).MCvScalar, 2); face_detect = true; } foreach (System.Drawing.Rectangle eye in eyes) { CvInvoke.Rectangle(image, eye, new Bgr(System.Drawing.Color.Blue).MCvScalar, 2); eye_detect = true; } image.Save("Result.bmp"); Bitmap result = image.ToBitmap(); byte[] xByte = ToByteArray(result, ImageFormat.Bmp); int id; //qui salva la foto solo se è stata rilevata una faccia o degli occhi if (face_detect || eye_detect) { inserisciFoto(xByte, true); id = recuperaUltimoId(); recuperaFoto(id); return(true); } else { inserisciFoto(xByte, false); id = recuperaUltimoId(); recuperaFoto(id); return(false); } } catch (Exception e) { Debug.Print("Exception: " + e); return(false); } }
// //////////////////////////////////////////////////////////////////////////////////////////////////// private void button3_Click(object sender, EventArgs e) { try { OpenFileDialog of = new OpenFileDialog(); of.Filter = "Image File(*.bmp,*.jpg,*.png,*.gif,*.tif)|*.bmp;*.jpg;*.png;*.gif;*.tif"; DialogResult DR = of.ShowDialog(); if (DR == DialogResult.Cancel) { return; } Emgu.CV.Image <Bgr, Byte> Origin_IMG = new Emgu.CV.Image <Bgr, byte>(of.FileName); int wid = Origin_IMG.Width, hei = Origin_IMG.Height; Emgu.CV.Image <Gray, Byte> Gray_IMG = Origin_IMG.Convert <Gray, Byte>(); Emgu.CV.Image <Gray, Byte> BW_IMG = new Emgu.CV.Image <Gray, Byte>(wid, hei); CvInvoke.cvThreshold(Gray_IMG, BW_IMG, 0, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_OTSU); pictureBox1.Image = Origin_IMG.ToBitmap(); bool flag = false; int start_pos = 0, end_pos = 0; //======================== set the start and end point by whether [0,17] point is black or white color=============================== if (BW_IMG.Data[0, 20, 0] == 0) { flag = true; start_pos = 0; end_pos = hei - 1; } else { start_pos = hei - 1; end_pos = 0; } //============ find the start point for boundary line seaching in 152 colume=========================================== int[] bound_pos = new int[wid]; int[] s_point = new int[wid]; int m_s_value = -1; if (flag) { for (int i = start_pos; i <= end_pos; i++) { if (m_s_value == -1) { m_s_value = BW_IMG.Data[i, 152, 0]; } else if (m_s_value != BW_IMG.Data[i, 152, 0]) { s_point[152] = i; break; } } } else { for (int i = start_pos; i >= end_pos; i--) { if (m_s_value == -1) { m_s_value = BW_IMG.Data[i, 152, 0]; } else if (m_s_value != BW_IMG.Data[i, 152, 0]) { s_point[152] = i; break; } } } for (int j = 0; j < wid; j++) { if (s_point[j] < 0) { s_point[j] = 0; } else if (s_point[j] >= hei) { s_point[j] = hei - 1; } } //******************* search the boundary line from start point *********************************** for (int j = 151; j >= 17; j--) { int top_bound_p = -1, end_pp = -1; if (s_point[j + 1] - 1 <= 0) { end_pp = 0; } else { end_pp = s_point[j + 1] - 2; } for (int i = s_point[j + 1]; i >= end_pp; i--) { if (i - 1 <= 0 || i >= hei) { break; } if (BW_IMG.Data[i, j, 0] == 255 && BW_IMG.Data[i - 1, j, 0] == 0) { top_bound_p = i; break; } else if (BW_IMG.Data[i, j, 0] == 0 && BW_IMG.Data[i - 1, j, 0] == 255) { top_bound_p = i - 1; break; } } //========================================================================= int bottom_bound_p = -1; if (s_point[j + 1] + 1 >= hei) { end_pp = hei; } else { end_pp = s_point[j + 1] + 2; } for (int i = s_point[j + 1]; i <= end_pp; i++) { if (i + 1 >= hei || i < 0) { break; } if (BW_IMG.Data[i, j, 0] == 255 && BW_IMG.Data[i + 1, j, 0] == 0) { bottom_bound_p = i; break; } else if (BW_IMG.Data[i, j, 0] == 0 && BW_IMG.Data[i + 1, j, 0] == 255) { bottom_bound_p = i + 1; break; } } //========================================================================== if (top_bound_p != -1 && top_bound_p == bottom_bound_p) { s_point[j] = top_bound_p; continue; } if (top_bound_p == -1 && top_bound_p == bottom_bound_p) { s_point[j] = s_point[j + 1]; continue; } if (bottom_bound_p - s_point[j + 1] == 1 && bottom_bound_p - top_bound_p == 2 && !flag) { s_point[j] = bottom_bound_p; continue; } else if (bottom_bound_p - s_point[j + 1] == 1 && bottom_bound_p - top_bound_p == 2 && flag) { s_point[j] = top_bound_p; continue; } if (s_point[j + 1] - top_bound_p == bottom_bound_p - s_point[j + 1] && !flag) { s_point[j] = bottom_bound_p; continue; } else if (s_point[j + 1] - top_bound_p == bottom_bound_p - s_point[j + 1] && flag) { s_point[j] = top_bound_p; continue; } //=============================================================================== if (top_bound_p == -1 && bottom_bound_p != -1) { s_point[j] = bottom_bound_p; } else if (top_bound_p != -1 && bottom_bound_p == -1) { s_point[j] = top_bound_p; } else if (s_point[j + 1] - top_bound_p >= bottom_bound_p - s_point[j + 1]) { s_point[j] = bottom_bound_p; } else if (s_point[j + 1] - top_bound_p < bottom_bound_p - s_point[j + 1]) { s_point[j] = top_bound_p; } } //************************************************************************ Emgu.CV.Image <Gray, Byte> plate_IMG = new Emgu.CV.Image <Gray, Byte>(wid, hei); Emgu.CV.Image <Gray, Byte> Letter_IMG = new Emgu.CV.Image <Gray, Byte>(wid, hei); for (int j = 17; j <= 152; j++) { int top_p = 0; int botoom_p = 0; if (s_point[j] > start_pos) { top_p = start_pos; botoom_p = s_point[j]; } else { top_p = s_point[j] + 1; botoom_p = start_pos + 1; } for (int i = top_p; i < botoom_p; i++) { plate_IMG.Data[i, j, 0] = 255; } } //========================================================================= for (int i = 0; i < hei; i++) { for (int j = 17; j <= 152; j++) { if (plate_IMG.Data[i, j, 0] == 0 && BW_IMG.Data[i, j, 0] == 0 || plate_IMG.Data[i, j, 0] == 255 && BW_IMG.Data[i, j, 0] == 255) { Letter_IMG.Data[i, j, 0] = 255; } } for (int j = 0; j <= 17; j++) { if (BW_IMG.Data[i, j, 0] == 255) { Letter_IMG.Data[i, j, 0] = 255; } if (BW_IMG.Data[i, 16, 0] == 0) { Letter_IMG.Data[i, 17, 0] = 0; } } } letterA.Image = Letter_IMG.ToBitmap(); //******************************************************************************* Letter_splite(Letter_IMG); //pictureBox1.Image = Letter_IMG.ToBitmap(); letterA.Image = BW_IMG.ToBitmap(); Application.DoEvents(); Emgu.CV.Image <Gray, Byte> Letter_IMG1 = new Emgu.CV.Image <Gray, Byte>(wid, hei); if (Letter_imgs.Count <= 3) { int[] top_bound = new int[BW_IMG.Width]; int[] bottom_bound = new int[BW_IMG.Width]; int[] equal_bound = new int[BW_IMG.Width]; if (flag) { top_bound = get_top_bound(BW_IMG, start_pos, end_pos); bottom_bound = get_bottom_bound(BW_IMG, start_pos, end_pos); } else { top_bound = get_top_bound1(BW_IMG, end_pos, start_pos); bottom_bound = get_bottom_bound1(BW_IMG, end_pos, start_pos); } //------------------------------------------------------------------ int origin_p = 152; double h = 0; int s_p = 152; for (int j = 152; j >= 17; j--) { if (j == 18) { if (Math.Abs(equal_bound[s_p] - top_bound[17]) >= Math.Abs(equal_bound[s_p] - bottom_bound[17])) { top_bound[17] = bottom_bound[17]; } else { bottom_bound[17] = top_bound[17]; } } if (j == 17) { int middle_point = 17; if (!flag) { int n = 0; for (int k1 = 17; k1 < s_p; k1++) { if (top_bound[k1] >= equal_bound[17] && top_bound[k1] <= equal_bound[s_p]) { n++; } } if (n == s_p - 17) { middle_point = Convert.ToInt16((s_p + 17) / 2); h = Convert.ToDouble(equal_bound[s_p] - top_bound[middle_point]) / (s_p - middle_point); origin_p = middle_point; for (int k = s_p - middle_point; k >= 0; k--) { equal_bound[s_p - k] = Convert.ToInt16(equal_bound[s_p] - h * k); } s_p = middle_point; } for (int k1 = 0; k1 < hei; k1++) { if (BW_IMG.Data[k1, 17, 0] == 255) { top_bound[17] = k1; } else if (BW_IMG.Data[k1, 17, 0] == 0 && BW_IMG.Data[k1, 16, 0] == 255) { top_bound[17] = k1; } else if (BW_IMG.Data[k1, 17, 0] == 0 && BW_IMG.Data[k1, 16, 0] == 0 && (BW_IMG.Data[k1 + 1, 16, 0] == 255 || BW_IMG.Data[k1 + 1, 17, 0] == 255)) { top_bound[17] = k1; } else { break; } } } else { for (int k1 = hei - 2; k1 >= 0; k1--) { if (BW_IMG.Data[k1, 17, 0] == 255) { top_bound[17] = k1; } else if (BW_IMG.Data[k1, 17, 0] == 0 && BW_IMG.Data[k1, 16, 0] == 255) { top_bound[17] = k1; } else if (BW_IMG.Data[k1, 17, 0] == 0 && BW_IMG.Data[k1, 16, 0] == 0 && (BW_IMG.Data[k1 - 1, 16, 0] == 255 || BW_IMG.Data[k1 - 1, 17, 0] == 255)) { top_bound[17] = k1; } else { break; } } } h = Convert.ToDouble(equal_bound[s_p] - top_bound[j]) / (s_p - j); origin_p = j; for (int k = s_p - j; k >= 0; k--) { equal_bound[s_p - k] = Convert.ToInt16(equal_bound[s_p] - h * k); } continue; } //****************************************************************************************** if (top_bound[j] == bottom_bound[j] && origin_p - j <= 1) { if (Math.Abs(top_bound[s_p] - top_bound[j]) > 5) { if (Math.Abs(equal_bound[s_p] - top_bound[j]) >= Math.Abs(equal_bound[s_p] - bottom_bound[j])) { equal_bound[j] = bottom_bound[j]; } else { equal_bound[j] = top_bound[j]; } continue; } equal_bound[j] = top_bound[j]; s_p = j; origin_p = j; } else if (top_bound[j] == bottom_bound[j] && origin_p - j > 15) { if (Math.Abs(s_p - j) <= 17) { continue; } h = Convert.ToDouble(equal_bound[s_p] - top_bound[j]) / (s_p - j); for (int k = s_p - j; k >= 0; k--) { equal_bound[s_p - k] = Convert.ToInt16(equal_bound[s_p] - h * k); } s_p = j; origin_p = j; } } //-------------------------------------------------------------------------------------------------- Emgu.CV.Image <Gray, Byte> plate_IMG1 = new Emgu.CV.Image <Gray, Byte>(wid, hei); Letter_IMG1 = new Emgu.CV.Image <Gray, Byte>(wid, hei); for (int i = 17; i <= 152; i++) { if (flag) { for (int j = 0; j <= equal_bound[i] - 1; j++) { plate_IMG1.Data[j, i, 0] = 255; } } else { for (int j = equal_bound[i] + 1; j < hei; j++) { plate_IMG1.Data[j, i, 0] = 255; } } } //========================================================================= for (int i = 0; i < hei; i++) { for (int j = 17; j <= 152; j++) { if (plate_IMG1.Data[i, j, 0] == 0 && BW_IMG.Data[i, j, 0] == 0 || plate_IMG1.Data[i, j, 0] == 255 && BW_IMG.Data[i, j, 0] == 255) { Letter_IMG1.Data[i, j, 0] = 255; } } for (int j = 0; j <= 17; j++) { if (BW_IMG.Data[i, j, 0] == 255) { Letter_IMG1.Data[i, j, 0] = 255; } if (BW_IMG.Data[i, 16, 0] == 0) { Letter_IMG1.Data[i, 17, 0] = 0; } } } Letter_splite(Letter_IMG1); } //******************************** to split the letter into 2 letters******************************************************* if (Letter_imgs.Count == 4) { int tmp_wid = 0, img_index = 0; for (int i = 0; i < Letter_imgs.Count; i++) { if (Letter_imgs[i].Width > tmp_wid) { tmp_wid = Letter_imgs[i].Width; img_index = i; } } Image <Gray, Byte> split_img1; Image <Gray, Byte> split_img2; if (tmp_wid < 30) { if (img_index == 0 && Letter_imgs[1].Width == Letter_imgs[0].Width) { img_index = 1; } split_img1 = Letter_imgs[img_index].Copy(new Rectangle(0, 0, Convert.ToInt32(tmp_wid / 2 + 5), Letter_imgs[img_index].Height)); split_img2 = Letter_imgs[img_index].Copy(new Rectangle(Convert.ToInt32(tmp_wid / 2) + 5, 0, tmp_wid - Convert.ToInt32(tmp_wid / 2) - 5, Letter_imgs[img_index].Height)); } else { split_img1 = Letter_imgs[img_index].Copy(new Rectangle(0, 0, Convert.ToInt32(tmp_wid / 2) + 1, Letter_imgs[img_index].Height)); split_img2 = Letter_imgs[img_index].Copy(new Rectangle(Convert.ToInt32(tmp_wid / 2) + 2, 0, tmp_wid - Convert.ToInt32(tmp_wid / 2) - 2, Letter_imgs[img_index].Height)); } int[] v_hist = get_VertHist1(split_img1, 0, split_img1.Width, split_img1.Height); int[] h_hist = get_horiHist1(split_img1); split_img1 = split_img1.Copy(new Rectangle(h_hist[0], v_hist[0], h_hist[1] - h_hist[0], v_hist[1] - v_hist[0])); v_hist = get_VertHist1(split_img2, 0, split_img2.Width, split_img2.Height); h_hist = get_horiHist1(split_img2); split_img2 = split_img2.Copy(new Rectangle(h_hist[0], v_hist[0], h_hist[1] - h_hist[0], v_hist[1] - v_hist[0])); Letter_imgs[img_index] = bwareaopen(split_img1, 5); Letter_imgs.Insert(img_index + 1, bwareaopen(split_img2, 5)); for (int i = 0; i < Letter_imgs[img_index].Height; i++) { Letter_IMG1.Data[i, Convert.ToInt32(tmp_wid / 2), 0] = 0; } } //******************************** to display the letters******************************************************* if (Letter_imgs.Count >= 5) { letterA.Image = Letter_imgs[0].ToBitmap(); letterB.Image = Letter_imgs[1].ToBitmap(); letterC.Image = Letter_imgs[2].ToBitmap(); letterD.Image = Letter_imgs[3].ToBitmap(); letterE.Image = Letter_imgs[4].ToBitmap(); } else { letterA.Image = null; letterB.Image = null; letterC.Image = null; letterD.Image = null; letterE.Image = null; textBox1.Text = "No recognition"; return; } //*************** to detect the letter by using machine learning ******************************************************************************* Matrix <float> testPattern; testPattern = new Matrix <float>(1, 400); string detectedLetters = ""; for (int i = 0; i < Letter_imgs.Count; i++) { Image <Gray, Byte> resize_IMG = Letter_imgs[i].Resize(20, 20, Emgu.CV.CvEnum.INTER.CV_INTER_LINEAR); int pixel_count = -1; for (int i1 = 0; i1 < resize_IMG.Width; i1++) { for (int j1 = 0; j1 < resize_IMG.Height; j1++) { pixel_count++; testPattern[0, pixel_count] = Convert.ToSingle(resize_IMG.Data[i1, j1, 0]); } } //======================== to make the k-nearst model and train it by train data======================================================== int K = response.Rows; Matrix <float> neighborResponses = new Matrix <float>(testPattern.Rows, K); Matrix <float> results = new Matrix <float>(testPattern.Rows, 1); Emgu.CV.ML.KNearest model = new Emgu.CV.ML.KNearest(); model.Train(LettersPattern, response, null, false, K, false); float response1 = model.FindNearest(testPattern, K, results, null, neighborResponses, null); detectedLetters += res_array[Convert.ToInt32(neighborResponses[0, 0])]; } textBox1.Text = detectedLetters; } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
/// <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); }
private void timImage_Tick(object sender, EventArgs e) { Emgu.CV.Image <Bgr, Byte> photo = imgCapture.QueryFrame(); picProfile.Image = photo.ToBitmap(); }
public void fillHoles(Form1 form1) { Emgu.CV.Image <Gray, ushort> filledsurface = sc.image; for (int i = 3; i < filledsurface.Height - 4; i++) { form1.progressBar1.Value = i / filledsurface.Height; for (int j = 3; j < filledsurface.Width - 4; j++) { bool frame = true; uint sumframe = 0; uint suminner = 0; int count = 0; #region frame check for (int k = -3; k <= 3; k++) { if (filledsurface.Data[i + k, j - 3, 0] != 0) { sumframe += filledsurface.Data[i + k, j - 3, 0]; } else { frame = false; } if (filledsurface.Data[i + k, j + 3, 0] != 0 && frame == true) { sumframe += filledsurface.Data[i + k, j + 3, 0]; } else { frame = false; } } for (int k = -2; k <= 2; k++) { if (filledsurface.Data[i - 3, j + k, 0] != 0 && frame == true) { sumframe += filledsurface.Data[i - 3, j + k, 0]; } else { frame = false; } if (filledsurface.Data[i + 3, j + k, 0] != 0 && frame == true) { sumframe += filledsurface.Data[i + 3, j + k, 0]; } else { frame = false; } } #endregion if (frame == false) { continue; } for (int k = -2; k <= 2; k++) { for (int l = -2; l <= 2; l++) { if (filledsurface.Data[i + k, j + l, 0] != 0) { suminner += filledsurface.Data[i + k, j + l, 0]; count++; } } } if (count == 0 || count == 25) { continue; } if (Math.Abs(suminner / count - sumframe / 24) < 100) { for (int k = -2; k <= 2; k++) { for (int l = -2; l <= 2; l++) { if (filledsurface.Data[i + k, j + l, 0] == 0) { filledsurface.Data[i + k, j + l, 0] = (ushort)(suminner / count); } } } } } } form1.progressBar1.Value = 100; filledsurface.Save(form1.SavePath + "\\" + "surface_filled.png"); filledsurface.Save(form1.SavePath + "\\" + "surface.png"); form1.pictureBox1.Image = filledsurface.ToBitmap(); sc.image = filledsurface; }