gpt4 book ai didi

python - 裁剪和存储图像集合的边界框图像区域?

转载 作者:行者123 更新时间:2023-12-02 17:21:53 27 4
gpt4 key购买 nike

当前代码旨在为文件夹中的一组图像裁剪和存储多个边界框图像区域。裁剪的边界框图像区域存储到不同的文件夹。一共有100张图片,每张图片有多个bounding box。 CSV 文件包含每个给定图像的多个边界框坐标。代码如图所示:

import pandas as pd
import cv2
import numpy as np
import glob
import os

filenames = glob.glob("folder/abnormal/*.png")
filenames.sort()
images = [cv2.imread(img) for img in filenames]
print(images)
df = pd.read_csv('abnormal.csv')

for img in images:
for i in range(len(df)):
name = df.loc[i]['patientId']
start_point = (df.loc[i]['x_dis'],df.loc[i]['y_dis'])
end_point = (df.loc[i]['x_dis']+df.loc[i]['width_dis'],df.loc[i]['y_dis']+df.loc[i]['height_dis'])
crop = img[df.loc[i]['y_dis']:df.loc[i]['y_dis']+df.loc[i]['height_dis'],
df.loc[i]['x_dis']:df.loc[i]['x_dis']+df.loc[i]['width_dis']]
cv2.imwrite("abnormal/crop_{0}.png".format(i), crop)

在运行上面的代码时,循环无限期地继续。发生这种情况时,所有裁剪都相对于 image1 的边界框图像区域,然后所有存储的裁剪都相对于 image2 的边界框图像区域进行转换,依此类推。需要的是每个图像的多个框区域,并裁剪和存储一次。图像以名称 Patient*.png (patient1.png) 或 Patient*.*.png (patient1_1.png) 开头。

最佳答案

以下代码片段应该可以完成这项工作:

filenames = glob.glob("folder/abnormal/*.png")
filenames.sort()
df = pd.read_csv('abnormal.csv')
im_csv_np = df.loc[:,"patientId"].values

for f in filenames:
img = cv2.imread(f)
img_name = f.split(os.sep)[-1]
idx = np.where(im_csv_np == img_name)
if idx[0].shape[0]: # if there is a match shape[0] should 1, if not 0
for i in idx:
name = df.loc[i]['patientId']
start_point = (df.loc[i]['x_dis'],df.loc[i]['y_dis'])
end_point = (df.loc[i]['x_dis']+df.loc[i]['width_dis'],df.loc[i]['y_dis']+df.loc[i]['height_dis'])
crop = img[df.loc[i]['y_dis']:df.loc[i]['y_dis']+df.loc[i]['height_dis'],
df.loc[i]['x_dis']:df.loc[i]['x_dis']+df.loc[i]['width_dis']]
cv2.imwrite("abnormal/crop_{0}.png".format(i), crop)

关于python - 裁剪和存储图像集合的边界框图像区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61831083/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com