Mikos
Mikos

Reputation: 8563

AForge.net edge detection - how to get the edge points?

I am using Aforge to run edge detection on an image, how would I get the x,y for the detected edge(s) points? Other than the obvious way of looping through the image bitmaps.

This is the code from the Aforge samples, but how can I get the edge points?

    // On Filters->Sobel edge detector
            private void sobelEdgesFiltersItem_Click( object sender, System.EventArgs e )
            {
                // save original image
                Bitmap originalImage = sourceImage;
                // get grayscale image
                sourceImage = Grayscale.CommonAlgorithms.RMY.Apply( sourceImage );
                // apply edge filter
                ApplyFilter( new SobelEdgeDetector( ) );
                // delete grayscale image and restore original
                sourceImage.Dispose( );
                sourceImage = originalImage;

// this is the part where the source image is now edge detected. How to get the x,y for //each point of the edge? 

                sobelEdgesFiltersItem.Checked = true;
            }

Upvotes: 1

Views: 9394

Answers (2)

Steven H
Steven H

Reputation: 39

Are the edges you want to detect from a certain shape? Because if so, you can use a BlobCounter and reason out what the coordinates of the shape are.

//Measures and sorts the spots. Adds them to m_Spots
private void measureSpots(ref Bitmap inImage)
{
    //The blobcounter sees white as blob and black as background
    BlobCounter bc = new BlobCounter();
    bc.FilterBlobs = false;
    bc.ObjectsOrder = ObjectsOrder.Area; //Descending order
    try
    {
        bc.ProcessImage(inImage);
        Blob[] blobs = bc.GetObjectsInformation();

        Spot tempspot;
        foreach (Blob b in blobs)
        {
            //The Blob.CenterOfGravity gives back an Aforge.Point. You can't convert this to
            //a System.Drawing.Point, even though they are the same...
            //The location(X and Y location) of the rectangle is the upper left corner of the rectangle.
            //Now I should convert it to the center of the rectangle which should be the center
            //of the dot.
            Point point = new Point((int)(b.Rectangle.X + (0.5 * b.Rectangle.Width)), (int)(b.Rectangle.Y + (0.5 * b.Rectangle.Height)));

            //A spot (self typed class) has an area, coordinates, circularity and a rectangle that defines its region
            tempspot = new Spot(b.Area, point, (float)b.Fullness, b.Rectangle);

            m_Spots.Add(tempspot);
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

Hopefully this helps.


After typing this I saw the questions' date, but seeing as how I've already typed everything I'll just post it. Hopefully it will do good to someone.

Upvotes: 0

Oli
Oli

Reputation: 840

The filters are merely what the name suggests: Filters (Image -> Process -> NewImage)

I don't know, if there is something similar for edges, but AForge has a corner detector. My sample loads an image, runs the corner detector and displays little red boxes around every corner. (You'll need a PictureBox control named "pictureBox").

    public void DetectCorners()
    {
        // Load image and create everything you need for drawing
        Bitmap image = new Bitmap(@"myimage.jpg");
        Graphics graphics = Graphics.FromImage(image);
        SolidBrush brush = new SolidBrush(Color.Red);
        Pen pen = new Pen(brush);

        // Create corner detector and have it process the image
        MoravecCornersDetector mcd = new MoravecCornersDetector();
        List<IntPoint> corners = mcd.ProcessImage(image);

        // Visualization: Draw 3x3 boxes around the corners
        foreach (IntPoint corner in corners)
        {
            graphics.DrawRectangle(pen, corner.X - 1, corner.Y - 1, 3, 3);
        }

        // Display
        pictureBox.Image = image;
    }

It might not be exactly what you're looking for, but maybe it helps.

Upvotes: 6

Related Questions