Search code examples
c#image-processingaforgehough-transform

Get Number of lines in an image using AForge or OpenCV


I am trying to detect if a line passes through an image or not using C# and AForge hough transformation line detection. In the same context, I am thinking in a better solution that detect if the image is clear (No lines) then to return a false value and vice versa. I have the following image and I want to check if the line passes through it, I will return true, Otherwise return false:

http://s10.postimg.org/3sn8wari1/image.png

I used the following code to get the number of lines, but it seems that it is not accurate or I am using the algorithm badly.

        AForge.Imaging.Image.FormatImage(ref SEChild);
        // lock the source image
        BitmapData sourceData = SEChild.LockBits(
            new System.Drawing.Rectangle(0, 0, SEChild.Width, SEChild.Height),
            ImageLockMode.ReadOnly, SEChild.PixelFormat);
        // binarize the image
        UnmanagedImage binarySource = filter.Apply(new UnmanagedImage(sourceData));

        HoughLineTransformation lineTransform = new HoughLineTransformation();
        lineTransform. = 10;
        // apply Hough line transofrm
        lineTransform.ProcessImage(binarySource);
        HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(0.5);
        if (lines.Count() > 0)
        {
            Result += "NW: Yes!\n";
        }
        else
        {
            Result += "NW: No!\n";
        }

Solution

  • I applied a solution that is powerful with all curves except the straight lines.

    I used AForge.net Library and used the following steps as shown in the code afterwards:

    1. Apply the Euclidean Color Filtering on the image
    2. Apply Blob Counter on the resulted image and get the number of rectangles(Blobs) that have been found in the image.

    The Code:

      private bool CheckLines(Bitmap image, Color filterColor)
        {
            EuclideanColorFiltering ColorFilter = new EuclideanColorFiltering();
            // set center colour and radius
            AForge.Imaging.RGB color = new RGB(filterColor.R, filterColor.G, filterColor.B, filterColor.A);
            ColorFilter.CenterColor = color;
            ColorFilter.Radius = 100;
            // Apply the filter
            ColorFilter.ApplyInPlace(image);
    
            // Define the Blob counter and use it!
            BlobCounter blobCounter = new BlobCounter();
            blobCounter.MinWidth = 5;
            blobCounter.MinHeight = 5;
            blobCounter.FilterBlobs = true;
            //blobCounter.ObjectsOrder = ObjectsOrder.Size;
            blobCounter.ProcessImage(image);
            System.Drawing.Rectangle[] rects = blobCounter.GetObjectsRectangles();
            if (rects.Length > 0)
            {
                return true;
            }
            return false;
        }