【发布时间】:2019-10-13 17:15:43
【问题描述】:
我有一个 csv 数据框如下:
filename width height class xmin ymin xmax ymax
0 1.jpg 2048 1251 1 706 513 743 562
1 10.jpg 1600 980 1 715 157 733 181
2 11.jpg 2828 1828 1 460 1530 482 1557
3 12.jpg 1276 1754 1 846 517 878 563
....
19 10.jpg 1600 980 1 428 83 483 145
我想为每张图片获取蒙版。如果每个图像只有一个框,我已经成功获得它们,但是有些图像有多个边界框(例如 10.jpg)。我怎样才能将该边界框添加到蒙版?
到目前为止,我的代码如下(如果图像有 1 行,效果很好):
for idimage in annotations['filename']:
img = cv2.imread('images/'+idimage)
x1 = annotations[annotations['filename'] == idimage]['xmin'][0]
y1 = annotations[annotations['filename'] == idimage]['ymin'][0]
x2 = annotations[annotations['filename'] == idimage]['xmax'][0]
y2 = annotations[annotations['filename'] == idimage]['ymax'][0]
mask = np.zeros((img.shape[0],img.shape[1])).astype('uint8')
mask[y1:y2, x1:x2] = 1
mask = cv2.imwrite('mask/'+idimage,mask)
谢谢!
【问题讨论】:
标签: python-3.x pandas image numpy annotations