I’ve been hearing about Python for a few years now, but I haven’t had an opportunity (or motivation) to really dive in and learn it. However, last week after a business pitch from my aunt, I wanted to play around with edge detection to see if it might solve the problem she presented. This also seemed like an ideal opportunity to give Python a shot. In the end, I am very happy with Python, but less so with my edge detector.
I started out by reading up on the Canny Edge Detect algorithm. There are lots of sources of information, but the best outline seems to be on Wikipedia. http://en.wikipedia.org/wiki/Canny_edge_detector
For testing I just used a couple of the images that come with the windows install.

The first step is to blur the image so only the really prominent edges will be detected and hopefully any noise will be eliminated. You can do this by convolving your image with a small filter matrix. Wow, look at all the fancy terminology. Essentially what this meant to me was that every pixel in your image needs to be blended with the pixels around it. You can either go out one layer with a 3×3 matrix or 2 layers with a 5×5 matrix. You can go bigger, but the the information I found seems to suggest that it is not necessary. The matrix basically tells you how much weight each of the surrounding pixels should have on the new pixel color you are generating. So the pixel you are looking at should have the biggest weight. The first layer of pixels around it should have a slightly lower weight. And if you go further out, each layer should have smaller weight then the one before it. The picture below gives a visual representation of this idea. There is a mathmatical theory for figuring out what those percentages should actually be, but you can probably find better explanations of it elsewhere.

Next, gray scaling the image helps in the calculation of what is actually an edge. Instead of trying to calculate if the pixel at point X is a different RGB color than a pixel at point Y, you can simply use the gray scale intensity to make the comparison. This drops you from a 3 value to 3 value comparison to a 1 value to 1 value.

Although to a human eye, edge detection is a very simple process, it is not so easy for a computer. We have to perform a couple different passes to determine what is actually an edge and what is not. I chose the Sobel operator which returns potential edges in the horizontal and vertical direction.

These images can be combined and some additional math performed to find the direction of each edge that was detected.

Using non-maximum suppression and tracing edges with thresholds can lead to some very good results. However, in my case, I wasn’t as happy with the final products as I was hoping.

I used some additional images to see what kind of results my algorithm would produce on different types of pictures.














