python - 使用 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 文件中存在多个边界框。但我想从图像列表中裁剪每个图像的多个边界框。那么有人可以帮我解决这个问题吗?谢谢。
解决方案
您的 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
推荐阅读
- node.js - 谷歌云功能始终接收/来自 API 网关
- c++ - 为什么我在 Visual Studio 2019 和 g++11 上得到不同的输出以用于以下代码中的右值参考实现?
- git - Git status 给了我 gitignores 文件 - 为什么?
- c++ - 如何将参数从 UWP 应用程序传递到纯 C++ 控制台应用程序?
- javascript - 如何在javascript中向body元素添加一个类?
- python - PyQt5(对于 Python)中 Qt for c++ 的“QT_CONFIG()”等价物是什么?
- c# - 在 C# 中使用分隔符进行 XML 反序列化 - 没有命名空间
- python - 从列表中的字典中提取值
- r - 如何有效地从点创建线串?
- chart.js - 如何在图表js中设置饼图样式?我想改变边框颜色、边框宽度并给它们阴影