private void button1_Click(object sender, EventArgs e) { //pierwszy czarnobialy obrazek String photoString = ""; //String photoPath = @"C:\Users\Michał\Downloads\" + photoString + ".jpg"; String photoPath = null; ///margines aby wykluczyc pozostale znalezione punkty float selectionMargin = 0.25f; List<Point> points = new List<Point>(); int minArea = 1000; float maxSize = 0f; if (openFileDialog1.ShowDialog() == DialogResult.OK) { photoPath = openFileDialog1.InitialDirectory + openFileDialog1.FileName; } Bitmap photo = new Bitmap(photoPath); using (Image<Gray, Byte> img = new Image<Gray, byte>(photo)) { MSERDetector mserDetector = new MSERDetector(5, 1440000, minArea, 0.25f, 0.2f, 200, 1.01, 0.00003, 10); Image<Gray, Byte> img2 = img.SmoothBlur(30, 30, true); MKeyPoint[] keyPoints = mserDetector.DetectKeyPoints(img, null); foreach (MKeyPoint p in keyPoints) { if (((p.Point.X / img.Width > selectionMargin) && (p.Point.X / img.Width < 1 - selectionMargin)) && ((p.Point.Y / img.Height > selectionMargin) && (p.Point.Y / img.Height < 1 - selectionMargin))) { points.Add(new Point((int)(p.Point.X), (int)(p.Point.Y))); if (p.Size > maxSize) maxSize = p.Size; } } float sumX = 0f, sumY = 0f; foreach (Point p in points) { sumX += p.X; sumY += p.Y; } img.Draw(new CircleF(new PointF(sumX / points.Count, sumY / points.Count), 8), new Gray(), 10); //img.DrawPolyline(points.ToArray(), true, new Gray(), 4); textBox1.Clear(); textBox1.AppendText((sumX / points.Count).ToString() + ":" + (sumY / points.Count).ToString() + ":" + (maxSize/2).ToString()); //imgWhite.Draw(points, new Gray(0), 10); //Show the image using ImageViewer from Emgu.CV.UI pictureBox2.Image = img.ToBitmap(); img.Save(@"C:\Users\Michał\Downloads\nowy.jpg"); } }
private extern static void CvMSERKeyPoints( IntPtr image, IntPtr mask, IntPtr keypoints, ref MSERDetector mser);
/// <summary> /// Extracts the contours of Maximally Stable Extremal Regions /// </summary> /// <param name="image">The image where mser will be extracted from</param> /// <param name="mask">Can be null if not needed. Optional parameter for the region of interest</param> /// <param name="param">MSER parameter</param> /// <param name="storage">The storage where the contour will be saved</param> /// <returns>The MSER regions</returns> public Seq<Point>[] ExtractContours(IImage image, Image<Gray, Byte> mask, ref MSERDetector param, MemStorage storage) { IntPtr mserPtr = new IntPtr(); CvInvoke.cvExtractMSER(image.Ptr, mask, ref mserPtr, storage, param); IntPtr[] mserSeq = new Seq<IntPtr>(mserPtr, storage).ToArray(); return Array.ConvertAll<IntPtr, Seq<Point>>(mserSeq, delegate(IntPtr ptr) { return new Seq<Point>(ptr, storage); }); }
public static extern void cvExtractMSER( IntPtr img, IntPtr mask, ref IntPtr contours, IntPtr storage, MSERDetector parameters);
public void Process() { ///margines aby wykluczyc pozostale znalezione punkty float selectionMargin = 0.25f; List<Point> points = new List<Point>(); int minArea = 1000; float maxSize = 0f; float resultX = 0f; float resultY = 0f; float resultR = 0f; float sumX = 0f, sumY = 0f; foreach (Bitmap bitmap in bitmaps) { points.Clear(); resultX = 0f; resultY = 0f; resultR = 0f; sumX = 0f; sumY = 0f; using (Image<Gray, Byte> img = new Image<Gray, byte>(bitmap)) { MSERDetector mserDetector = new MSERDetector(5, 1440000, minArea, 0.25f, 0.2f, 200, 1.01, 0.00003, 10); Image<Gray, Byte> img2 = img.SmoothBlur(30, 30, true); MKeyPoint[] keyPoints = mserDetector.DetectKeyPoints(img, null); foreach (MKeyPoint p in keyPoints) { if (((p.Point.X / img.Width > selectionMargin) && (p.Point.X / img.Width < 1 - selectionMargin)) && ((p.Point.Y / img.Height > selectionMargin) && (p.Point.Y / img.Height < 1 - selectionMargin))) { points.Add(new Point((int)(p.Point.X), (int)(p.Point.Y))); if (p.Size > maxSize) maxSize = p.Size; } } foreach (Point p in points) { sumX += p.X; sumY += p.Y; } resultX = sumX / points.Count; resultY = sumY / points.Count; resultR = maxSize / 2; results.Add(new Result(resultX, resultY, resultR)); } } }
public void TestMSER() { MSERDetector keyPointDetector = new MSERDetector(); SIFT descriptorGenerator = new SIFT(); //ParamDef[] parameters = keyPointDetector.GetParams(); TestFeature2DTracker(keyPointDetector, descriptorGenerator); }
/// <summary> /// Extracts the contours of Maximally Stable Extremal Regions /// </summary> /// <param name="image">The image where mser will be extracted from</param> /// <param name="mask">Can be null if not needed. Optional parameter for the region of interest</param> /// <param name="param">MSER parameter</param> /// <param name="storage">The storage where the contour will be saved</param> /// <returns>The MSER regions</returns> public Seq <Point>[] ExtractContours(IImage image, Image <Gray, Byte> mask, ref MSERDetector param, MemStorage storage) { IntPtr mserPtr = new IntPtr(); CvInvoke.cvExtractMSER(image.Ptr, mask, ref mserPtr, storage, param); IntPtr[] mserSeq = new Seq <IntPtr>(mserPtr, storage).ToArray(); return(Array.ConvertAll <IntPtr, Seq <Point> >(mserSeq, delegate(IntPtr ptr) { return new Seq <Point>(ptr, storage); })); }