裁剪和存储图像集合的边界框图像区域?
Cropping and storing bounding box image regions for a collection of images?
当前代码旨在为文件夹中的一组图像裁剪和存储多个边界框图像区域。裁剪的边界框图像区域存储到不同的文件夹中。一共有100张图片,每张图片都有多个边界框。 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 的边界框图像区域进行转换,依此类推。需要的是每个图像的多个框区域以及裁剪和存储的 once.The 图像以名称 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)
当前代码旨在为文件夹中的一组图像裁剪和存储多个边界框图像区域。裁剪的边界框图像区域存储到不同的文件夹中。一共有100张图片,每张图片都有多个边界框。 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 的边界框图像区域进行转换,依此类推。需要的是每个图像的多个框区域以及裁剪和存储的 once.The 图像以名称 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)