public void Apply() { string output_dir = @"C:\Users\yaoyichi\Desktop\"; string inputPath = output_dir + "1.png"; Log.SetLogDir(output_dir); Log.SetOutputDir(output_dir); HoughCircleTransformation circleTransform = new HoughCircleTransformation(35); // apply Hough circle transform Bitmap srcimg = new Bitmap(inputPath); AForge.Imaging.Filters.BradleyLocalThresholding bradley = new AForge.Imaging.Filters.BradleyLocalThresholding(); Image <Gray, Byte> img = new Image <Gray, Byte>(inputPath); Bitmap dstimg = bradley.Apply(img.Bitmap); circleTransform.ProcessImage(dstimg); Bitmap houghCirlceImage = circleTransform.ToBitmap(); // get circles using relative intensity HoughCircle[] circles = circleTransform.GetCirclesByRelativeIntensity(0.2); Graphics g = Graphics.FromImage(ImageUtils.AnyToFormat24bppRgb(srcimg)); foreach (HoughCircle circle in circles) { //circle.RelativeIntensity g.DrawEllipse(new Pen(Color.Red), circle.X, circle.Y, circle.Radius, circle.Radius); } g.Dispose(); srcimg.Save(output_dir + "1_.png"); }
private static void FindCircles(UnmanagedImage binarySource) { // apply Hough circle transform circleTransform.ProcessImage(binarySource); // get circles using relative intensity HoughCircle[] circles = circleTransform.GetCirclesByRelativeIntensity(0.5); foreach (HoughCircle circle in circles) { string s = string.Format("X = {0}, Y = {1}, I = {2} ({3})", circle.X, circle.Y, circle.Intensity, circle.RelativeIntensity); System.Diagnostics.Debug.WriteLine(s); } System.Diagnostics.Debug.WriteLine("Found circles: " + circleTransform.CirclesCount); System.Diagnostics.Debug.WriteLine("Max intensity: " + circleTransform.MaxIntensity); }
private void houghCircle(Bitmap basicSaus) { Bitmap saus = ConvertToFormat(basicSaus); HoughCircleTransformation circleTransform = new HoughCircleTransformation(35); // apply Hough circle transform circleTransform.ProcessImage(saus); Bitmap houghCirlceImage = circleTransform.ToBitmap(); // get circles using relative intensity HoughCircle[] circles = circleTransform.GetCirclesByRelativeIntensity(0.1); foreach (HoughCircle circle in circles) { Console.WriteLine(JsonConvert.SerializeObject(circle, Formatting.Indented)); } pictureBox2.Image = houghCirlceImage; }
public void HughCircleTransform() { Grayscale filterGrayscale = new Grayscale(0.2125, 0.7154, 0.0721); ImageBitmap8pp = filterGrayscale.Apply(ImageBitmap); Blur filterBlur = new Blur(); filterBlur.ApplyInPlace(ImageBitmap8pp); //ContrastStretch filterContrast = new ContrastStretch(); //filterContrast.ApplyInPlace(ImageBitmap8pp); ImageBitmap8pp.Save("test0.bmp"); CannyEdgeDetector filter = new CannyEdgeDetector(); filter.ApplyInPlace(ImageBitmap8pp); HoughCircleTransformation circleTransform = new HoughCircleTransformation(35); circleTransform.LocalPeakRadius = 300; circleTransform.ProcessImage(ImageBitmap8pp); Bitmap houghCirlceImage = circleTransform.ToBitmap(); HoughCircle[] circles = circleTransform.GetCirclesByRelativeIntensity(0.6); foreach (HoughCircle circle in circles) { Pen redPen = new Pen(System.Drawing.Color.Red, 3); using (var graphics = Graphics.FromImage(ImageBitmap)) { graphics.DrawEllipse(redPen, circle.X - circle.Radius, circle.Y - circle.Radius, circle.Radius * 2, circle.Radius * 2); } } ImageBitmap8pp.Save("test1.bmp"); houghCirlceImage.Save("test2.bmp"); ImageBitmap.Save("test3.bmp"); }
private void button4_Click(object sender, EventArgs e) { Bitmap sourceImage = (Bitmap)pictureBox1.Image; HoughCircleTransformation circleTransform = new HoughCircleTransformation(35); // apply Hough circle transform circleTransform.ProcessImage(ConvertToFormat(sourceImage, PixelFormat.Format8bppIndexed)); Bitmap houghCirlceImage = circleTransform.ToBitmap(); // get circles using relative intensity HoughCircle[] circles = circleTransform.GetCirclesByRelativeIntensity(0.5); Graphics g; MessageBox.Show("Jumlah Lingkaran: " + circles.Length.ToString()); foreach (HoughCircle circle in circles) { Pen pen = new Pen(Brushes.Black, 5); g = Graphics.FromImage(houghCirlceImage); g.DrawEllipse(pen, circle.X, circle.Y, 5, 5); g.Dispose(); } pictureBox2.Image = sourceImage; }
// Open image file private void openToolStripMenuItem_Click(object sender, EventArgs e) { try { // show file open dialog if (openFileDialog.ShowDialog() == DialogResult.OK) { // load image var tempImage = (Bitmap)Bitmap.FromFile(openFileDialog.FileName); var image = AForge.Imaging.Image.Clone(tempImage, PixelFormat.Format24bppRgb); tempImage.Dispose(); // format image AForge.Imaging.Image.FormatImage(ref image); // lock the source image var sourceData = image.LockBits( new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat); // binarize the image var binarySource = filter.Apply(new UnmanagedImage(sourceData)); // apply Hough line transofrm lineTransform.ProcessImage(binarySource); // get lines using relative intensity var lines = lineTransform.GetLinesByRelativeIntensity(0.2); foreach (var line in lines) { var s = string.Format("Theta = {0}, R = {1}, I = {2} ({3})", line.Theta, line.Radius, line.Intensity, line.RelativeIntensity); System.Diagnostics.Debug.WriteLine(s); // uncomment to highlight detected lines /* * // get line's radius and theta values * int r = line.Radius; * double t = line.Theta; * * // check if line is in lower part of the image * if ( r < 0 ) * { * t += 180; * r = -r; * } * * // convert degrees to radians * t = ( t / 180 ) * System.Math.PI; * * // get image centers (all coordinate are measured relative * // to center) * int w2 = image.Width /2; * int h2 = image.Height / 2; * * double x0 = 0, x1 = 0, y0 = 0, y1 = 0; * * if ( line.Theta != 0 ) * { * // none vertical line * x0 = -w2; // most left point * x1 = w2; // most right point * * // calculate corresponding y values * y0 = ( -Math.Cos( t ) * x0 + r ) / System.Math.Sin( t ); * y1 = ( -Math.Cos( t ) * x1 + r ) / System.Math.Sin( t ); * } * else * { * // vertical line * x0 = line.Radius; * x1 = line.Radius; * * y0 = h2; * y1 = -h2; * } * * // draw line on the image * Drawing.Line( sourceData, * new IntPoint( (int) x0 + w2, h2 - (int) y0 ), * new IntPoint( (int) x1 + w2, h2 - (int) y1 ), * Color.Red ); */ } System.Diagnostics.Debug.WriteLine("Found lines: " + lineTransform.LinesCount); System.Diagnostics.Debug.WriteLine("Max intensity: " + lineTransform.MaxIntensity); // apply Hough circle transform circleTransform.ProcessImage(binarySource); // get circles using relative intensity var circles = circleTransform.GetCirclesByRelativeIntensity(0.5); foreach (var circle in circles) { var s = string.Format("X = {0}, Y = {1}, I = {2} ({3})", circle.X, circle.Y, circle.Intensity, circle.RelativeIntensity); System.Diagnostics.Debug.WriteLine(s); } System.Diagnostics.Debug.WriteLine("Found circles: " + circleTransform.CirclesCount); System.Diagnostics.Debug.WriteLine("Max intensity: " + circleTransform.MaxIntensity); // unlock source image image.UnlockBits(sourceData); // dispose temporary binary source image binarySource.Dispose(); // show images sourcePictureBox.Image = image; houghLinePictureBox.Image = lineTransform.ToBitmap(); houghCirclePictureBox.Image = circleTransform.ToBitmap(); } } catch { MessageBox.Show("Failed loading the image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }