Matplotlib 在子批中显示函数的图像
我有一个函数(sample_show),它可以显示一个带有边框的图像,这很好Matplotlib 在子批中显示函数的图像,matplotlib,seaborn,visualization,Matplotlib,Seaborn,Visualization,我有一个函数(sample_show),它可以显示一个带有边框的图像,这很好 def sample_show(image_id=None): unique_imgs = df.image_id.unique() if image_id is None: idx = random.randrange(len(unique_imgs)) img_id = unique_imgs[idx] img = cv2.imread(f'{TRA
def sample_show(image_id=None):
unique_imgs = df.image_id.unique()
if image_id is None:
idx = random.randrange(len(unique_imgs))
img_id = unique_imgs[idx]
img = cv2.imread(f'{TRAIN_DIR}{img_id}.jpg')
img_df = df[df.image_id == img_id].copy()
print('Total bounding boxes = {}'.format(img_df.shape[0]))
for i in range(img_df.shape[0]):
start_point = (img_df.iloc[i,:]['x'], img_df.iloc[i,:]['y'])
end_point = (img_df.iloc[i,:]['x']+img_df.iloc[i,:]['w'], img_df.iloc[i,:]['y']+img_df.iloc[i,:]['h'])
img = cv2.rectangle(img, start_point, end_point, 255, 2)
plt.figure(figsize=(15,15))
sns.set_style('white')
plt.imshow(img)
plt.title(f'{img_id}')
现在,我想在子地块上显示这些图像
f, axes = plt.subplots(4, 1, figsize=(10, 40), sharex=True)
sns.despine(left=True)
for i in range(4):
...
有没有办法在子地块上使用sample_show()显示图像?您必须修改函数,使其在特定轴实例上工作,而不是每次都创建新图形
def sample_show(image_id=None, ax=None):
if ax is None:
ax = plt.gca()
unique_imgs = df.image_id.unique()
if image_id is None:
idx = random.randrange(len(unique_imgs))
img_id = unique_imgs[idx]
img = cv2.imread(f'{TRAIN_DIR}{img_id}.jpg')
img_df = df[df.image_id == img_id].copy()
print('Total bounding boxes = {}'.format(img_df.shape[0]))
for i in range(img_df.shape[0]):
start_point = (img_df.iloc[i,:]['x'], img_df.iloc[i,:]['y'])
end_point = (img_df.iloc[i,:]['x']+img_df.iloc[i,:]['w'], img_df.iloc[i,:]['y']+img_df.iloc[i,:]['h'])
img = cv2.rectangle(img, start_point, end_point, 255, 2)
ax.imshow(img)
ax.set_title(f'{img_id}')