我的想法是使用给定图像的多个异常区域的边界框坐标,并裁剪这些区域以保存到单独的文件夹中。我已经编写了下面所示的代码,用于裁剪单个图像的多个边界框坐标,但是我也得到了我需要摆脱的边界框。
import pandas as pd
import cv2
import numpy as np
df = pd.read_csv('excel1.csv')
image = cv2.imread('image2.png')
im_name = 'image2.png'
for i in range(len(df)):
name = df.loc[i]['filename']
if name == im_name:
start_point = (df.loc[i]['x'],df.loc[i]['y'])
end_point = (df.loc[i]['x']+df.loc[i]['width'],df.loc[i]['y']+df.loc[i]['height'])
color = (128, 0, 0)
thickness = 2
image = cv2.rectangle(image, start_point, end_point, color, thickness)
crop = image[df.loc[i]['y']:df.loc[i]['y']+df.loc[i]['height'],
df.loc[i]['x']:df.loc[i]['x']+df.loc[i]['width']]
cv2.imwrite("cropped/crop_{0}.png".format(i), crop)
cv2.imwrite('bb.png', image)