“参数`image`必须是二维数组”

时间:2019-01-15 16:05:41

标签: python scikit-image orb

我正在尝试通过scikit-image提取球体特征,但出现错误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

1 个答案:

答案 0 :(得分:1)

恐怕我无能为力了: 我使用调试器运行它,并且在ORB内部创建的金字塔第二层上的图像只有一个条目和形状为(1, 1),它将在随后的{{1 }}。

更新:Op(Daria Musatkina)找到了解决此问题的方法,并引用: 这里的问题是orb的第一个参数是降采样而不是n_keypoints。这就是创建形状为np.squeeze的八度音阶的原因。

ORB API doc

供参考

我的最初答案不正确(请参见下面的评论):

我假设您的图像是RGB,可能是作为2D +通道(总共3D)导入的,(1, 1)numpy.ndarray个条目。 uint8不会更改图像的尺寸,只会更改数据类型。现在,您有了一个ndarray.astype的3D数组,而不是uint8的3D数组,它们的值相同(此处未考虑任何数字误差)。因此,您没有转换为灰度空间,只是更改了数组的数据类型。例如,您可以尝试沿通道轴使用np.mean