In this post, we will write 2 python scripts -

  1. First one, to load a image, extract the keypoints and save them in a file.
  2. Second one, load the file saved in the previous script and draw the contours on the same image used in step 1.

Normally, for loading and saving data, we will use cPickle package. To store data in a file we use the dump function and to load data we use loads function. The problem with OpenCV’s keypoint type is that we cannot directly dump keypoints objects using dump function. If we try to do so, we’ll get an error as shown below -

pickle.PicklingError: Can't pickle <type 'cv2.KeyPoint'>: it's not the same object as cv2.KeyPoint

So, we need to serialize and deserialize the keypoints data.

So, let’s write code, for the first script for saving the keypoints in a file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import cv2
import cPickle

im = cv2.imread("/home/bikz05/Desktop/dataset/checkered-3.jpg")
gr = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
d = cv2.FeatureDetector_create("SIFT")
kp = d.detect(gr)

index = []
for point in kp:
temp = (point.pt, point.size, point.angle, point.response, point.octave, point.class_id
index.append(temp)

# Dump the keypoints
f = open("/home/bikz05/Desktop/dataset/keypoints.txt", "w")
f.write(cPickle.dumps(index))
f.close()

We have saved our keypoints in keypoints.txt. Now, let’s load those keypoints in a separate script and draw them on the same image.

Now let’s write second script which will load the keypoints from the file and display them on the image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import cv2
import cPickle

im = cv2.imread("/home/bikz05/Desktop/dataset/checkered-3.jpg")
index = cPickle.loads(open("/home/bikz05/Desktop/dataset/keypoints.txt").read())
kp = []

for point in index:
    temp = cv2.KeyPoint(x=point[0][0],y=point[0][1],_size=point[1], _angle=point[2], _response=point[3], _octave=point[4], _class_id=point[5]) 
    kp.append(temp)

# Draw the keypoints
imm = cv2.drawKeypoints(im, kp);
cv2.imshow("Image", imm);
cv2.waitKey(0)

That’s it, the above script when run will display the image with all the keypoints marked on it.

Results

Input Image
Figure 1: Input Image [JPG]
Output Image
Figure 2: Output Image [PNG]