Ejemplo n.º 1
0
    def _shift_scale_aug(self, image, bbox, crop_bbox, size):
        ''' 对具有上下文信息的gt bbox进行位移和缩放调整,然后输出的bbox,和对应的图像区域
        :param image:
        :param bbox:  带有上下文信息的box(gt值),在crop511坐标系下的坐标
        :param crop_bbox: 要crop的bbox位置信息 127*127或者255*255
        :param size: 期望crop出来的区域尺寸,网络输入时模板大小127*127,或者搜索区域大小255*255
        :return:返回的图像,是按照增强后的crop_box扣取出的roi图像区域,返回的bbox是gt信息也做相应调整后并转化到crop图像坐标系下的位置信息
        '''
        im_h, im_w = image.shape[:2]

        # adjust crop bounding box
        crop_bbox_center = corner2center(crop_bbox)          #对要crop输出的box进行大小调整和位移调整
        if self.scale:
            scale_x = (1.0 + Augmentation.random() * self.scale)
            scale_y = (1.0 + Augmentation.random() * self.scale)
            h, w = crop_bbox_center.h, crop_bbox_center.w
            scale_x = min(scale_x, float(im_w) / w)         #对要crop输出的box的w,h进行调整,取最小值是为了上搜索区域w,h不要超过图像区域
            scale_y = min(scale_y, float(im_h) / h)
            crop_bbox_center = Center(crop_bbox_center.x,
                                      crop_bbox_center.y,
                                      crop_bbox_center.w * scale_x,
                                      crop_bbox_center.h * scale_y)

        crop_bbox = center2corner(crop_bbox_center)

        if self.shift:
            sx = Augmentation.random() * self.shift             #siamese rpn++ 论文中讨论了shift最大范围的时候能够一定程度上解决网络学习过程中的位置偏见问题
            sy = Augmentation.random() * self.shift
           # print("shift", self.shift,sx,sy)
            x1, y1, x2, y2 = crop_bbox

            sx = max(-x1, min(im_w - 1 - x2, sx))   #min(im_w - 1 - x2, sx) 保证x2+sx不会超出图像右边界,也就是即使平移搜索区域,右边也不要超出右边图像边界,max(-x1,xxx)是保证x1+xxx不会小鱼0,也就是即使平移搜索区域,左边也不会超出左边图像边界
            sy = max(-y1, min(im_h - 1 - y2, sy))

            crop_bbox = Corner(x1 + sx, y1 + sy, x2 + sx, y2 + sy)

        # adjust target bounding box  要crop的box的变换上面已经确定,这里需要将他的gt信息也同样做调整
        x1, y1 = crop_bbox.x1, crop_bbox.y1
        # 以要crop输出的box的左上角为参考点,计算bbox新的坐标,也就是相应得修改gt的信息,与要crop的内容保持一致
        # 输出的bbox是相对与127*127或者255*255图像下的坐标,
        bbox = Corner(bbox.x1 - x1, bbox.y1 - y1,
                      bbox.x2 - x1, bbox.y2 - y1)

        if self.scale:
            bbox = Corner(bbox.x1 / scale_x, bbox.y1 / scale_y,
                          bbox.x2 / scale_x, bbox.y2 / scale_y)

        image = self._crop_roi(image, crop_bbox, size)     #扣取出要crop的区域
        return image, bbox
Ejemplo n.º 2
0
 def _flip_aug(self, image, bbox):
     '''
     :param image:
     :param bbox:
     :return:  左右翻转
     '''
     image = cv2.flip(image, 1)
     width = image.shape[1]
     bbox = Corner(width - 1 - bbox.x2, bbox.y1,
                   width - 1 - bbox.x1, bbox.y2)
     return image, bbox
Ejemplo n.º 3
0
    def _shift_scale_aug(self, image, bbox, crop_bbox, size):
        im_h, im_w = image.shape[:2]

        # adjust crop bounding box
        crop_bbox_center = corner2center(crop_bbox)
        if self.scale:
            scale_x = (1.0 + Augmentation.random() * self.scale)
            scale_y = (1.0 + Augmentation.random() * self.scale)
            h, w = crop_bbox_center.h, crop_bbox_center.w
            scale_x = min(scale_x, float(im_w) / w)
            scale_y = min(scale_y, float(im_h) / h)
            crop_bbox_center = Center(crop_bbox_center.x,
                                      crop_bbox_center.y,
                                      crop_bbox_center.w * scale_x,
                                      crop_bbox_center.h * scale_y)

        crop_bbox = center2corner(crop_bbox_center)
        if self.shift:
            sx = Augmentation.random() * self.shift
            sy = Augmentation.random() * self.shift

            x1, y1, x2, y2 = crop_bbox

            sx = max(-x1, min(im_w - 1 - x2, sx))
            sy = max(-y1, min(im_h - 1 - y2, sy))

            crop_bbox = Corner(x1 + sx, y1 + sy, x2 + sx, y2 + sy)

        # adjust target bounding box
        x1, y1 = crop_bbox.x1, crop_bbox.y1
        bbox = Corner(bbox.x1 - x1, bbox.y1 - y1,
                      bbox.x2 - x1, bbox.y2 - y1)

        if self.scale:
            bbox = Corner(bbox.x1 / scale_x, bbox.y1 / scale_y,
                          bbox.x2 / scale_x, bbox.y2 / scale_y)

        image = self._crop_roi(image, crop_bbox, size)
        return image, bbox
Ejemplo n.º 4
0
 def _flip_aug(self, image, bbox):
     image = cv2.flip(image, 1)  # 水平翻转
     width = image.shape[1]
     bbox = Corner(width - 1 - bbox.x2, bbox.y1, width - 1 - bbox.x1,
                   bbox.y2)
     return image, bbox