Search code examples
image-processingcomputer-visionedge-detection

Edge Detection with Thresholded Image


I'm trying to properly detect the edges of a playing card that has been blurred, grayscaled and then thresholded. I thought having the sharp black and white contrast would make the edges quite easy to detect, but no joy so far. I'm starting with:

enter image description here

And using the Canny Edge Detector I wrote producing this:

enter image description here

The result of Sobel was basically the same. However using OpenCV's Canny Detection I could produce this:

enter image description here

The border being correctly fitted together is what I'm desperately needing to recreate in my own code, and I'm not committed to using any particular type of Edge Detection, I just need to find an algorithm that will give me the connected edge! My Canny code can be found here, and it is very much based from the LIRE code here. If anybody could help me go from the first image to the third I would be incredibly grateful! Any edge detection welcome!

Edit: Code for NMS:

    //main program
    for(int x = 1; x < width-1; x++)
    {
        for(int y = 1; y < height-1; y++)
        {
            if(src.getRaster().getPixel(x, y, tmp)[0] >= 250)
            {
                trackWeakOnes(x, y, src);
            }
        }
    }

    private static void trackWeakOnes(int x, int y, BufferedImage src) 
    {
        for (int a = x - 1; a <= x + 1; a++)
        {
            for (int b = y - 1; b <= y + 1; b++) 
            {
                if (checkWeak(a, b, src)) 
                {
                    src.getRaster().setPixel(x, y, tmpMax);
                    trackWeakOnes(a, b, src);
                }
            }
        }
    }

    private static boolean checkWeak(int x, int y, BufferedImage src)
    {   
        return ((src.getRaster().getPixel(x, y, tmpPix)[0] > 0) && 
                (src.getRaster().getPixel(x, y, tmpPix)[0] < 255));
    }

tmpPix is an empty array to be filled, tmpMax is an array {255, 255, 255} to make edges white.


Solution

  • For this clean image, you don't need complex algorithms. A couple of simple filters will do the trick.

    In matlab, the code looks like:

    O=abs(filter2([-1 0 1],I))+abs(filter2([-1;0;1],I));
    

    which means that for each pixel (x,y) you do:

    output(x,y) = abs( I(x+1,y)-I(x-1,y) ) + abs( I(x,y+1) - I(x,y-1) );