How do I create KeyPoints to compute SIFT?

2024/10/15 5:22:54

I am using OpenCV-Python.

I have identified corner points using cv2.cornerHarris. The output is of type dst.

I need to compute SIFT features of the corner points. The input to sift.compute() has to be of the type KeyPoint.

I'm not able to figure out how to use cv2.KeyPoint().

How do I do this?

Answer

Harris detector return dst, which have same shape with your image. Harris mark on dst where it think the corner. So, you have to extract keypoint from dst.

def harris(self, img):'''Harris detector:param img: an color image:return: keypoint, image with feature marked corner'''gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)gray_img = np.float32(gray_img)dst = cv2.cornerHarris(gray_img, 2, 3, 0.04)result_img = img.copy() # deep copy image# Threshold for an optimal value, it may vary depending on the image.result_img[dst > 0.01 * dst.max()] = [0, 0, 255]# for each dst larger than threshold, make a keypoint out of itkeypoints = np.argwhere(dst > 0.01 * dst.max())keypoints = [cv2.KeyPoint(x[1], x[0], 1) for x in keypoints]return (keypoints, result_img)
https://en.xdnf.cn/q/69325.html

Related Q&A

Error in Tensorboards(PyTorch) add_graph

Im following this Pytorchs Tensorboard documentation. I have the following code: model = torchvision.models.resnet50(False) writer.add_graph(model)It throws the following error:_ = model(*args) # dont…

Population must be a sequence or set. For dicts, use list(d)

I try to excute this code and I get the error bellow, I get the error in the random function and I dont know how to fix it, please help me.def load_data(sample_split=0.3, usage=Training, to_cat=True, v…

Why do imports fail in setuptools entry_point scripts, but not in python interpreter?

I have the following project structure:project |-project.py |-__init__.py |-setup.py |-lib|-__init__.py|-project|-__init__.py|-tools.pywith project.py:from project.lib import *def main():print("ma…

msgpack unserialising dict key strings to bytes

I am having issues with msgpack in python. It seems that when serialising a dict, if the keys are strings str, they are not unserialised properly and causing KeyError exceptions to be raised.Example:&g…

Better solution for Python Threading.Event semi-busy waiting

Im using pretty standard Threading.Event: Main thread gets to a point where its in a loop that runs:event.wait(60)The other blocks on a request until a reply is available and then initiates a:event.set…

\ufeff Invalid character in identifier

I have the following code :import urllib.requesttry:url = "https://www.google.com/search?q=test"headers = {}usag = Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:25.0) Gecko/20100101 Firefo…

Python multiprocessing - Passing a list of dicts to a pool

This question may be a duplicate. However, I read lot of stuff around on this topic, and I didnt find one that matches my case - or at least, I didnt understood it.Sorry for the inconvenance.What Im tr…

Failed to write to file but generates no Error

Im trying to write to a file but its not working. Ive gone through step-by-step with the debugger (it goes to the write command but when I open the file its empty).My question is either: "How do I…

train spacy for text classification

After reading the docs and doing the tutorial I figured Id make a small demo. Turns out my model does not want to train. Heres the codeimport spacy import random import jsonTRAINING_DATA = [["My l…

Python threading vs. multiprocessing in Linux

Based on this question I assumed that creating new process should be almost as fast as creating new thread in Linux. However, little test showed very different result. Heres my code: from multiprocessi…