Advertisement

Mask RCNN代码修改笔记

阅读量:

*****

  • 1 对掩膜的颜色进行调整
  • 2 移除对象预选框和标签项
  • 3 剔除掩膜四周多余的边距线
  • 4 针对选定的具体类别执行掩膜处理
  • 5 首先调整保存图片的尺寸设置;其次确保生成的图像无多余空白边缘

1 掩码颜色修改

其中image[:, :, c] * (1 - alpha) + alpha * color[c] * 255为掩码的颜色RGB值
修改为0即为黑色

复制代码
    def apply_mask(image, mask, color, alpha=0.5):
    """Apply the given mask to the image.
    """
    for c in range(3):
        image[:, :, c] = np.where(mask == 1,
                                  image[:, :, c] *
                                  (1 - alpha) + alpha * color[c] * 255,
                                  image[:, :, c])
    return image
在这里插入图片描述

2 取消对象预选框和标签

注释掉 visualiz.py 的 display_instances 函数中的以下代码

复制代码
    def apply_mask(image, mask, color, alpha=0.5):
    """Apply the given mask to the image.
    """
    for c in range(3):
        image[:, :, c] = np.where(mask == 1,
                                  image[:, :, c] *
                                  (1 - alpha) + alpha * color[c] * 255,
                                  image[:, :, c])
    return image
在这里插入图片描述

3 去掉掩码四周的边缘线

修改 visualiz.py 的 display_instances 函数中的代码
edgecolor=“none”

复制代码
    for verts in contours:
            # Subtract the padding and flip (y, x) to (x, y)
            verts = np.fliplr(verts) - 1
            p = Polygon(verts, facecolor="none", edgecolor="none")
            //p = Polygon(verts, facecolor="none", edgecolor=color)
            ax.add_patch(p)
在这里插入图片描述

4 对特定的类进行掩码操作

调整原先编写的可视化代码
不要将visualize.py直接添加到主函数中,并应在display_instance之前导入visualize模块。
如果需要处理不同类,则应将person替换为class_names中的其他类别名。

复制代码
    class_id = class_names.index('person')
    if class_names.index('person') in r['class_ids']:
    k = list(np.where(r['class_ids'] == class_names.index('person'))[0])
    
    r['scores'] = np.array([r['scores'][i] for i in k])
    
    r['rois'] = np.array([r['rois'][i] for i in k])
    
    print(r['masks'].shape)
    r['masks'] = r['masks'].transpose((2,1,0))
    r['masks'] = np.array([r['masks'][i] for i in k])
    r['masks'] = r['masks'].transpose((2,1,0))
    print(r['masks'].shape)
    
    r['class_ids'] = np.array([r['class_ids'][i] for i in k])
    display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
在这里插入图片描述

5 修改保存图片的尺寸及保存图片无白边

  1. visualiz.py

1 修改display_instances 函数中的 figsize=(a , b)
------ 其中 a = 图高/100 b = 图宽/100

复制代码
    def display_instances(count,image, boxes, masks, class_ids, class_names,
                      scores=None, title="",
                      figsize=(6.4, 4.8), ax=None,
                      show_mask=True, show_bbox=True,
                      colors=None, captions=None):

2 调整display_instances函数内的相关代码块 ------ 移除注释仅保留ax.axis('off')这条指令

复制代码
    # Show area outside image boundaries.
    # height, width = image.shape[:2]
    # ax.set_ylim(height + 10, -10)
    # ax.set_xlim(-10, width + 10)
    # ax.axis('off')
    # ax.set_title(title)
    ax.axis('off')

3 优化display_instances 函数中的相关代码
------ 其中用于设置图像尺寸的函数为:fig.set_size_inches(642/15,482/15)
------ 将其中一项参数从642/15 替换成自己的图像宽度
------ 另一项参数从482/15 替换成自己的图像高度

复制代码
      if auto_show:
     # plt.savefig("/home/zxy/123456.jpg")
     fig = plt.gcf()
     fig.set_size_inches(640 / 100.0, 480 / 100.0)
     plt.gca().xaxis.set_major_locator(plt.NullLocator())
     plt.gca().yaxis.set_major_locator(plt.NullLocator())
     plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0)
     plt.margins(0, 0)
    
     plt.savefig("../test_results/%3s.png" % (str(count[0:17])))
     plt.close()
     #plt.show()
在这里插入图片描述

参考

全部评论 (0)

还没有任何评论哟~