Monday, October 24, 2011

Corner Detection with OpenCV

Corner detection is an approach used within computer vision systems to extract certain kinds of features and infer the contents of an image [Ref]. We have seen in the previous post how to perform an edge detection using the Sobel operator. Using the edges, we can define a corner as a point for which there are two dominant and different edge directions in a local neighborhood of the point.
One of the most used tool for corner detection is the Harris Corner Detector operator. OpenCV provide a function that implement this operator. The name of the function is CornerHarris(...) and the corners in the image can be found as the local maxima of the returned image. Let's see how to use it in Python:
imcolor = cv.LoadImage('stairs.jpg')
image = cv.LoadImage('stairs.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
cornerMap = cv.CreateMat(image.height, image.width, cv.CV_32FC1)
# OpenCV corner detection

for y in range(0, image.height):
 for x in range(0, image.width):
  harris = cv.Get2D(cornerMap, y, x) # get the x,y value
  # check the corner detector response
  if harris[0] > 10e-06:
   # draw a small circle on the original image
   cv.Circle(imcolor,(x,y),2,cv.RGB(155, 0, 25))

cv.NamedWindow('Harris', cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage('Harris', imcolor) # show the image
cv.SaveImage('harris.jpg', imcolor)
The following image is the result of the program:
The red markers are the corners found by the OpenCV's function.


  1. Ok for the method, but it would have been much nicer with some toughtbacks, wouldn't it?

    OpenCV do contain lots of "magical" functions like this one. And more and more people use it without knowing what they. And so they finally become really "magic" ^^

    Simple thought for example, why did you choose this exact threshold?
    And what can you use your output for?

  2. Hi jlengrand, thanks for your comment.
    Usually I put a links that contains the theoretical background of the post and sometimes I try to explain what's under the hood by myself. Anyway, the original purpose of this blog is to provide snippets that you can reuse in another program just modifying some lines.

  3. And it is nice to have :).
    I think we do miss open source code for image processing in Python.
    So you are doing it the right way :).

    What I don't like (if you subscribe to the opencv channel you must have seen it) is the huge number of people using this code without thinking about it and posting for any dumm typo problem...

    See what I mean ?

    Are you writing a lots of code for IP in Python?

  4. Yes, I am writing lots of code using opencv because I'm involved in various computer vision projects.

    I'm not subscribed to the opencv channel, but I agree with you. You need a strong mathematical background to understand how opencv's functions work and some people use it without.

    By the way, the main lack of OpenCV is that there's few examples in Python and sometimes the documentation is not enough to understand how to use some of its function.

  5. I clearly agree.

    I have worked on computer vision for some time, in several langagues (most with Matlab though), and I really felt in love with Python/OpenCV.
    I hope it could somehow outfit the Matlab image processing toolbox.

    I am currently writing lots of functions in order to reduce the amount of code needed to deploy applications in Python, based on my matlab experience.
    I hope it will promoting Computer Vision :).

    Is there a list of IP projects you are involved in somewhere? I would love to know about them

  6. I'm sorry, at moment I can't share any official documentation.

    Keep in touch via twitter or linkedin.

  7. It is very helpful. Thanks :-)

  8. Thaks for this help

  9. HI, could you please post the java equivalent code?

    1. Hi Tharaka, you can take a look at this: