通过阅读stackoverflow上的几个答案,到目前为止我已经学到了很多东西:
掩码必须是numpy
数组(其形状与图像相同),数据类型为CV_8UC1
,其值为0
至255
。< / p>
但这些数字的含义是什么?是否会在检测过程中忽略具有相应掩码值为零的任何像素,并且将使用掩码值为255的任何像素?两者之间的价值怎么样?
另外,如何在python中初始化数据类型为numpy
的{{1}}数组?我可以使用CV_8UC1
以下是我目前使用的代码,基于我在上面做出的假设。但问题是,当我为任一图片运行dtype=cv2.CV_8UC1
时,我无法获得任何关键点。我觉得这可能是因为掩码不是正确的数据类型。如果我对此有所了解,我该如何纠正?
detectAndCompute
答案 0 :(得分:1)
所以答案中的大多数(如果不是全部)都是:
这些数字是什么意思
0表示忽略像素,255表示使用它。我仍然不清楚它们之间的值,但我不认为掩码中所有非零值都被认为是“等效”到255。见here。
另外,如何在python中初始化数据类型为CV_8UC1的numpy数组?
CV_8U类型是无符号的8位整数,使用numpy是numpy.uint8
。 C1后缀意味着阵列是1通道,而不是3通道用于彩色图像,4通道用于rgba图像。因此,要创建一个1通道无符号8位整数数组:
import numpy as np
np.zeros((480, 720), dtype=np.uint8)
(三通道阵列的形状为(480, 720, 3)
,四通道(480, 720, 4)
等。)此掩码会导致探测器和提取器忽略整个图像,因为它全部为零
如何更正[代码]?
有两个单独的问题,每个问题分别导致每个关键点数组为空。
首先,我忘了设置base_mask
base_mask = np.array(np.where(base_cond, 255, 0)) # wrong
base_mask = np.array(np.where(base_cond, 255, 0), dtype=uint8) # right
其次,我使用了错误的图像来生成我的curr_cond
数组:
curr_cond = self.base[:,:,3] == 255 # wrong
curr_cond = self.curr[:,:,3] == 255 # right
一些非常愚蠢的错误。
以下是完整更正的代码:
# 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), dtype=np.uint8)
curr_cond = self.curr[:,:,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)
TL; DR:掩码参数是一个1通道numpy
阵列,其形状与灰度图像的形状相同,您在其中寻找要素(如果图像形状为(480, 720)
,那么掩模)。
数组中的值属于np.uint8
,255
表示“使用此像素”,0
表示“请勿”
感谢Dan Mašek引导我完成了部分答案。