Пример #1
0
    def __call__(self, results):
        img, gt_bboxes, gt_labels = [
            results[k] for k in ('img', 'gt_bboxes', 'gt_labels')
        ]
        gt_dict = {}
        for bbox, label in zip(gt_bboxes, gt_labels):
            gt_dict.setdefault(label, []).append(bbox)

        if np.random.rand() < self.matting_ratio:
            ind = np.random.randint(len(self.normal_groups()))
            group_name = self.normal_groups()[ind]
            img_ = []
            for i in range(5):
                img_name = 'imgs_' + group_name + '_{}.jpg'.format(str(i))
                gray = cv2.imread(os.path.join(self.normal_path, img_name), 0)
                gray = mmcv.imresize_like(gray, img)

                raw = img[:, :, i]
                bboxes = gt_dict.get(i + 1)
                if bboxes is None:
                    img_.append(gray[..., np.newaxis])
                else:
                    for bbox in bboxes:
                        xmin = int(bbox[0])
                        ymin = int(bbox[1])
                        xmax = int(bbox[2])
                        ymax = int(bbox[3])
                        gray[ymin:ymax, xmin:xmax] = cv2.addWeighted(
                            raw[ymin:ymax, xmin:xmax], self.blend_ratio,
                            gray[ymin:ymax,
                                 xmin:xmax], 1 - self.blend_ratio, 0)
                    img_.append(gray[..., np.newaxis])

            results['img'] = np.concatenate(img_, axis=-1)
        return results
 def __call__(self, results):
     matting_flag = True if np.random.rand() < self.matting_ratio else False
     if matting_flag:
         bg_img = mmcv.imread(random.choice(self.bg_img_paths))
         bg_img = mmcv.imresize_like(bg_img, results['img'])
         for key in results.get('bbox_fields', []):
             for box in results[key]:
                 bg_img[int(box[1]):int(box[3]),
                        int(box[0]):int(box[2]), :] = results['img'][
                            int(box[1]):int(box[3]),
                            int(box[0]):int(box[2]), :]
         results['img'] = bg_img
     return results
Пример #3
0
 def __call__(self, results):
     matting = True if np.random.rand() < self.matting_ratio else False
     if matting:
         template_no = np.random.randint(len(self.template_list))
         template_im_name = self.template_list[template_no]
         img_temp = mmcv.imread(template_im_name)
         img_temp = mmcv.imresize_like(img_temp, results['img'])
         results['concat_img'] = img_temp
         for bbox in results['gt_bboxes']:
             xmin = int(bbox[0])
             ymin = int(bbox[1])
             xmax = int(bbox[2])
             ymax = int(bbox[3])
             beta = np.random.uniform(0.5, 0.8)
             img_temp[ymin: ymax, xmin: xmax, :] = cv2.addWeighted(results['img'][ymin: ymax, xmin: xmax, :], beta,
                                                                   img_temp[ymin: ymax, xmin: xmax, :], 1-beta, 1)
         results['img'] = img_temp
     else:
         results['concat_img'] = None
     return results
Пример #4
0
 def __call__(self, results):
     if 'matting' not in results:
         matting = True if np.random.rand() < self.matting_ratio else False
         results['matting'] = matting
     if results['matting']:
         ind = np.random.randint(len(self.normal_imgs))
         normal_name = self.normal_imgs[ind]
         normal_img = mmcv.imread(normal_name)
         normal_img = mmcv.imresize_like(normal_img, results['img'])
         gt_bboxes_ = []
         for bbox in results['gt_bboxes']:
             xmin = int(bbox[0])
             ymin = int(bbox[1])
             xmax = int(bbox[2])
             ymax = int(bbox[3])
             if self.mode == 'fixed':
                 normal_img[ymin:ymax, xmin:xmax, :] = cv2.addWeighted(
                     results['img'][ymin:ymax, xmin:xmax, :],
                     self.blend_ratio, normal_img[ymin:ymax, xmin:xmax, :],
                     1 - self.blend_ratio, 0)
                 gt_bboxes_.append(bbox)
             elif self.mode == 'random':
                 h, w = ymax - ymin, xmax - xmin
                 img_h, img_w, _ = results['img_shape']
                 xmin = np.random.randint(0, img_w - w)
                 ymin = np.random.randint(0, img_h - h)
                 xmax = xmin + w
                 ymax = ymin + h
                 normal_img[ymin:ymax, xmin:xmax, :] = cv2.addWeighted(
                     results['img'][ymin:ymax, xmin:xmax, :],
                     self.blend_ratio, normal_img[ymin:ymax, xmin:xmax, :],
                     1 - self.blend_ratio, 0)
                 gt_bboxes_.append([xmin, ymin, xmax, ymax])
             else:
                 raise NotImplementedError
         results['img'] = normal_img
         results['gt_bboxes'] = np.array(gt_bboxes_, dtype=np.float32)
     return results
Пример #5
0
'''
MMCV进行图片变换
参考:https://mmcv.readthedocs.io/en/latest/image.html#resize
'''
import mmcv
import numpy as np

img = mmcv.imread("cluo.jpg")
dst_img = mmcv.imread("boy.jpg")

# ============Resize===============
# resize to a given size
mmcv.imresize(img, (1000, 600), return_scale=True)

# resize to the same size of another image
mmcv.imresize_like(img, dst_img, return_scale=False)

# resize by a ratio
mmcv.imrescale(img, 0.5)

# resize so that the max edge no longer than 1000, short edge no longer than 800
# without changing the aspect ratio
mmcv.imrescale(img, (1000, 800))


# =============Rotate==============
# rotate the image clockwise by 30 degrees.
img_ = mmcv.imrotate(img, 30)

# rotate the image counterclockwise by 90 degrees.
img_ = mmcv.imrotate(img, -90)
Пример #6
0
 def test_imresize_like(self):
     a = np.zeros((100, 200, 3))
     resized_img = mmcv.imresize_like(self.img, a)
     assert resized_img.shape == (100, 200, 3)
Пример #7
0
    def __call__(self, results):
        img, gt_bboxes, gt_labels = [
            results[k] for k in ('img', 'gt_bboxes', 'gt_labels')
        ]
        img_h, img_w, _ = results['img_shape']
        if img_h > 1000:
            gt_lst = self.all_gt_bboxes()

            if np.random.rand() < self.keep_ratio:
                gt_bboxes_ = gt_bboxes.tolist()
                gt_labels_ = gt_labels.tolist()
            else:
                ind = np.random.randint(len(self.normal_imgs))
                normal_path = self.normal_imgs[ind]
                normal_img = mmcv.imread(normal_path)
                normal_img = mmcv.imresize_like(normal_img, img)
                img = normal_img
                gt_bboxes_, gt_labels_ = [], []

            defects = random.choices(gt_lst, k=self.max_per_img)
            defects = sorted(defects,
                             key=(lambda x: x[-1] * x[-2]),
                             reverse=True)
            for det in defects:
                defect_img = cv2.imread(os.path.join(self.img_path, det[0]))
                label = det[1]
                x, y, w, h = list(map(int, det[2:]))
                defect = defect_img[y:y + h, x:x + w, :]
                mode = np.random.randint(3)
                # scale
                if mode == 0:
                    defect = self.bbox_scale(defect)
                    h, w = defect.shape[:2]
                # rotate
                elif mode == 1:
                    ctx = x + w * 0.5
                    cty = y + h * 0.5
                    corners = np.hstack(
                        (x, y, x + w, y, x, y + h, x + w, y + h))
                    h, w, angle = self.bbox_rotate(corners, h, w)
                    rotated_img = mmcv.imrotate(defect_img,
                                                -angle,
                                                center=(ctx, cty))
                    x1 = int(ctx - w * 0.5)
                    y1 = int(cty - h * 0.5)
                    x2 = x1 + w
                    y2 = y1 + h
                    defect = rotated_img[y1:y2, x1:x2, :]
                try:
                    xmin = np.random.randint(self.left_border,
                                             img_w - self.right_border - w)
                    ymin = np.random.randint(self.top_border,
                                             img_h - self.bottom_border - h)
                    xmax = xmin + w
                    ymax = ymin + h
                    img[ymin:ymax, xmin:xmax, :] = cv2.addWeighted(
                        defect, 1, img[ymin:ymax, xmin:xmax, :], 0, 0)
                    gt_bboxes_.append([xmin, ymin, xmax, ymax])
                    gt_labels_.append(label)
                except:
                    continue

            results['img'] = img
            results['gt_bboxes'] = np.array(gt_bboxes_, dtype=np.float32)
            results['gt_labels'] = np.array(gt_labels_, dtype=np.int64)
        return results