首页 > 解决方案 > 使用 python 从图像列表中裁剪多个部分

问题描述

这是我的代码

#crop images
import numpy as np # linear algebra
import xml.etree.ElementTree as ET # for parsing XML
import matplotlib.pyplot as plt # to show images
from PIL import Image # to read images
import os
import glob

root_images="../img"
root_annots="../xmls"

all_images=os.listdir("../img")
print(f"Total images : {len(all_images)}")

breeds = glob.glob('../xmls')
annotation=[]
for b in breeds:
   annotation+=glob.glob(b+"/*")
print(f"Total annotation : {len(annotation)}")

breed_map={}
for annot in annotation:
   breed=annot.split("../")[-2]
   index=breed.split("-")[0]
   breed_map.setdefault(index,breed)

print(f"Total Breeds : {len(breed_map)}")

def bounding_box(image):
   bpath=root_annots+"/"+str(image.split(".")[0]+".xml")
   tree = ET.parse(bpath)
   root = tree.getroot()
   objects = root.findall('object')
   
   for o in objects:
       bndbox = o.find('bndbox') # reading bound box
       xmin = int(bndbox.find('xmin').text)
       ymin = int(bndbox.find('ymin').text)
       xmax = int(bndbox.find('xmax').text)
       ymax = int(bndbox.find('ymax').text)
    
    
   return (xmin,ymin,xmax,ymax)

plt.figure(figsize=(10,10))
bbox=[]
for i,image in enumerate(all_images):
   bbox=bounding_box(image) 
   print(box)  
   im=Image.open(os.path.join(root_images,image))
   im=im.crop(bbox)
   im.save('../result_imgs/{}.jpeg'.format(i,im)) 

这段代码适用于图像列表,但它只裁剪每个图像的一个边界框,而 XML 文件中存在多个边界框。但我想从图像列表中裁剪每个图像的多个边界框。那么有人可以帮我解决这个问题吗?谢谢。

标签: pythonimagepython-imaging-librarycrop

解决方案


您的 bounding_box(image) 函数仅返回 1 个边界框。(每次为给定图像找到一个新的边界框时,它都会覆盖前一个)我想你应该在 bounding_box() 函数中做这样的事情:

bboxes = [] #a list of all the bounding boxes for an image
for o in objects:
       bndbox = o.find('bndbox') # reading bound box
       xmin = int(bndbox.find('xmin').text)
       ymin = int(bndbox.find('ymin').text)
       xmax = int(bndbox.find('xmax').text)
       ymax = int(bndbox.find('ymax').text)
       bboxes.append(xmin,ymin,xmax,ymax)
return(bboxes)

并在您的图像循环中:

bboxes=bounding_boxes(image) 
j = 0
for bbox in bboxes:
   im=Image.open(os.path.join(root_images,image))
   im=im.crop(bbox)
   im.save('../result_imgs/{}.jpeg'.format(i,im,j))
   j += 1

推荐阅读