美文网首页
目标检测中绘制矩形框

目标检测中绘制矩形框

作者: 古de莫宁 | 来源:发表于2018-09-16 16:53 被阅读0次

    1. 利用matplotlib.pyplot 画矩形框

    from matplotlib import pyplot as plt
    

    plt.Rectangle( )参数:

    plt.Rectangle(self, xy, width, height, angle=0.0, **kwargs)
    #可选参数: fill : bool, optional (whether to fill the rect)
    edgecolor: mpl color spec;  linewidth: float or None
    

    第一个参数是矩形左上角点坐标 tuple (x,y),第二个参数是矩形宽,第三个参数是矩形高

    示例:

    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.add_subplot(1,1,1)
    rect = plt.Rectangle((0.1,0.1), 0.5, 0.3, fill=False, edgecolor = 'red',linewidth=1)
    ax.add_patch(rect)
    plt.show()
    
    rect1.jpg

    补充,matplotlib中的图和子图

    在使用pyplot进行绘制图形时,是对当前的 figure 或者 axes 进行操作,所以需要先取得需要进行操作的子图,然后利用plt.plot()进行绘图,默认plt.plot( )会在最近的一个图上进行绘制

    获得当前的图: plt.gcf() 获得当前的子图: plt.gca()

    2. 目标检测中绘制 bounding boxes

    参数 Args:
    img:    (~numpy.ndarray) in RGB format with shape (3, height, width)
    bbox:   (~numpy.ndarray) shape in (R, 4), R is the number of bboxes
    label:  (~numpy.ndarray) An integer array of shape R (R, )
    score:  (~numpy.ndarray) A float array of shape (R, ), indicating the confidence of predicting
    ax:     (matplotlib.axes.Axes): visualization is displayed on this axis, if "None", new one
    Returns:    ~matplotlib.axes.Axes
    

    3. 函数 vis_bbox(img, bbox, label=None, score=None, ax=None) 原始链接

    def vis_bbox(img, bbox, label=None, score=None, ax=None):
      label_names = list(VOC_BBOX_LABEL_NAMES) + ['bg']
      # add for index `-1`
      if label is not None and not len(bbox) == len(label):
          raise ValueError('The length of label must be same as that of bbox')
      if score is not None and not len(bbox) == len(score):
          raise ValueError('The length of score must be same as that of bbox')
    
      # Returns newly instantiated matplotlib.axes.Axes object if ax is None
      ax = vis_image(img, ax=ax)
    
      # If there is no bounding box to display, visualize the image and exit.
      if len(bbox) == 0:
          return ax
    
      for i, bb in enumerate(bbox):
          xy = (bb[1], bb[0])
          height = bb[2] - bb[0]
          width = bb[3] - bb[1]
          ax.add_patch(plot.Rectangle(
              xy, width, height, fill=False, edgecolor='red', linewidth=2))
    
          caption = list()
    
          if label is not None and label_names is not None:
              lb = label[i]
                if not (-1 <= lb < len(label_names)):  # modfy here to add backgroud
                  raise ValueError('No corresponding name is given')
              caption.append(label_names[lb])
          if score is not None:
              sc = score[i]
              caption.append('{:.2f}'.format(sc))
    
          if len(caption) > 0:
              ax.text(bb[1], bb[0],
                      ': '.join(caption),
                      style='italic',
                      bbox={'facecolor': 'white', 'alpha': 0.5, 'pad': 0})
      return ax

    相关文章

      网友评论

          本文标题:目标检测中绘制矩形框

          本文链接:https://www.haomeiwen.com/subject/ikqfnftx.html