1
votes

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.

2
I don't get the point of your question. Would you please clarify what you are looking for exactly?Saeed
Write my own edge detection that can detect the edges in the first image so it looks like the third image, as apposed to the broken edges in the second one. The one I've written doesn't detect the border of the card properly. I'm not committed to using only Canny, I'll try other edge detection algorithms if they work!Sean Kelly
Why don't you just use the OpenCV Canny if that works?Mark Setchell
Doing this as part of a uni project, identifying playing card suit/rank. Need to write my own edge detection, but have had very little luck.Sean Kelly

2 Answers

1
votes

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) );
1
votes

I didn't read your code, but I observe a strange artifact: along the horizontal edges, the detected pixels come in isolated 8-connected triples. I would suspect a flaw in the non-maximum suppression logics. (In any case, there is an anisotropy somewhere.)

This said, edge detection on a binary image can be done by contour tracing.