Python OpenCV类型错误:输出数组的布局与cv::M不兼容

2024-10-01 15:28:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里使用选择性搜索:http://koen.me/research/selectivesearch/ 这就提供了对象可能位于的感兴趣区域。我想做一些处理,只保留一些区域,然后删除重复的边界框,最后有一个整洁的边界框集合。为了丢弃不需要的/重复的边界框区域,我使用opencv的grouprectangles函数进行修剪。

从上面链接中的“选择性搜索算法”中从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已经是一个列表。


Tags: andthein区域isarray边界candidates
3条回答

另一个原因可能是数组不连续。使它相邻也可以解决这个问题

image = np.ascontiguousarray(image, dtype=np.uint8)

我自己的解决方案是简单地询问原始数组的副本…(天哪,加里布拉斯基知道为什么…)

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)  

解决方案是首先将found转换为numpy数组,然后将其恢复为列表:

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

相关问题 更多 >

    热门问题