Search code examples
pythonopencvcomputer-visionfeature-extraction

How to manually set keypoints and extract features


I'm using ORB to detect keypoints on a set of images, like in this example:

enter image description here

What i want to do is: i want to manually set 22 points at specific coordinates of the image and store the features extracted from these points into feature vectors. For example:

enter image description here

After that, store those features into respectively a 22th dimensional vector.

The code im currently using to load my images and set the keypoints is this:

import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
from sklearn.utils import Bunch
from skimage.io import imread
import skimage
import cv2

DATADIR = "C:/Dataset"
CATEGORIES = ["class 1", "class 2", "class 3", "class 4", "class 5"]


def load_image_files(fullpath):
    descr = "A image classification dataset"
    for category in CATEGORIES:
        path = os.path.join(DATADIR, category)
        for person in os.listdir(path):
            personfolder = os.path.join(path, person)
            for imgname in os.listdir(personfolder):
                class_num = CATEGORIES.index(category)
                fullpath = os.path.join(personfolder, imgname)
                imageList = skimage.io.imread(fullpath)
                orb = cv2.ORB_create(22)
                kp, des = orb.detectAndCompute(imageList, None)'''
                orb = cv2.ORB_create()
                key_points = [cv2.KeyPoint(64, 9, 1), cv2.KeyPoint(107, 6, 10), cv2.KeyPoint(171, 10, 10)]
                kp, des = orb.compute(imageList, key_points)
                drawnImages = cv2.drawKeypoints(imageList, kp, None, flags= cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
                cv2.imshow("Image", drawnImages)
                cv2.waitKey(0)

    return Bunch(target_names=CATEGORIES,
                     images=images,
                     DESCR=descr)

These are the coordinates i wish to extract features from

p1 = 60, 10
p2 = 110, 10
p3 = 170, 10
p4 = 25, 60
p5 = 60, 40
p6 = 110, 35
p7 = 170, 35
p8 = 190, 60
p9 = 30, 95
p10 = 60, 80
p11 = 100, 105
p12 = 120, 105
p13 = 160, 180
p14 = 185, 95
p15 = 25, 160
p16 = 55, 160
p17 = 155, 160
p18 = 185, 160
p19 = 65, 200
p20 = 83, 186
p21 = 128, 186
p22 = 157, 197

Solution

  • Use the compute method in the orb API. Something standard would be

    kp = orb.detect(img,None)
    kp, des = orb.compute(img, kp)
    

    But for your case, key points come from user input so use something like

    input_kp = # comes from user
    kp, des = orb.compute(img, input_kp)
    

    Make sure that the input keypoints match the format that the compute method is expecting. You can create a key point from x, y values like this.

    key_points = [cv2.KeyPoint(x1, y1, 1), cv2.KeyPoint(x2, y2, 1) ...]