通过阅读stackoverflow上的几个答案,我已经了解到以下内容:
掩膜必须是一个numpy
数组(形状与图像相同),其数据类型为CV_8UC1
,并且具有从0
到255
的值。
然而,这些数字的含义是什么呢?是说任何相应掩码值为零的像素都将在检测过程中被忽略,而任何掩码值为255的像素都将被使用吗?那么介于两者之间的值呢?
此外,在Python中如何初始化具有数据类型CV_8UC1
的numpy
数组呢?我可以只使用dtype=cv2.CV_8UC1
吗?
这是我目前正在使用的代码,基于我以上所做的假设。但问题在于当我对任何一张图片运行detectAndCompute
时都没有得到关键点。我有一种感觉,可能是因为掩膜不是正确的数据类型。如果我的想法是正确的,那么我该怎么改正呢?
# convert images to grayscale
base_gray = cv2.cvtColor(self.base, cv2.COLOR_BGRA2GRAY)
curr_gray = cv2.cvtColor(self.curr, cv2.COLOR_BGRA2GRAY)
# initialize feature detector
detector = cv2.ORB_create()
# create a mask using the alpha channel of the original image--don't
# use transparent or partially transparent parts
base_cond = self.base[:,:,3] == 255
base_mask = np.array(np.where(base_cond, 255, 0))
curr_cond = self.base[:,:,3] == 255
curr_mask = np.array(np.where(curr_cond, 255, 0), dtype=np.uint8)
# use the mask and grayscale images to detect good features
base_keys, base_desc = detector.detectAndCompute(base_gray, mask=base_mask)
curr_keys, curr_desc = detector.detectAndCompute(curr_gray, mask=curr_mask)
print("base keys: ", base_keys)
# []
print("curr keys: ", curr_keys)
# []
uint8
。| 检查掩码确保它们是合理的。 | 这个链接是http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html中的第一个自然段。 - Dan Mašek