gpt4 book ai didi

python - 如何在检测后屏蔽 opencv 的关键点?

转载 作者:行者123 更新时间:2023-12-05 04:49:22 31 4
gpt4 key购买 nike

我在 python 上使用 opencv。

我正在寻找一种有效的方法来使用掩码来选择落入其中的所有关键点,在已经检测到关键点之后

要清楚 - 我知道我可以在检测阶段使用掩码,我需要的是在已经检测到关键点之后使用另一个(不同的)掩码(并且这个过程用多个子区域掩码重复,所以我不能在检测之前组合 2 个掩码)。

import cv2

image = cv2.imread(some_image_file)
mask = cv2.imread(some_mask_file)
mask_2 = cv2.imread(some_additional_mask_file)


# Defining the detector:
detector = cv2.ORB_create(scoreType=cv2.ORB_FAST_SCORE)

# Detecting keypoints inside the initial mask's region:
kp, dsk = detector.detectAndCompute(image, mask)

# Using mask_2 to select a subset of the detected keypoints:
...

最佳答案

根据 this发布后,您需要遍历所有关键点和描述符并将好的关键点和描述符添加到新列表中。

There is no possibility to mark them unnecessary.

这是一个示例代码:

import numpy as np
import cv2

some_image_file = 'graf.png'

image = cv2.imread(some_image_file)

rows, cols = image.shape[0], image.shape[1]

mask = np.full((rows, cols), 255, np.uint8) # cv2.imread(some_mask_file)
mask_2 = mask.copy() #cv2.imread(some_additional_mask_file)
mask_2[:, 0:cols//2] = 0

# Defining the detector:
detector = cv2.ORB_create(scoreType=cv2.ORB_FAST_SCORE)

# Detecting keypoints inside the initial mask's region:
kp, dsk = detector.detectAndCompute(image, mask)

good_kp = [] # List of "good keypoint"
good_dsk = [] # List of "good descriptors"


# Iterate over all keypoints and descriptors and the good ones to a new list.
# There is no possibility to mark them unnecessary.
# https://stackoverflow.com/questions/29180815/delete-matches-in-opencv-keypoints-and-descriptors
for k, d in zip(kp, dsk):
x, y = k.pt # Each keypoint as an x, y tuple https://stackoverflow.com/questions/35884409/how-to-extract-x-y-coordinates-from-opencv-cv2-keypoint-object

if mask_2[int(y), int(x)] != 0:
good_kp.append(k) # Append keypoint to a list of "good keypoint".
good_dsk.append(d) # Append descriptor to a list of "good descriptors".

# Draw keypoints for testing
image_kp = image.copy()
cv2.drawKeypoints(image, kp, image_kp)

image_good_kp = image.copy()
cv2.drawKeypoints(image, good_kp, image_good_kp)

cv2.imshow('image_kp', image_kp)
cv2.imshow('image_good_kp', image_good_kp)
cv2.waitKey()
cv2.destroyAllWindows()

输入图像:
enter image description here

image_kp:
enter image description here

image_good_kp.png(左侧关键点被屏蔽):
enter image description here

关于python - 如何在检测后屏蔽 opencv 的关键点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67622830/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com