Corner detection (http://en.wikipedia.org/wiki/Corner_detection ) is a standard technique in Computer Vision. scikits-image offers a Harris Corner Detector, which is great, because corner detection is pretty complicated. Obviously, we could do it ourselves from scratch, but that would violate the cardinal rule of not reinventing the wheel.
You might need to install jpeglib
on your system to be able to load the scikits-learn image, which is a JPEG file. If you are on Windows, use the installer; otherwise, download the distribution, unpack it, and build from the top folder with the following command:
./configure make sudo make install
We will load a sample image from scikits-learn. This is not absolutely necessary for this example; you can use any other image instead.
scikits-learn currently has two sample JPEG images in a dataset structure. We will look at the first image only:
dataset = load_sample_images() img = dataset.images[0]
Call the
harris
function to get the coordinates of corners:
harris_coords = harris(img) print "Harris coords shape", harris_coords.shape y, x = numpy.transpose(harris_coords)
The code for the corner detection is as follows:
from sklearn.datasets import load_sample_images from matplotlib.pyplot import imshow, show, axis, plot import numpy from skimage.feature import harris dataset = load_sample_images() img = dataset.images[0] harris_coords = harris(img) print "Harris coords shape", harris_coords.shape y, x = numpy.transpose(harris_coords) axis('off') imshow(img) plot(x, y, 'ro') show()
We get an image with red dots, where corners are detected, as shown in the following screenshot:
We applied the Harris corner detection on a sample image from scikits-image. The result is pretty good, as you can see. We could have done this with NumPy only, since it is just a straightforward, linear-algebra type computation, still it could have become pretty messy. The scikits-image toolkit has a lot more similar functions, so check the scikits-image documentation if you are in need of an image processing routine.