'The parameter `image` must be a 2-dimensional array'

'The parameter `image` must be a 2-dimensional array'

我正在尝试通过 scikit-image 提取 orb 特征,但出现错误 The parameter image must be a 2-dimensional array。我将它转换为灰度,所以图像实际上是二维的。

    from skimage.feature import ORB
    from skimage.color import rgb2gray

    def find_orb(img, n_keypoints=2000, **kwargs):
        descriptor_extractor = ORB(n_keypoints, **kwargs)
        descriptor_extractor.detect_and_extract(rgb2gray(img))
        return descriptor_extractor.keypoints, descriptor_extractor.descriptors

    pano_image_collection = io.ImageCollection('jpeg/lowres/8_*.jpg',
                                    load_func=lambda f:io.imread(f).astype(np.float32) / 255)
    img = pano_image_collection[0]
    keypoints, descriptors = find_orb(img)

这就是错误

ValueError                                Traceback (most recent call last)
<ipython-input-5-5dce31f8d3f4> in <module>()
----> 7 keypoints, descriptors = find_orb(img)

<ipython-input-4-26e09ccf38ce> in find_orb(img, n_keypoints, **kwargs)
 14     descriptor_extractor = ORB(n_keypoints, **kwargs)
---> 15     descriptor_extractor.detect_and_extract(rgb2gray(img))
 16     return descriptor_extractor.keypoints, descriptor_extractor.descriptors

/usr/local/lib/python3.6/site-packages/skimage/feature/orb.py in detect_and_extract(self, image)
302 
303             keypoints, orientations, responses = \
--> 304                 self._detect_octave(octave_image)
305 
306             if len(keypoints) == 0:

/usr/local/lib/python3.6/site-packages/skimage/feature/orb.py in _detect_octave(self, octave_image)
139         # Extract keypoints for current octave
140         fast_response = corner_fast(octave_image, self.fast_n,
--> 141                                     self.fast_threshold)
142         keypoints = corner_peaks(fast_response, min_distance=1)
143 

/usr/local/lib/python3.6/site-packages/skimage/feature/corner.py in corner_fast(image, n, threshold)
745 
746     """
--> 747     image = _prepare_grayscale_input_2D(image)
748 
749     image = np.ascontiguousarray(image)

/usr/local/lib/python3.6/site-packages/skimage/feature/util.py in _prepare_grayscale_input_2D(image)
140 def _prepare_grayscale_input_2D(image):
141     image = np.squeeze(image)
--> 142     assert_nD(image, 2)
143     return img_as_float(image)
144 

/usr/local/lib/python3.6/site-packages/skimage/_shared/utils.py in assert_nD(array, ndim, arg_name)
176         raise ValueError(msg_empty_array % (arg_name))
177     if not array.ndim in ndim:
--> 178         raise ValueError(msg_incorrect_dim % (arg_name, '-or-'.join([str(n) for n in ndim])))
179 
180 

ValueError: The parameter `image` must be a 2-dimensional array

恐怕我帮不了你了: 我用调试器 运行 它在 ORB 内部创建的金字塔第二层的图像只有一个条目和形状 (1, 1),它将被缩减为一维图像随后 np.squeeze 调用。

更新:Op (Daria Musatkina) 找到了问题的解决方案,引用: 这里的问题是 orb 的第一个参数是下采样而不是 n_keypoints。这就是为什么创建形状为 (1, 1) 的八度。

作为参考,ORB API doc

我最初的回答是错误的(见下面的评论):

我假设您的图像是 RGB,它可能作为 2D + 通道(总共 3D)numpy.ndarrayuint8 个条目导入。 ndarray.astype 不改变图像的维度,只改变数据类型。而不是 uint8 的 3D 数组,您现在有一个具有相同值的 float32 的 3D 数组(这里不考虑任何数值错误)。因此,你没有转换成灰度space,你只是改变了数组的数据类型。例如,您可以尝试沿通道轴使用 np.mean