简体   繁体   English

OpenCV 的 Python ORB 模块的掩码格式是什么?

[英]What is the format of the mask for OpenCV's Python ORB module?

I've been trying to use ORB to find keypoint/descriptors and I need to mask out part of the image because many features are very similar in two parts of my image.我一直在尝试使用 ORB 来查找关键点/描述符,我需要掩盖图像的一部分,因为图像的两个部分中的许多特征非常相似。 However, I can't determine the correct format of the mask parameter to the detectandcompute function, and the documentation is ambiguous to me.但是,我无法确定检测和计算函数的掩码参数的正确格式,并且文档对我来说是模棱两可的。 I tried looking at the source code but I am not familiar enough with C++ to understand it.我尝试查看源代码,但我对 C++ 不够熟悉,无法理解它。 I thought it was just a binary array where 1 = use and 0 = ignore, but every mask I've tried doesn't return any keypoints.我以为它只是一个二进制数组,其中 1 = 使用和 0 = 忽略,但我尝试过的每个掩码都没有返回任何关键点。 Here is some example code:下面是一些示例代码:

img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
#ignore the left half of the first image
mask1 = np.ones(img1_gray.shape)
mask1[:,:mask1.shape[1]/2] = 0
#ignore the right half of the second image
mask2 = np.ones(img2_gray.shape)
mask2[:,mask2.shape[1]/2:] = 0
kp1, des1 =orb.detectAndCompute(img1_gray,mask1)
kp2, des2 =orb.detectAndCompute(img2_gray,mask2)

The documentation is here: http://docs.opencv.org/3.0-beta/modules/features2d/doc/feature_detection_and_description.html文档在这里: http : //docs.opencv.org/3.0-beta/modules/features2d/doc/feature_detection_and_description.html

img1 img2 IMG1 IMG2

I've used orb in OpenCV C++ and if I recall correctly the mask has to be CV_8UC1 type with values 0 and 255 (at least that's what I used).我在 OpenCV C++ 中使用过 orb,如果我没记错的话,掩码必须是 CV_8UC1 类型,值为 0 和 255(至少这是我使用的)。 Have you already tried like this?你已经试过这样了吗?

ORB is great for detecting pattern on image or finding image duplicates, but for such kind of task, ORB may be not enough informative, because it's designed as fast and lightweight binary descriptor. ORB 非常适合检测图像上的模式或查找图像重复项,但对于此类任务,ORB 可能提供的信息不足,因为它被设计为快速且轻量级的二进制描述符。 Try to look at SIFT and SURF试试看SIFTSURF

The bug in the above code was that the masks had to be changed to Uint8.上面代码中的错误是必须将掩码更改为 Uint8。 Thus, this would change the masks to be of CV_8UC1 type with values 0 to 255. Here is the fully tested working Python code (Version 3) using SIFT features rather than ORB features:因此,这会将掩码更改为值为 0 到 255 的 CV_8UC1 类型。以下是使用 SIFT 功能而不是 ORB 功能的经过全面测试的工作 Python 代码(版本 3):

import cv2
import numpy as np 
import matplotlib.pyplot as plt

def showing_features(img1, key_points):
   plt.imshow(cv2.drawKeypoints(img1, key_points, None))
   plt.show() 

img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

sift= cv2.xfeatures2d.SIFT_create(nfeatures=0,
                            nOctaveLayers=3,
                            contrastThreshold=0.05,
                            edgeThreshold=10.0,
                            sigma=1.6)

"*-------------- Create Masks --------------*"
mask1 = np.ones(img1_gray.shape)
#ignore the left half of the first image
mask1[:,:int(mask1.shape[1]/2)] = 0
#ignore the right half of the second image
mask2 = np.ones(img2_gray.shape)
mask2[:,int(mask2.shape[1]/2):] = 0

"*-------------- Change Masks to Uint8 --------------*"
mask1 = mask1.astype(np.uint8)
mask2 = mask2.astype(np.uint8)

"*-------------- Extract SIFT Features --------------*"
kp1m, des1m =sift.detectAndCompute(img1_gray,mask1)
kp2m, des2m =sift.detectAndCompute(img2_gray,mask2)

"*-------------- Display SIFT features after using MASK --------------*"
showing_features(img1, kp1m)
showing_features(img2, kp2m)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM