Chapter 6: Object Detection

a. Good feature and SURF

The two functions in which we are interested in this section are GoodFeaturesToTrack and extractSURF. They are both high level function that allow features detection and especially corners. The example below how how to use them in the most simple way but before some explanation about this functions.

  • GoodFeaturesToTrack: This function is especially designed to do corner detection. By default it uses¬†CornerMinEigenVal algorithm but you can change it to use CornerHarris (as seen above) by changing a parameter value. The main difference with the Harris algorithm is that you should specify the minimum distance between each point, the quality level and the number of corners to detect. So at some point it does automaticaly what has been done in the previous chapter with the Harris example.
  • SURF: This alogrithm is also especialised in feature detection but provide more informations like the corner orientation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2.cv as cv
import math

im = cv.LoadImage("img/church.png", cv.CV_LOAD_IMAGE_GRAYSCALE)
im2 = cv.CloneImage(im)

# Goodfeatureto track algorithm
eigImage = cv.CreateMat(im.height, im.width, cv.IPL_DEPTH_32F)
tempImage = cv.CloneMat(eigImage)
cornerCount = 500
quality = 0.01
minDistance = 10

corners = cv.GoodFeaturesToTrack(im, eigImage, tempImage, cornerCount, quality, minDistance)

radius = 3
thickness = 2

for (x,y) in corners:
    cv.Circle(im, (int(x),int(y)), radius, (255,255,255), thickness)

cv.ShowImage("GoodfeaturesToTrack", im)

#SURF algorithm
hessthresh = 1500 # 400 500
dsize = 0 # 1
layers = 1 # 3 10

keypoints, descriptors = cv.ExtractSURF(im2, None, cv.CreateMemStorage(), (dsize, hessthresh, 3, layers))
for ((x, y), laplacian, size, dir, hessian) in keypoints:
    cv.Circle(im2, (int(x),int(y)), cv.Round(size/2), (255,255,255), 1)
    x2 = x+((size/2)*math.cos(dir))
    y2 = y+((size/2)*math.sin(dir))
    cv.Line(im2, (int(x),int(y)), (int(x2),int(y2)), (255,255,255), 1)

cv.ShowImage("SURF ", im2)

cv.WaitKey(0)

GoodFeatureToTrack:

SURF:

b. Face detection

Regarding all what we have done before, you can think that detect faces in a picture is quite hard, but it is in fact pretty easy with the HaarDetectObjects. This function take an XML file in argument  that describe the feature to detect. Hopefully the OpenCV provide multiples xml files to detect features like the face, eyes, ear mouse or the nose. The usage of this function is straightforward and the example below show a program that blur the face on an image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import cv2.cv as cv

image=cv.LoadImage('img/alkaline.jpg', cv.CV_LOAD_IMAGE_COLOR)

#Load the haar cascade
hc = cv.Load("haarcascades/haarcascade_frontalface_alt.xml")

#Detect face in image
faces = cv.HaarDetectObjects(image, hc, cv.CreateMemStorage(), 1.2,2, cv.CV_HAAR_DO_CANNY_PRUNING, (0,0) )

for ((x,y,w,h),stub) in faces:

    face = cv.GetSubRect(image, (x,y,w,h)) #Get the coordinate of the face in a rectangle

    cv.Smooth(face,face,cv.CV_BLUR, 15,15) #Blur the face for instance

    cv.Rectangle(image,(int(x),int(y)),(int(x)+w,int(y)+h),(0,255,0),2,0) #Draw a rectangle around the face

cv.ShowImage("Face detect", image)
cv.WaitKey(0)

Result:

<<Line Edge And Contours Detection | Home | Video IO and Sources>>