Tuesday, October 12, 2010

Good Features to Track using OpenCV and Python

import cv

filename =  "burke94.jpg" # replace w/ your filename
grayImage = cv.LoadImage(filename, 2)
eigImage = cv.CreateImage(cv.GetSize(grayImage), cv.IPL_DEPTH_32F, 1)
tempImage = cv.CreateImage(cv.GetSize(grayImage), cv.IPL_DEPTH_32F, 1)

cornerMem = []
cornerCount = 300
qualityLevel = 0.1
minDistance = 5
cornerMem = cv.GoodFeaturesToTrack(grayImage, eigImage, tempImage,  cornerCount, qualityLevel, minDistance, None, 3, False)

print len(cornerMem), " corners found"
print cornerMem


for point in cornerMem:
    center = int(point[0]), int(point[1])
    cv.Circle(colorImage, (center), 2, (0,255,255))
cv.SaveImage("savedcolor.jpg",colorImage)

3 comments:

  1. Unfortunately, the codes does not compile. colorImage is not defined.

    ReplyDelete
  2. Oh dear, I just realized that. GFtT works on grayscale image converted from the original color image. The pointer colorImage should point to a copy of the original image. If the statement colorImage = cv.LoadImage(filename, ??) is inserted as Line 2 it should work but I'm not working with OpenCV at the moment to find the exact value for the second paramete.

    ReplyDelete
  3. Thanks for the code. It works if you add this:
    import cv

    filename = "lenna.png" # replace w/ your filename
    colorImage = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_COLOR) #additional line
    grayImage = cv.LoadImage(filename, cv.CV_LOAD_IMAGE_GRAYSCALE) #optional, I just change it slightly to match up the top part
    eigImage = cv.CreateImage(cv.GetSize(grayImage), cv.IPL_DEPTH_32F, 1)
    tempImage = cv.CreateImage(cv.GetSize(grayImage), cv.IPL_DEPTH_32F, 1)

    ReplyDelete