【问题标题】:Show images from a function in a subplot在子图中显示来自函数的图像
【发布时间】:2020-10-26 01:15:24
【问题描述】:

我有一个函数 (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() 显示图像?

【问题讨论】:

    标签: matplotlib seaborn visualization


    【解决方案1】:

    您必须修改您的函数,使其适用于特定的 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}')
    

    【讨论】:

      猜你喜欢
      • 2017-04-29
      • 2019-04-15
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 2019-06-18
      • 2013-05-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多