2014-05-23 61 views
5

我在这里使用了选择性搜索:http://koen.me/research/selectivesearch/ 这给出了可能的对象所在的区域。我想要做一些处理并只保留一些区域,然后删除重复的边界框以获得最终整齐的边界框集合。为了丢弃不需要/重复的边界框区域,我使用了opencv的grouprectangles函数进行修剪。python opencv TypeError:输出数组的布局与cv不兼容:: Mat

有一次,我从上面的链接“选择性搜索算法”得到Matlab的有趣的地区,我保存在.mat文件中的结果,然后在Python程序检索它们,就像这样:

import scipy.io as sio 
inboxes = sio.loadmat('C:\\PATH_TO_MATFILE.mat') 
candidates = np.array(inboxes['boxes']) 
# candidates is 4 x N array with each row describing a bounding box like this: 
# [rowBegin colBegin rowEnd colEnd] 
# Now I will process the candidates and retain only those regions that are interesting 
found = [] # This is the list in which I will retain what's interesting 
for win in candidates: 
    # doing some processing here, and if some condition is met, then retain it: 
    found.append(win) 

# Now I want to store only the interesting regions, stored in 'found', 
# and prune unnecessary bounding boxes 

boxes = cv2.groupRectangles(found, 1, 2) # But I get an error here 

错误是:

boxes = cv2.groupRectangles(found, 1, 2) 
TypeError: Layout of the output array rectList is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels) 

怎么了? 我在另一段代码中做了非常类似的事情,没有发现任何错误。这是无差错的代码:

inboxes = sio.loadmat('C:\\PATH_TO_MY_FILE\\boxes.mat') 
boxes = np.array(inboxes['boxes']) 
pruned_boxes = cv2.groupRectangles(boxes.tolist(), 100, 300) 

我可以看到的唯一区别是,boxes是numpy的阵列,其余然后转换为一个列表。但在我有问题的代码中,found已经是一个列表。

回答

4

的解决方案是found转换到一个numpy的数组,然后把它人们重新进入名单:

found = np.array(found) 
boxes = cv2.groupRectangles(found.tolist(), 1, 2) 
21

自己的解决方案是简单地问原数组的副本...(神&加里·布拉德斯基知道为什么...)

im = dbimg[i] 
bb = boxes[i] 
m = im.transpose((1, 2, 0)).astype(np.uint8).copy() 
pt1 = (bb[0],bb[1]) 
pt2 = (bb[0]+bb[2],bb[1]+bb[3]) 
cv2.rectangle(m,pt1,pt2,(0,255,0),2) 
+3

简单地复制数组为我工作的一个类似的错误,以及。 –

+0

可以证实这一点,似乎没有明显的区别,寿。 – Pwnna

+0

此解决方案适用于由cv2.ellipse()函数产生的类似错误 – DanGoodrick

0

opencv的似乎是问题的绘制与NumPy有数据类型np.int64,这是通过诸如np.array和返回的默认数据类型数组:

>>> 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 "<stdin>", line 1, in <module> 
TypeError: Layout of the output array img is incompatible with cv::Mat (step[ndims-1] != elemsize or step[1] != elemsize*nchannels) 

的解决方案是将数组转换为np.int32第一:

>>> 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)