在子图中显示来自函数的图像
Show images from a function in a subplot
我有一个函数 (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'{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() 显示图像?
您必须修改您的函数,使其适用于特定的 Axes 实例,而不是每次都创建一个新图形。
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}')
我有一个函数 (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'{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() 显示图像?
您必须修改您的函数,使其适用于特定的 Axes 实例,而不是每次都创建一个新图形。
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}')