Ejemplo n.º 1
0
    def process(self, data: Data):

        prob = np.random.rand()
        if self.params['probability'] < prob:
            pass
        else:
            if self.params['axis'] == 'horizontal':
                data.data['image'] = cv2.flip(data.data['image'], 1)

            if self.params['axis'] == 'vertical':
                data.data['image'] = cv2.flip(data.data['image'], 0)

            if self.params['axis'] == 'both':
                data.data['image'] = cv2.flip(data.data['image'], -1)
Ejemplo n.º 2
0
    def apply_augmentations(self):
        self.cfg_file = open('augmentation_cfg.yml')
        self.cfg = yaml.load(self.cfg_file, Loader=yaml.FullLoader)
        self.pipelines = []
        self.configure_pipeline()
        if len(self.img_paths) != 0:
            counter = 1
            for img_path in self.img_paths:
                image = cv2.imread(img_path)
                image_name = os.path.basename(img_path)
                self.text_out.append("Processing  " + image_name)
                scaled_counter = scale(
                    counter,
                    [0, len(self.img_paths) * len(self.pipelines) - 1],
                    [0, 100])
                self.pbar.setValue(int(scaled_counter))
                for pipeline in self.pipelines:
                    pipeline.execute(
                        Data(image=image.copy(),
                             file_path=img_path,
                             output_dir=self.output_img_dir,
                             count=counter,
                             applied_augmentations=[]))
                    counter += 1

                QApplication.processEvents()
        else:
            pass
        self.text_out.append("\n")
        self.apply_aug_button.setChecked(False)
Ejemplo n.º 3
0
    def process(self, data: Data):
        input_img = data.data['image']
        if input_img.ndim == 3:
            h, w, c = input_img.shape
        elif input_img.ndim == 2:
            h, w = input_img.shape

        prob = np.random.rand()  # returns a random nb between 0 and 1
        if self.params['probability'] < prob:
            pass
        else:
            while True:
                s = np.random.uniform(self.params['min_prop'],
                                      self.params['max_prop']) * h * w
                r = np.random.uniform(self.params['min_ratio'],
                                      self.params['max_ratio'])
                w1 = int(np.sqrt(s / r))
                h1 = int(np.sqrt(s * r))
                left = np.random.randint(0, w)
                top = np.random.randint(0, h)

                if left + w1 <= w and top + h1 <= h:
                    break

            if input_img.ndim == 3:
                col = np.random.uniform(0, 255, (h1, w1, c))
            if input_img.ndim == 2:
                col = np.random.uniform(0, 255, (h1, w1))

            input_img[top:top + h1, left:left + w1] = col
            data.data['image'] = input_img
Ejemplo n.º 4
0
 def process(self, data: Data):
     img = data.data['image'].copy()
     img = img.astype('float32')
     if self.params['gain'] != 0:
         img = (img * self.params['gain'] + self.params['bias']).clip(
             0.0, 255.0)
     data.data['image'] = img.astype('uint8')
Ejemplo n.º 5
0
    def process(self, data: Data):
        img = data.data['image'].copy()

        inv_gamma = 1.0 / self.params['gamma']
        table = np.array([((i / 255.0)**inv_gamma) * 255
                          for i in np.arange(0, 256)])
        corrected_image = cv2.LUT(img.astype(np.uint8), table.astype(np.uint8))

        data.data['image'] = corrected_image
Ejemplo n.º 6
0
 def process(self, data: Data):
     img = data.data['image'].astype('uint8')
     d = self.params['d']
     sigma_color = self.params['sigma_color']
     sigma_space = self.params['sigma_space']
     img = cv2.bilateralFilter(src=img,
                               d=d,
                               sigmaColor=sigma_color,
                               sigmaSpace=sigma_space)
     data.data['image'] = img
Ejemplo n.º 7
0
 def process(self, data: Data):
     kernel_size = make_tuple(self.params['kernel_size'])
     img = data.data['image']
     if self.params['type'] == 'box':
         img = cv2.boxFilter(src=img, ddepth=0, ksize=kernel_size)
     if self.params['type'] == 'gaussian':
         img = cv2.GaussianBlur(src=img, ksize=kernel_size, sigmaX=0)
     if self.params['type'] == 'median':
         img = cv2.medianBlur(src=img, ksize=kernel_size[0])
     data.data['image'] = img
Ejemplo n.º 8
0
    def process(self, data: Data):
        img = data.data['image']
        h, w, c = img.shape
        if c == 1:
            img = cv2.equalizeHist(img)
        else:
            img[:, :, 0] = cv2.equalizeHist(img[:, :, 0])
            img[:, :, 1] = cv2.equalizeHist(img[:, :, 1])
            img[:, :, 2] = cv2.equalizeHist(img[:, :, 2])

        data.data['image'] = img
Ejemplo n.º 9
0
    def process(self, data: Data):
        img = data.data['image']
        if img.ndim == 3:
            h, w, c = img.shape
        elif img.ndim == 2:
            h, w = img.shape
        if self.params['type'] == "gaussian":
            mean = self.params['gaussian']['mean']
            var = self.params['gaussian']['var']
            sigma = var**0.5
            gauss = np.random.normal(mean, sigma, (h, w, c))
            gauss = gauss.reshape(h, w, c)
            noisy = img + gauss
            data.data['image'] = noisy

        if self.params['type'] == "sp":
            svsp = 0.5
            amount = self.params['sp']['amount']

            # Salt mode
            num_salt = np.ceil(amount * img.size * svsp)
            coords = [
                np.random.randint(0, i - 1, int(num_salt)) for i in img.shape
            ]
            img[tuple(coords)] = 1

            # Pepper mode
            num_pepper = np.ceil(amount * img.size * (1. - svsp))
            coords = [
                np.random.randint(0, i - 1, int(num_pepper)) for i in img.shape
            ]
            img[tuple(coords)] = 0
            data.data['image'] = img

        if self.params['type'] == "speckle":
            gauss = np.random.randn(h, w, c)
            gauss = gauss.reshape(h, w, c)
            noisy = img + img * gauss
            data.data['image'] = noisy
Ejemplo n.º 10
0
 def process(self, data: Data):
     (h, w, c) = data.data['image'].shape
     prob = np.random.rand()  # returns a random nb between 0 and 1
     if self.params['probability'] < prob:
         pass
     else:
         if self.params['random_color']:
             self.color = (np.random.randint(0, 255),
                           np.random.randint(0, 255),
                           np.random.randint(0, 255))
         tint_img = np.full((h, w, c), self.color, np.uint8)
         new_img = cv2.addWeighted(data.data['image'], 1 - self.weight,
                                   tint_img, self.weight, 0)
         data.data['image'] = new_img
Ejemplo n.º 11
0
    def process(self, data: Data):
        h, w, c = data.data['image'].shape
        prob = np.random.rand()
        if self.params['probability'] < prob:
            pass
        else:
            t_x_min, t_x_max = make_tuple(self.params['t_y_range'])
            t_y_min, t_y_max = make_tuple(self.params['t_x_range'])
            if t_y_min <= t_y_max and t_x_min <= t_x_max:
                t_y = np.random.randint(t_y_min, t_y_max)
                t_x = np.random.randint(t_x_min, t_x_max)
                T = np.float32([[1, 0, t_x], [0, 1, t_y]])

                data.data['image'] = cv2.warpAffine(data.data['image'], T,
                                                    (w, h)).astype('uint8')
Ejemplo n.º 12
0
    def process(self, data: Data):
        img = data.data['image']
        h, w, c = img.shape

        prob = np.random.rand()
        if self.params['probability'] < prob:
            pass
        else:
            shear_f_min, shear_f_max = make_tuple(
                self.params['shear_factor_range'])
            if shear_f_min <= shear_f_max:
                rand_shear_factor = np.random.uniform(shear_f_min, shear_f_max)
                SH = np.float32([[1, abs(rand_shear_factor), 0], [0, 1, 0]])
                nW = int(img.shape[1] + abs(rand_shear_factor * img.shape[0]))
                img = cv2.warpAffine(img, SH, (nW, h)).astype('uint8')
                img = cv2.resize(img, (w, h))

                data.data['image'] = img
Ejemplo n.º 13
0
    def process(self, data: Data):
        (h, w, c) = data.data['image'].shape
        (cx, cy) = (w // 2, h // 2)

        prob = np.random.rand()
        if self.params['probability'] < prob:
            pass
        else:
            min_angle, max_angle = make_tuple(self.params['angle_range'])
            if min_angle <= max_angle:
                angle = np.random.randint(min_angle, max_angle)
                M = cv2.getRotationMatrix2D((cx, cy), -angle, 1.0)
                cos = np.abs(M[0, 0])
                sin = np.abs(M[0, 1])

                nw = int((h * sin) + (w * cos))
                nh = int((h * cos) + (w * sin))

                M[0, 2] += (nw / 2) - cx
                M[1, 2] += (nh / 2) - cy

                img = cv2.warpAffine(data.data['image'], M, (nw, nh))

                data.data['image'] = img
Ejemplo n.º 14
0
    def process(self, data: Data):
        img = data.data['image']
        h, w, c = img.shape

        prob = np.random.rand()
        if self.params['probability'] < prob:
            pass
        else:
            scale_x_min, scale_x_max = make_tuple(
                self.params['scale_factor_x_range'])
            scale_y_min, scale_y_max = make_tuple(
                self.params['scale_factor_y_range'])
            if scale_x_min <= scale_x_max and scale_y_min <= scale_y_max:
                scale_x = np.random.uniform(scale_x_min, scale_x_max)
                scale_y = np.random.uniform(scale_y_min, scale_y_max)
                resize_scale_x = 1 + scale_x
                resize_scale_y = 1 + scale_y

                img = cv2.resize(img,
                                 None,
                                 fx=resize_scale_x,
                                 fy=resize_scale_y)

                data.data['image'] = img
Ejemplo n.º 15
0
 def process(self, data: Data):
     img = data.data['image']
     kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
     img = cv2.filter2D(img, -1, kernel)
     data.data['image'] = img