python opencv TypeError: Layout of the output array incompatible with cv::Mat

前端 未结 5 1096
温柔的废话
温柔的废话 2021-01-02 01:54

I\'m using the selective search here: http://koen.me/research/selectivesearch/ This gives possible regions of interest where an object might be. I want to do some processing

5条回答
  •  暖寄归人
    2021-01-02 02:26

    Opencv appears to have issues drawing to numpy arrays that have the data type np.int64, which is the default data type returned by methods such as np.array and np.full:

    >>> canvas = np.full((256, 256, 3), 255)
    >>> canvas
    array([[255, 255, 255],
           [255, 255, 255],
           [255, 255, 255]])
    >>> canvas.dtype
    dtype('int64')
    >>> cv2.rectangle(canvas, (0, 0), (2, 2), (0, 0, 0))
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels)
    

    The solution is to convert the array to np.int32 first:

    >>> cv2.rectangle(canvas.astype(np.int32), (0, 0), (2, 2), (0, 0, 0))
    array([[  0,   0,   0],
           [  0, 255,   0],
           [  0,   0,   0]], dtype=int32)
    

提交回复
热议问题