TypeError: cannot unpack non-iterable numpy.intc object
使用cv2.connectedComponentsWithStats得到bbox,其中bbox是一个二维数组:l_x, l_y, w, h, 连通图。
def mask_find_bboxs(mask):
retval, labels, stats, centroids = cv2.connectedComponentsWithStats(mask, connectivity=8) # connectivity参数的默认值为8
stats = stats[stats[:, 4].argsort()]
return stats[:-1] # 排除最外层的连通图
# 连通图的左上角坐标和连通图的宽高。
把bboxs数据写入txt文件中
def wirte_txt(bboxs, image_name):
print(bboxs[0])
x_center = bboxs[0][0] + bboxs[0][2] / 2
y_center = bboxs[0][1] + bboxs[0][3] / 2
w = bboxs[0][2]
h = bboxs[0][3]
file_txt_path_name = os.path.join(file_path, image_name.split('.')[0] + '.txt')
with open(file_txt_path_name, 'w') as file_txt:
file_txt.write(str(x_center) + ' ' + str(y_center) + ' ' + str(w) + ' ' + str(h) + '\n')
file_txt.close()
如果把
//代码1
w = bboxs[0][2]
h = bboxs[0][3]
换成
//代码2
w = bboxs[0][2],h = bboxs[0][3]
则会报错:
TypeError: cannot unpack non-iterable numpy.intc object
如果使用代码2,那么w和h则会是一个tuple类型。tuple里面的为numpy.intc。其实使用代码1类型依然是numpy.intc,为什么可以赋值了呢???唉。。不懂!!!!!!!!!!
reference:
-参考1