internal void Process(int hueAfter, byte satAfter, int hueMid, int hueWidth) { byte afterSat = 0; BeforeImage = new Image<Bgr, byte>(BeforeImagePath).Resize(440, 320, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC, false); DebugImage = BeforeImage.Convert<Hsv, byte>(); hueMid = FindHuePeak(); int hueStart = (180 + hueMid - (hueWidth / 2)) % 180; int hueEnd = (180 + hueMid + (hueWidth / 2)) % 180; for (int i = 0; i < DebugImage.Width; i++) for (int j = 0; j < DebugImage.Height; j++) { int hue = DebugImage.Data[j, i, 0]; int sat = DebugImage.Data[j, i, 1]; int val = DebugImage.Data[j, i, 2]; if ((hueStart < hueEnd) && (hue < hueEnd && hue > hueStart) || (hueStart > hueEnd) && (hue < hueEnd || hue > hueStart)) { if (sat > 30) { DebugImage.Data[j, i, 0] =(byte) hueAfter; //DebugImage.Data[j, i, 1] = satAfter; } } } AfterImage = DebugImage.Convert<Bgr, byte>(); }
public static Bitmap GetBitmap(Emgu.CV.Image <Bgr, byte> image) { Emgu.CV.Image <Gray, byte> gray = image.Convert <Gray, byte>(); int [] k = new int[gray.Width]; for (int i = 0; i < gray.Width; i++) { for (int j = 0; j < gray.Height; j++) { if (gray[j, i].Intensity <= 200) { k[i]++; } } } Bitmap image2 = new Bitmap(gray.Width, gray.Height); System.Drawing.Graphics p = System.Drawing.Graphics.FromImage(image2); p.Clear(Color.Black); for (int i = 0; i < k.Length - 3; i++) { PointF p1 = new PointF(i, k[i]); PointF p2 = new PointF(i + 1, k[i + 1]); PointF p3 = new PointF(i + 2, k[i + 2]); PointF p4 = new PointF(i + 3, k[i + 3]); p.DrawBezier(new Pen(Brushes.White), p1, p2, p3, p4); } return(image2); }
public Bitmap Findcircles(double cannyThreshold, double circleAccumulatorThreshold) { Bitmap bmp; CircleF[] circles = CvInvoke.HoughCircles(imgEdgesROI, HoughType.Gradient, 1.0, 5, cannyThreshold, circleAccumulatorThreshold); Emgu.CV.Image <Bgr, byte> imgWithCircles = img.Convert <Bgr, byte>(); foreach (CircleF circle in circles) { imgWithCircles.Draw(circle, new Bgr(0, 0, 255), 1, LineType.EightConnected); } bmp = imgWithCircles.Bitmap; return(bmp); }
public static Image <Gray, Byte> RGBtoGrey(Mat img) { Image <Gray, Byte> grayImage = new Image <Gray, byte>(new Size(0, 0)); try { Emgu.CV.Image <Bgr, Byte> image = img.ToImage <Bgr, Byte>(); grayImage = image.Convert <Gray, Byte>(); } catch (Exception ex) { MessageBox.Show(ex.Message); } return(grayImage); }
private void PrepareSource() { // Blit together the processed image onto the starting image // Or any other combinations of options yet to be thought of if (m_bShowOver) { BitmapSource bmpRaw = BitmapSourceConvert.ToBitmapSource(m_RawImage); if (m_ResultImage != null) { Image <Rgb, byte> raw = m_RawImage.Convert <Rgb, byte>(); raw = raw.Or(m_ResultImage); DispImage.Source = BitmapSourceConvert.ToBitmapSource(raw); } else { DispImage.Source = bmpRaw; } } else { DispImage.Source = BitmapSourceConvert.ToBitmapSource(m_RawImage); } }
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); }
private void tmrRefrashFrame_Tick(object sender, EventArgs e) { try { this.tmrRefrashFrame.Stop(); ImageSize(); SendMessage(m_CapHwnd, WM_CAP_GT_FRAME, 0, 0); SendMessage(m_CapHwnd, WM_CAP_COPY, 0, 0); OpenClipboard(m_CapHwnd); IntPtr img = GetClipboardData(2); CloseClipboard(); //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(m_Width, m_Height); //using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp)) //{ // g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low; // g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighSpeed; // g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; // g.DrawImage(Image.FromHbitmap(img), 0, 0, m_Width, m_Height); //} //ImgWebCam.Image = bmp; IDataObject tempObj = Clipboard.GetDataObject(); Image tempImg = (System.Drawing.Bitmap)tempObj.GetData(DataFormats.Bitmap); ImgWebCam.Image = tempImg; using (Emgu.CV.Image <Bgr, byte> orignalFrame = new Image <Bgr, byte>((Bitmap)ImgWebCam.Image)) { //convert image into gray Image <Bgr, byte> image = orignalFrame.Resize(ImgWebCam.Size.Width, ImgWebCam.Size.Height, 0); Image <Gray, byte> frame = image.Convert <Gray, byte>(); //Perform absdiff only if it's the second frame else store it in the global first frame if (this.previousFrame != null) { //convert the previous frame to gray Image <Gray, byte> previousFrameGray = previousFrame.Convert <Gray, byte>(); previousFrameGray = previousFrameGray.Resize(ImgWebCam.Size.Width, ImgWebCam.Size.Height, 0); Image <Gray, byte> BgDifference = new Image <Gray, byte>(ImgWebCam.Width, ImgWebCam.Height); CvInvoke.AbsDiff(previousFrameGray, frame, BgDifference); //assign the current frame to previousFrame this.previousFrame = new Image <Bgr, byte>((Bitmap)ImgWebCam.Image); ImgWebCam.Image = BgDifference.ToBitmap(); //ImgWebCam.Image = image.ToBitmap(); } else { this.previousFrame = new Image <Bgr, byte>((Bitmap)ImgWebCam.Image); this.previousFrame = this.previousFrame.Resize(ImgWebCam.Size.Width, ImgWebCam.Size.Height, 0); } } ImgWebCam.Refresh(); Application.DoEvents(); if (!bStopped) { this.tmrRefrashFrame.Start(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private Dictionary <int, KeyValuePair <decimal, decimal> > _FindContoursAndCalculate(Emgu.CV.Image <Bgr, Byte> colorImage, out Emgu.CV.Image <Gray, Byte> processedImage) { int circles = 0; VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); Mat hierarchy = new Mat(); /// Detect edges using Threshold Image <Gray, Byte> Img_Source_Gray = colorImage.Convert <Gray, Byte>(); Img_Source_Gray = Img_Source_Gray.SmoothBlur(3, 3); Image <Gray, Byte> threshold_output = Img_Source_Gray.ThresholdBinary(new Gray(90), new Gray(255)); /// Find contours CvInvoke.FindContours(threshold_output, contours, hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxSimple); /// Approximate contours to polygons + get bounding rects and circles VectorOfVectorOfPoint contours_poly = new VectorOfVectorOfPoint(contours.Size); Point[][] con = contours.ToArrayOfArray(); PointF[][] conf = new PointF[con.GetLength(0)][]; for (int i = 0; i < con.GetLength(0); i++) { conf[i] = new PointF[con[i].Length]; for (int j = 0; j < con[i].Length; j++) { conf[i][j] = new PointF(con[i][j].X, con[i][j].Y); } } for (int i = 0; i < contours.Size; i++) { double a = CvInvoke.ContourArea(contours[i], false); if (a > 30000) { CvInvoke.ApproxPolyDP(contours[i], contours_poly[circles], 3, true); circles++; } } /// Draw polygonal contour + bonding rects + circles Image <Gray, byte> filteredImage = new Image <Gray, byte>(Img_Source_Gray.Width, Img_Source_Gray.Height); for (int i = 0; i < circles; i++) { CvInvoke.DrawContours(filteredImage, contours_poly, i, new MCvScalar(255, 255, 255), -1); } filteredImage = colorImage.Convert <Gray, Byte>().And(filteredImage); Image <Gray, Byte> whiteAreas = new Image <Gray, byte>(filteredImage.Width, filteredImage.Height); List <DetectedCircle> detectedCircles = new List <DetectedCircle>(); for (int i = 0; i < circles; i++) { PointF[] pointfs = Array.ConvertAll(contours_poly[i].ToArray(), input => new PointF(input.X, input.Y)); Rectangle boundingRect = PointCollection.BoundingRectangle(pointfs); DetectedCircle detectedCircle = new DetectedCircle() { contour = contours_poly[i], boundingRect = boundingRect, center = new Point(boundingRect.X + (boundingRect.Width / 2), boundingRect.Y + (boundingRect.Height / 2)) }; detectedCircles.Add(detectedCircle); filteredImage.ROI = boundingRect; int threshold = _ComputeThreshold(filteredImage, detectedCircle); filteredImage.ROI = Rectangle.Empty; Image <Gray, Byte> mask = new Image <Gray, byte>(filteredImage.Width, filteredImage.Height); mask.Draw(boundingRect, new Gray(255), -1); mask = filteredImage.And(mask); /* Extract white are solely based on the Blue channel */ mask = mask.ThresholdBinary(new Gray(threshold), new Gray(255)); whiteAreas = whiteAreas.Or(mask.Convert <Gray, Byte>().ThresholdBinary(new Gray(10), new Gray(255))); CvInvoke.DrawContours(whiteAreas, contours_poly, i, new MCvScalar(255, 255, 255), 3); } detectedCircles = detectedCircles.OrderBy(c => c.center.X).ToList(); processedImage = whiteAreas; return(_ComputeMetrics(whiteAreas, detectedCircles, circles)); }
// //////////////////////////////////////////////////////////////////////////////////////////////////// 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()); } }
public void findChips() { try { //CudaImage<Bgr,Byte> gpuImg = new CudaImage<Bgr,Byte>(sourceImage); //VectorOfGpuMat outVec = new VectorOfGpuMat(); //CudaGaussianFilter gaussFilter = new CudaGaussianFilter(DepthType.new Size(11,11),0.8,0.8,BorderType.Constant,BorderType.Constant); //gaussFilter.Apply(gpuImg, gpuImg, null); diagnosticImage = sourceImage.Clone(); Rectangle[] rects = cc.DetectMultiScale(sourceImage.Convert <Gray, Byte>(), 1.1, 4, new System.Drawing.Size(10, 10)); //cc.ScaleFactor = 1.1; //cc.MinNeighbors = 4; //cc.MinObjectSize = new Size(10, 10); //cc.DetectMultiScale(gpuImg.Convert<Gray, Byte>(), outVec); //Rectangle[] rects = cc.Convert(outVec); foreach (Rectangle rect in rects) { StackRegistry.stackFound(rect); } List <Stack> processedStacks = StackRegistry.processFrame(sourceImage); if (foundChipColor != null) { if (processedStacks.Count == 1) { ColorFinder.trainChip(sourceImage, processedStacks[0].samplePoints, (Stack.Color)foundChipColor); } } foreach (Stack stack in processedStacks) { Bgr col = new Bgr(Color.Magenta); switch (stack.color) { case Stack.Color.Black: col = new Bgr(Color.Black); break; case Stack.Color.Blue: col = new Bgr(Color.Blue); break; case Stack.Color.Red: col = new Bgr(Color.Red); break; case Stack.Color.Green: col = new Bgr(Color.Green); break; case Stack.Color.White: col = new Bgr(Color.White); break; } diagnosticImage.Draw(stack.location, col, 2); foreach (Point sample in stack.samplePoints) { diagnosticImage.Draw(new Rectangle(sample, new Size(1, 1)), new Bgr(System.Drawing.Color.Yellow), 2); } } } catch (Exception ex) { MessageBox.Show(ex.StackTrace.ToString()); } }
private void imageBox1_Click(object sender, EventArgs e) { if (im1.Image == null) { return; } //--Get Mouseposition in Pixelcoordinates-------- var mouseEventArgs = e as MouseEventArgs; int imWidth, imHeight, boxWidth, boxHeight; imWidth = im1.Image.Size.Width; imHeight = im1.Image.Size.Height; boxWidth = im1.Size.Width; boxHeight = im1.Size.Height; Point mouse = TranslateZoomMousePosition(mouseEventArgs.X, mouseEventArgs.Y, imWidth, imHeight, boxWidth, boxHeight); int x = mouse.X; //(int)(mouseEventArgs.X / im1.ZoomScale); int y = mouse.Y; //(int)(mouseEventArgs.Y / im1.ZoomScale); Emgu.CV.Image <Hsv, byte> original = LoadedImages[listBox1.SelectedItem.ToString()]; Hsv pcolor = original[y, x]; if (mouseEventArgs != null) { label1.Text = "@(" + x + "; " + y + "): " + pcolor.ToString(); } Emgu.CV.Image <Gray, byte> threshedimage; threshedimage = thresholdHSVtoGray(original, pcolor); imageBox2.Image = threshedimage; shapes.Clear(); Emgu.CV.Image <Hsv, byte> refImg = (Emgu.CV.Image <Hsv, byte>)im1.Image; Emgu.CV.Image <Gray, byte> inImg = (Emgu.CV.Image <Gray, byte>)im2.Image; Emgu.CV.Image <Hsv, byte> outImg = inImg.Convert <Hsv, byte>(); shapes.AddRange(findShapesinGrayImg(inImg, refImg, listBox1.SelectedIndex)); //--Find closest shape--------- ShapeColorObject temp = null; int dist = 0; foreach (ShapeColorObject shp in shapes) { int d = (shp.pos.X - mouse.X) * (shp.pos.X - mouse.X) + (shp.pos.Y - mouse.Y) * (shp.pos.Y - mouse.Y); if (ShapeColorObject.compareHues(shp.getColor().Hue, pcolor.Hue, dHue)) { if (temp == null || d < dist) { temp = shp; temp.imIndex = listBox1.SelectedIndex; dist = d; } } } if (temp != null && !chosenshapes.Contains(temp)) { temp.objIndex = chosenshapes.Count; chosenshapes.Add(temp); //trackedshapes.Add(temp); } foreach (ShapeColorObject shp in chosenshapes) { shp.drawOnImg(ref outImg); im2.Image = outImg; im2.Update(); } label2.Text = ""; foreach (ShapeColorObject shape in chosenshapes) { label2.Text += shape.toString() + "\n"; } }