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 -

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.

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.

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