Ejemplo n.º 1
0
 def write_BM3DELBP_example(self):
     print("Producing BM3DELBP examples for:", self.image_scaled.name)
     bm3delbp = BM3DELBP.BM3DELBP(save_img=True)
     bm3delbp.describe(self.image_scaled.data, test_image=False)
     bm3delbp_noise_classifier = NoiseClassifier.NoiseClassifier(save_img=True)
     print("No noise")
     bm3delbp_noise_classifier.describe(self.image_scaled, test_image=False)
     print("Gaussian Sigma 10")
     bm3delbp_noise_classifier.describe(self.image_gauss_10, test_image=False)
     print("Gaussian Sigma 25")
     bm3delbp_noise_classifier.describe(self.image_gauss_25, test_image=False)
     print("Speckle 0.02")
     bm3delbp_noise_classifier.describe(self.image_speckle_002, test_image=False)
     print("Speckle 0.02")
     bm3delbp_noise_classifier.describe(self.image_speckle_004, test_image=False)
     print("Salt & Pepper 2%")
     bm3delbp_noise_classifier.describe(self.image_salt_pepper_002, test_image=False)
     print("Salt & Pepper 4%")
     bm3delbp_noise_classifier.describe(self.image_salt_pepper_004, test_image=False)
     # Also demonstrate SAR-BM3D filter for speckle noise
     print("SAR-BM3D Example")
     sarbm3d = SARBM3D.SARBM3DFilter(ecs=False)
     sarbm3d.connect_matlab()
     sarbm3d_filtered_002 = sarbm3d.sar_bm3d_filter(ImageUtils.convert_float32_image_uint8(self.image_speckle_002.data), self.image_speckle_002.name, L=4)
     sarbm3d_filtered_004 = sarbm3d.sar_bm3d_filter(ImageUtils.convert_float32_image_uint8(self.image_speckle_004.data), self.image_speckle_004.name, L=4)
     sarbm3d.disconnect_matlab()
     write_image(ImageUtils.convert_float32_image_uint8(sarbm3d_filtered_002), os.path.join('BM3DELBP', 'NoiseFilters'),
                 self.image_speckle_002.name + '-Speckle-Var-0.02-SARBM3D-Filtered.png')
     write_image(ImageUtils.convert_float32_image_uint8(sarbm3d_filtered_004), os.path.join('BM3DELBP', 'NoiseFilters'),
                 self.image_speckle_004.name + '-Speckle-Var-0.04-SARBM3D-Filtered.png')
Ejemplo n.º 2
0
def do_classification(settings_tuple, noise_classifier, cross_validator,
                      images):
    sigma, cutoff, a, b = settings_tuple
    sigma_val = sigma / 255
    X_dataset = []
    y_dataset = []

    for image in images:
        X_dataset.append(
            noise_classifier.describe(
                ImageUtils.add_gaussian_noise_skimage(image.data, 10),
                sigma_val, cutoff, a, b))
        y_dataset.append('gaussian')
        X_dataset.append(
            noise_classifier.describe(
                ImageUtils.add_speckle_noise_skimage(image.data, 0.02),
                sigma_val, cutoff, a, b))
        y_dataset.append('speckle')
        X_dataset.append(
            noise_classifier.describe(
                ImageUtils.add_salt_pepper_noise_skimage(image.data, 0.02),
                sigma_val, cutoff, a, b))
        y_dataset.append('salt-pepper')

    print("Training with Sigma: {}/255, Cutoff Frequency: {}, a: {}, b: {}".
          format(sigma, cutoff, a, b))
    noise_classifier = NoiseTypePredictor(X_dataset, y_dataset,
                                          cross_validator)
    test_y_all, pred_y_all = noise_classifier.begin_cross_validation()
    f1 = f1_score(test_y_all,
                  pred_y_all,
                  labels=['gaussian', 'speckle', 'salt-pepper'])
    print("Completed with F1: {} , Sigma: {}, Cutoff freq: {}, a: {}, b: {}")
    return (f1, sigma, cutoff, a, b)
    def calculate_relbp(self, image):
        """
        Calculates the RELBP descriptor (joint histogram of RELBP_CI, RELBP_NI, RELBP_RD)
        If you apply the Median filter before this, it is the MRELBP descriptor.
        If you perform noise classification + variable filters beforehand, it is the BM3DELBP descriptor.
        :param image: Image object or float32 ndarray, scaled & padded image_scaled of zero mean and unit variance.
        :return: Combined RELBP descriptor histogram
        """
        # Generate r1 and w_r1 parameter pairs depending on whether user passed list or int for each.
        relbp_ni_rd = np.array([], dtype=np.int32)
        for r, w_r in self.r_wr_scales:
            if w_r % 2 == 0:
                raise ValueError(
                    'Kernel size w_r1 must be an odd number, but an even number was provided'
                )
            if isinstance(image, np.ndarray):
                relbp_ni, relbp_rd = self.relbp_ni_rd(image, r, w_r)
            else:
                relbp_ni, relbp_rd = self.relbp_ni_rd(image.data, r, w_r)

            if self.save_img:
                if isinstance(image, np.ndarray):
                    raise ValueError(
                        'save_img set but passed as ndarray instead of DatasetManager.Image'
                    )
                else:
                    GenerateExamples.write_image(
                        ImageUtils.convert_float32_image_uint8(relbp_ni),
                        'MRELBP',
                        '{}-RELBPNI_r-{}_wr-{}.png'.format(image.name, r, w_r))
                    GenerateExamples.write_image(
                        ImageUtils.convert_float32_image_uint8(relbp_rd),
                        'MRELBP',
                        '{}-RELBPRD_r-{}_wr-{}.png'.format(image.name, r, w_r))

            relbp_ni_hist = np.histogram(relbp_ni,
                                         self.p + 2)[0].astype(dtype=np.int32)
            relbp_rd_hist = np.histogram(relbp_rd,
                                         self.p + 2)[0].astype(dtype=np.int32)
            relbp_ni_rd = np.concatenate(
                (relbp_ni_rd, relbp_ni_hist, relbp_rd_hist))

        if isinstance(image, np.ndarray):
            relbp_ci_hist = self.relbp_ci(image, self.w_c, self.padding)
        else:
            relbp_ci_hist = self.relbp_ci(image.data, self.w_c, self.padding)

        combined_histogram = np.concatenate((relbp_ci_hist, relbp_ni_rd))
        return combined_histogram
    def describe(self, image, test_image: bool):
        """
        Perform MRELBP Description for an Image
        :param image: Image object or float32 ndarray image_scaled with zero mean and unit variance. (Use ImageUtils.scale_img first)
        :param test_image: Boolean to determine if we are evaluating the test image
        :return: MRELBP descriptor histogram
        """
        if isinstance(image, DatasetManager.Image):
            if test_image:
                image_data = image.test_data
            else:
                image_data = image.data
        elif isinstance(image, np.ndarray):
            image_data = image
            if self.save_img:
                raise ValueError(
                    'save_img set but passed as ndarray instead of DatasetManager.Image'
                )
        else:
            raise ValueError('Invalid image_scaled type')

        # Zero-pad image_scaled with padding border.
        image_padded = pad(array=image_data,
                           pad_width=self.padding,
                           mode='constant',
                           constant_values=0)
        # Allocate memory for output image_scaled
        image_filtered = np.zeros(image_padded.shape, dtype=np.float32)
        # Perform median filter on image_scaled
        SharedFunctions.median_filter(image_padded, self.w_c, self.padding,
                                      image_filtered)
        # Make new Image instance to avoid overwriting input image's data

        if self.save_img:
            GenerateExamples.write_image(
                ImageUtils.convert_float32_image_uint8(image_padded), 'MRELBP',
                '{}-padded.png'.format(image.name))
            GenerateExamples.write_image(
                ImageUtils.convert_float32_image_uint8(image_filtered),
                'MRELBP', '{}-median-filtered.png'.format(image.name))
            describe_image = DatasetManager.Image(image_filtered, image.name,
                                                  None)
        else:
            describe_image = DatasetManager.Image(image_filtered, None, None)

        # Return MRELBP descriptor
        return self.calculate_relbp(describe_image)
Ejemplo n.º 5
0
    def __init__(self, path):
        """
        Generate Example images for dissertation write-up
        :param path: Image to produce example images with
        """
        self.image_path = path
        image_name = path.split(os.sep)[-1].partition('.')[0]
        #image_uint8 = cv2.imread(path, cv2.IMREAD_GRAYSCALE)

        image_uint8 = cv2.resize(cv2.imread(path, cv2.IMREAD_GRAYSCALE), (0, 0),
                                fx=0.5,
                                fy=0.5)
        # Convert from uint8 to float32 without normalizing to zero mean
        image_unscaled = ImageUtils.convert_uint8_image_float32(image_uint8)
        # Convert from uint8 to float32 while normalizing to zero mean
        image_scaled = ImageUtils.scale_uint8_image_float32(image_uint8)
        image_gauss_10 = ImageUtils.add_gaussian_noise_skimage(image_scaled, 10)
        image_gauss_25 = ImageUtils.add_gaussian_noise_skimage(image_scaled, 25)
        image_speckle_002 = ImageUtils.add_speckle_noise_skimage(image_scaled, 0.02)
        image_speckle_004 = ImageUtils.add_speckle_noise_skimage(image_scaled, 0.04)
        image_salt_pepper_002 = ImageUtils.add_salt_pepper_noise_skimage(image_scaled, 0.02)
        image_salt_pepper_004 = ImageUtils.add_salt_pepper_noise_skimage(image_scaled, 0.04)
        image_label = path.split(os.sep)[-1].partition('-')[0]
        # Generate different permutations of this sample image
        self.image_uint8 = DatasetManager.Image(image_uint8, image_name, image_label)
        self.image_unscaled = DatasetManager.Image(image_unscaled, image_name, image_label)
        self.image_scaled = DatasetManager.Image(image_scaled, image_name, image_label)
        self.image_gauss_10 = DatasetManager.Image(image_gauss_10, image_name, image_label)
        self.image_gauss_10.test_noise='gaussian'; self.image_gauss_10.test_noise_val=10
        self.image_gauss_25 = DatasetManager.Image(image_gauss_25, image_name, image_label)
        self.image_gauss_25.test_noise = 'gaussian'; self.image_gauss_25.test_noise_val = 25
        self.image_speckle_002 = DatasetManager.Image(image_speckle_002, image_name, image_label)
        self.image_speckle_002.test_noise = 'speckle'; self.image_speckle_002.test_noise_val = 0.02
        self.image_speckle_004 = DatasetManager.Image(image_speckle_004, image_name, image_label)
        self.image_speckle_004.test_noise = 'speckle'; self.image_speckle_004.test_noise_val = 0.04
        self.image_salt_pepper_002 = DatasetManager.Image(image_salt_pepper_002, image_name, image_label)
        self.image_salt_pepper_002.test_noise = 'salt-pepper'; self.image_salt_pepper_002.noise_val = 0.02
        self.image_salt_pepper_004 = DatasetManager.Image(image_salt_pepper_004, image_name, image_label)
        self.image_salt_pepper_004.test_noise = 'salt-pepper'; self.image_salt_pepper_004.noise_val = 0.04
        self.path = os.path.join(GlobalConfig.get('CWD'), 'example')

        write_image(ImageUtils.convert_float32_image_uint8(self.image_unscaled.data), None, image_name + '-unedited.png')
        write_image(ImageUtils.convert_float32_image_uint8(self.image_scaled.data), None, image_name + '-scaled.png')
Ejemplo n.º 6
0
 def apply_filter(self,
                  image_data,
                  image_name,
                  noise_prediction,
                  ecs=False):
     if noise_prediction == 'gaussian':
         # Apply BM3D filter
         image_filtered = SharedFunctions.bm3d_filter(image_data, 50 / 255)
     elif noise_prediction == 'speckle':
         # Establish connection to MATLAB Engine for SAR-BM3D filter
         sar_bm3d = SARBM3D.SARBM3DFilter(ecs)
         # Connect to MATLAB Engine for SAR-BM3D filter
         sar_bm3d.connect_matlab()
         # Apply SAR-BM3D filter. This requires images to be scaled in range [0, 255]
         image_filtered = sar_bm3d.sar_bm3d_filter(
             ImageUtils.convert_float32_image_uint8(image_data),
             image_name,
             L=6)
         try:
             # Disconnect from MATLAB Engine
             sar_bm3d.disconnect_matlab()
         except SystemError:
             # If we try to disconnect matlab after it has already disconnected (eg: crashed) this throws an exception
             pass
     elif noise_prediction == 'salt-pepper':
         # Apply Median filter. Padding is required for median filter.
         image_padded = pad(array=image_data,
                            pad_width=1,
                            mode='constant',
                            constant_values=0)
         image_filtered = np.zeros(image_padded.shape, dtype=np.float32)
         SharedFunctions.median_filter(image_padded, 3, 1, image_filtered)
         image_filtered = image_filtered[
             1:-1, 1:-1]  # Remove padding now median filter done
     elif noise_prediction == 'no-noise':
         image_filtered = image_data.copy()
     else:
         raise ValueError(
             'Noise prediction: {} does not match expected values'.format(
                 noise_prediction))
     return image_filtered
Ejemplo n.º 7
0
 def write_noise_examples(self):
     """
     Produce examples of images with noise types applied
     :return: None
     """
     print("Producing Noisy Image examples for:", self.image_scaled.name)
     write_image(ImageUtils.convert_float32_image_uint8(self.image_gauss_10.data),
                      'Noise Applied', self.image_scaled.name + '-Gaussian-Sigma-10.png')
     write_image(ImageUtils.convert_float32_image_uint8(self.image_gauss_25.data),
                      'Noise Applied', self.image_scaled.name + '-Gaussian-Sigma-25.png')
     write_image(ImageUtils.convert_float32_image_uint8(self.image_speckle_002.data),
                      'Noise Applied', self.image_scaled.name + '-Speckle-Var-0.02.png')
     write_image(ImageUtils.convert_float32_image_uint8(self.image_speckle_004.data),
                      'Noise Applied', self.image_scaled.name + '-Speckle-Var-0.04.png')
     write_image(ImageUtils.convert_float32_image_uint8(self.image_salt_pepper_002.data),
                      'Noise Applied', self.image_scaled.name + '-Salt-Pepper-2%.png')
     write_image(ImageUtils.convert_float32_image_uint8(self.image_salt_pepper_004.data),
                      'Noise Applied', self.image_scaled.name + '-Salt-Pepper-4%.png')
     print("Finished producing Noisy Image examples")
Ejemplo n.º 8
0
def tune_bm3d_filter():
    textures = []

    textures.append(os.path.join(prefix, 'blanket1', 'blanket1-a-p001.png'))
    textures.append(os.path.join(prefix, 'blanket2', 'blanket2-a-p001.png'))
    textures.append(os.path.join(prefix, 'canvas1', 'canvas1-a-p001.png'))
    textures.append(os.path.join(prefix, 'ceiling1', 'ceiling1-a-p001.png'))
    textures.append(os.path.join(prefix, 'ceiling2', 'ceiling2-a-p001.png'))
    textures.append(os.path.join(prefix, 'cushion1', 'cushion1-a-p001.png'))
    textures.append(os.path.join(prefix, 'floor1', 'floor1-a-p001.png'))
    textures.append(os.path.join(prefix, 'floor2', 'floor2-a-p001.png'))
    textures.append(os.path.join(prefix, 'grass1', 'grass1-a-p001.png'))
    textures.append(os.path.join(prefix, 'lentils1', 'lentils1-a-p001.png'))
    textures.append(os.path.join(prefix, 'linseeds1', 'linseeds1-a-p001.png'))
    textures.append(os.path.join(prefix, 'oatmeal1', 'oatmeal1-a-p001.png'))
    textures.append(
        os.path.join(prefix, 'pearlsugar1', 'pearlsugar1-a-p001.png'))
    textures.append(os.path.join(prefix, 'rice1', 'rice1.b-p001.png'))
    textures.append(os.path.join(prefix, 'rice2', 'rice2-a-p001.png'))
    textures.append(os.path.join(prefix, 'rug1', 'rug1-a-p001.png'))
    textures.append(os.path.join(prefix, 'sand1', 'sand1-a-p001.png'))
    textures.append(os.path.join(prefix, 'scarf1', 'scarf1-a-p001.png'))
    textures.append(os.path.join(prefix, 'scarf2', 'scarf2-a-p001.png'))
    textures.append(os.path.join(prefix, 'screen1', 'screen1-a-p001.png'))
    textures.append(os.path.join(prefix, 'seat1', 'seat1-a-p001.png'))
    textures.append(os.path.join(prefix, 'seat2', 'seat2-a-p001.png'))
    textures.append(
        os.path.join(prefix, 'sesameseeds1', 'sesameseeds1-a-p001.png'))
    textures.append(os.path.join(prefix, 'stone1', 'stone1-a-p001.png'))
    textures.append(os.path.join(prefix, 'stone2', 'stone2-a-p001.png'))
    textures.append(os.path.join(prefix, 'stone3', 'stone3-a-p001.png'))
    textures.append(os.path.join(prefix, 'stoneslab1',
                                 'stoneslab1-a-p001.png'))
    textures.append(os.path.join(prefix, 'wall1', 'wall1-a-p001.png'))

    sigma = [70, 120]

    diffs = [0] * len(sigma)

    for texture in textures:

        image_uint8 = cv2.resize(cv2.imread(texture, cv2.IMREAD_GRAYSCALE),
                                 (0, 0),
                                 fx=0.5,
                                 fy=0.5)
        image_scaled = ImageUtils.scale_uint8_image_float32(image_uint8)
        image_gaussian = ImageUtils.add_gaussian_noise_skimage(
            image_scaled, 10)
        image_gaussian = ImageUtils.convert_float32_image_uint8(image_gaussian)
        image_speckle = ImageUtils.add_speckle_noise_skimage(
            image_scaled, 0.02)
        image_speckle = ImageUtils.convert_float32_image_uint8(image_speckle)

        diff_index = 0

        for val in sigma:
            sigma_val = val / 255
            print("Evaluating {}/255".format(val))
            bm3d_gaussian = SharedFunctions.bm3d_filter(image_gaussian,
                                                        sigma_psd=sigma_val)
            bm3d_gaussian = ImageUtils.convert_uint8_image_float32(
                bm3d_gaussian)
            bm3d_speckle = SharedFunctions.bm3d_filter(image_speckle,
                                                       sigma_psd=sigma_val)
            bm3d_speckle = ImageUtils.convert_uint8_image_float32(bm3d_speckle)

            # noise_salt_pepper = image_scaled - homomorphic_salt_pepper
            noise_gaussian = image_scaled - bm3d_gaussian
            noise_speckle = image_scaled - bm3d_speckle

            kurtosis_gauss = kurtosis(a=noise_gaussian,
                                      axis=None,
                                      fisher=False,
                                      nan_policy='raise')
            skewness_gauss = skew(a=noise_gaussian,
                                  axis=None,
                                  nan_policy='raise')
            kurtosis_speckle = kurtosis(a=noise_speckle,
                                        axis=None,
                                        fisher=False,
                                        nan_policy='raise')
            skewness_speckle = skew(a=noise_speckle,
                                    axis=None,
                                    nan_policy='raise')

            diff = abs(kurtosis_speckle - kurtosis_gauss) + abs(
                skewness_speckle - skewness_gauss
            )  # + math.sqrt(abs(kurtosis_speckle - kurtosis_sp))
            print("sigma_psd: {}/255, diff: {}".format(val, diff))

            # Increment the difference value for that configuration index
            diffs[diff_index] += diff

            diff_index += 1

    index_best = np.argmax(diffs)
    print("best index: {}".format(index_best))
    best_diff = diffs[index_best]
    best_sigma = sigma[index_best]
    print("OVERALL BEST: sigma_val: {}/255, difference: {}".format(
        best_sigma, best_diff))

    image_uint8 = cv2.resize(cv2.imread(textures[0], cv2.IMREAD_GRAYSCALE),
                             (0, 0),
                             fx=0.5,
                             fy=0.5)
    image_scaled = ImageUtils.scale_uint8_image_float32(image_uint8)
    image_salt_pepper = ImageUtils.add_salt_pepper_noise_skimage(
        image_scaled, 0.02)
    image_salt_pepper = ImageUtils.convert_float32_image_uint8(
        image_salt_pepper)
    image_gaussian = ImageUtils.add_gaussian_noise_skimage(image_scaled, 10)
    image_gaussian = ImageUtils.convert_float32_image_uint8(image_gaussian)
    image_speckle = ImageUtils.add_speckle_noise_skimage(image_uint8, 0.02)
    image_speckle = ImageUtils.convert_float32_image_uint8(image_speckle)

    bm3d_salt_pepper = SharedFunctions.bm3d_filter(image_salt_pepper,
                                                   sigma_psd=(best_sigma /
                                                              255))
    bm3d_gaussian = SharedFunctions.bm3d_filter(image_gaussian,
                                                sigma_psd=(best_sigma / 255))
    bm3d_speckle = SharedFunctions.bm3d_filter(image_speckle,
                                               sigma_psd=(best_sigma / 255))
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(bm3d_salt_pepper), 'bm3d',
        'homomorphic-salt-pepper.png')
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(bm3d_gaussian), 'bm3d',
        'homomorphic-gaussian.png')
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(bm3d_speckle), 'bm3d',
        'homomorphic-speckle.png')

    bm3d_salt_pepper = ImageUtils.convert_uint8_image_float32(bm3d_salt_pepper)
    bm3d_gaussian = ImageUtils.convert_uint8_image_float32(bm3d_gaussian)
    bm3d_speckle = ImageUtils.convert_uint8_image_float32(bm3d_speckle)

    noise_salt_pepper = image_scaled - bm3d_salt_pepper
    noise_gaussian = image_scaled - bm3d_gaussian
    noise_speckle = image_scaled - bm3d_speckle

    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(noise_salt_pepper), 'bm3d',
        'noise_estimate_salt_pepper.png')
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(noise_gaussian), 'bm3d',
        'noise_estimate_gaussian.png')
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(noise_speckle), 'bm3d',
        'noise_estimate_speckle.png')

    kurtosis_sp = kurtosis(a=noise_salt_pepper,
                           axis=None,
                           fisher=False,
                           nan_policy='raise')
    skewness_sp = skew(a=noise_salt_pepper, axis=None, nan_policy='raise')

    kurtosis_gauss = kurtosis(a=noise_gaussian,
                              axis=None,
                              fisher=False,
                              nan_policy='raise')
    skewness_gauss = skew(a=noise_gaussian, axis=None, nan_policy='raise')

    kurtosis_speckle = kurtosis(a=noise_speckle,
                                axis=None,
                                fisher=False,
                                nan_policy='raise')
    skewness_speckle = skew(a=noise_speckle, axis=None, nan_policy='raise')

    print("Salt & Pepper: Skewness {}, Kurtosis {}".format(
        skewness_sp, kurtosis_sp))
    print("Gaussian: Skewness {}, Kurtosis {}".format(skewness_gauss,
                                                      kurtosis_gauss))
    print("Speckle: Skewness {}, Kurtosis {}".format(skewness_speckle,
                                                     kurtosis_speckle))
Ejemplo n.º 9
0
def tune_homomorphic_filter():
    textures = []

    textures.append(os.path.join(prefix, 'blanket1', 'blanket1-a-p001.png'))
    textures.append(os.path.join(prefix, 'blanket2', 'blanket2-a-p001.png'))
    textures.append(os.path.join(prefix, 'canvas1', 'canvas1-a-p001.png'))
    textures.append(os.path.join(prefix, 'ceiling1', 'ceiling1-a-p001.png'))
    textures.append(os.path.join(prefix, 'ceiling2', 'ceiling2-a-p001.png'))
    textures.append(os.path.join(prefix, 'cushion1', 'cushion1-a-p001.png'))
    textures.append(os.path.join(prefix, 'floor1', 'floor1-a-p001.png'))
    textures.append(os.path.join(prefix, 'floor2', 'floor2-a-p001.png'))
    textures.append(os.path.join(prefix, 'grass1', 'grass1-a-p001.png'))
    textures.append(os.path.join(prefix, 'lentils1', 'lentils1-a-p001.png'))
    textures.append(os.path.join(prefix, 'linseeds1', 'linseeds1-a-p001.png'))
    textures.append(os.path.join(prefix, 'oatmeal1', 'oatmeal1-a-p001.png'))
    textures.append(
        os.path.join(prefix, 'pearlsugar1', 'pearlsugar1-a-p001.png'))
    textures.append(os.path.join(prefix, 'rice1', 'rice1.b-p001.png'))
    textures.append(os.path.join(prefix, 'rice2', 'rice2-a-p001.png'))
    textures.append(os.path.join(prefix, 'rug1', 'rug1-a-p001.png'))
    textures.append(os.path.join(prefix, 'sand1', 'sand1-a-p001.png'))
    textures.append(os.path.join(prefix, 'scarf1', 'scarf1-a-p001.png'))
    textures.append(os.path.join(prefix, 'scarf2', 'scarf2-a-p001.png'))
    textures.append(os.path.join(prefix, 'screen1', 'screen1-a-p001.png'))
    textures.append(os.path.join(prefix, 'seat1', 'seat1-a-p001.png'))
    textures.append(os.path.join(prefix, 'seat2', 'seat2-a-p001.png'))
    textures.append(
        os.path.join(prefix, 'sesameseeds1', 'sesameseeds1-a-p001.png'))
    textures.append(os.path.join(prefix, 'stone1', 'stone1-a-p001.png'))
    textures.append(os.path.join(prefix, 'stone2', 'stone2-a-p001.png'))
    textures.append(os.path.join(prefix, 'stone3', 'stone3-a-p001.png'))
    textures.append(os.path.join(prefix, 'stoneslab1',
                                 'stoneslab1-a-p001.png'))
    textures.append(os.path.join(prefix, 'wall1', 'wall1-a-p001.png'))

    cutoff_vals = [
        0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 1.25, 1.5, 1.75, 2, 2.5, 3, 3.5, 5, 10
    ]
    a_vals = [0.6, 0.75, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3]
    b_vals = [0.1, 0.2, 0.4, 0.6, 0.75, 1.0, 1.25, 1.5]

    diffs = [0] * len(cutoff_vals) * len(a_vals) * len(b_vals)
    cutoff_vals_order = [0] * len(cutoff_vals) * len(a_vals) * len(b_vals)
    a_vals_order = [0] * len(cutoff_vals) * len(a_vals) * len(b_vals)
    b_vals_order = [0] * len(cutoff_vals) * len(a_vals) * len(b_vals)

    for texture in textures:

        image_uint8 = cv2.resize(cv2.imread(texture, cv2.IMREAD_GRAYSCALE),
                                 (0, 0),
                                 fx=0.5,
                                 fy=0.5)
        image_scaled = ImageUtils.scale_uint8_image_float32(image_uint8)
        # image_salt_pepper = ImageUtils.add_salt_pepper_noise_skimage(image_scaled, 0.02)
        # image_salt_pepper = ImageUtils.convert_float32_image_uint8(image_salt_pepper)
        image_gaussian = ImageUtils.add_gaussian_noise_skimage(
            image_scaled, 10)
        image_gaussian = ImageUtils.convert_float32_image_uint8(image_gaussian)
        image_speckle = ImageUtils.add_speckle_noise_skimage(
            image_scaled, 0.02)
        image_speckle = ImageUtils.convert_float32_image_uint8(image_speckle)

        diff_index = 0

        for cut in cutoff_vals:
            for a in a_vals:
                for b in b_vals:

                    # homomorphic_salt_pepper = SharedFunctions.homomorphic_filter(image_salt_pepper, cut, a, b)
                    # homomorphic_salt_pepper = ImageUtils.convert_uint8_image_float32(homomorphic_salt_pepper)
                    homomorphic_gaussian = SharedFunctions.homomorphic_filter(
                        image_gaussian, cut, a, b)
                    homomorphic_gaussian = ImageUtils.convert_uint8_image_float32(
                        homomorphic_gaussian)
                    homomorphic_speckle = SharedFunctions.homomorphic_filter(
                        image_speckle, cut, a, b)
                    homomorphic_speckle = ImageUtils.convert_uint8_image_float32(
                        homomorphic_speckle)

                    # noise_salt_pepper = image_scaled - homomorphic_salt_pepper
                    noise_gaussian = image_scaled - homomorphic_gaussian
                    noise_speckle = image_scaled - homomorphic_speckle

                    # kurtosis_sp = kurtosis(a=noise_salt_pepper, axis=None, fisher=False, nan_policy='raise')
                    # skewness_sp = skew(a=noise_salt_pepper, axis=None, nan_policy='raise')

                    kurtosis_gauss = kurtosis(a=noise_gaussian,
                                              axis=None,
                                              fisher=False,
                                              nan_policy='raise')
                    skewness_gauss = skew(a=noise_gaussian,
                                          axis=None,
                                          nan_policy='raise')

                    kurtosis_speckle = kurtosis(a=noise_speckle,
                                                axis=None,
                                                fisher=False,
                                                nan_policy='raise')
                    skewness_speckle = skew(a=noise_speckle,
                                            axis=None,
                                            nan_policy='raise')

                    diff = abs(kurtosis_speckle - kurtosis_gauss) + abs(
                        skewness_speckle - skewness_gauss
                    )  # + math.sqrt(abs(kurtosis_speckle - kurtosis_sp))
                    print("cutoff: {}, a: {}, b: {}, diff: {}".format(
                        cut, a, b, diff))

                    # Increment the difference value for that configuration index
                    diffs[diff_index] += diff
                    cutoff_vals_order[diff_index] = cut
                    a_vals_order[diff_index] = a
                    b_vals_order[diff_index] = b

                    diff_index += 1

    index_best = np.argmax(diffs)
    print("best index: {}".format(index_best))
    best_diff = diffs[index_best]
    best_cutoff = cutoff_vals_order[index_best]
    best_a = a_vals_order[index_best]
    best_b = b_vals_order[index_best]
    print("OVERALL BEST: Cutoff: {}, a: {}, b: {}, difference: {}".format(
        best_cutoff, best_a, best_b, best_diff))

    image_uint8 = cv2.resize(cv2.imread(textures[0], cv2.IMREAD_GRAYSCALE),
                             (0, 0),
                             fx=0.5,
                             fy=0.5)
    image_scaled = ImageUtils.scale_uint8_image_float32(image_uint8)
    image_salt_pepper = ImageUtils.add_salt_pepper_noise_skimage(
        image_scaled, 0.02)
    image_salt_pepper = ImageUtils.convert_float32_image_uint8(
        image_salt_pepper)
    image_gaussian = ImageUtils.add_gaussian_noise_skimage(image_scaled, 10)
    image_gaussian = ImageUtils.convert_float32_image_uint8(image_gaussian)
    image_speckle = ImageUtils.add_speckle_noise_skimage(image_uint8, 0.02)
    image_speckle = ImageUtils.convert_float32_image_uint8(image_speckle)

    homomorphic_salt_pepper = SharedFunctions.homomorphic_filter(
        image_salt_pepper, best_cutoff, best_a, best_b)
    homomorphic_gaussian = SharedFunctions.homomorphic_filter(
        image_gaussian, best_cutoff, best_a, best_b)
    homomorphic_speckle = SharedFunctions.homomorphic_filter(
        image_speckle, best_cutoff, best_a, best_b)
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(homomorphic_salt_pepper),
        'Homomorphic', 'homomorphic-salt-pepper.png')
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(homomorphic_gaussian),
        'Homomorphic', 'homomorphic-gaussian.png')
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(homomorphic_speckle),
        'Homomorphic', 'homomorphic-speckle.png')

    homomorphic_salt_pepper = ImageUtils.convert_uint8_image_float32(
        homomorphic_salt_pepper)
    homomorphic_gaussian = ImageUtils.convert_uint8_image_float32(
        homomorphic_gaussian)
    homomorphic_speckle = ImageUtils.convert_uint8_image_float32(
        homomorphic_speckle)

    noise_salt_pepper = image_scaled - homomorphic_salt_pepper
    noise_gaussian = image_scaled - homomorphic_gaussian
    noise_speckle = image_scaled - homomorphic_speckle

    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(noise_salt_pepper),
        'Homomorphic', 'noise_estimate_salt_pepper.png')
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(noise_gaussian), 'Homomorphic',
        'noise_estimate_gaussian.png')
    GenerateExamples.write_image(
        ImageUtils.convert_float32_image_uint8(noise_speckle), 'Homomorphic',
        'noise_estimate_speckle.png')

    kurtosis_sp = kurtosis(a=noise_salt_pepper,
                           axis=None,
                           fisher=False,
                           nan_policy='raise')
    skewness_sp = skew(a=noise_salt_pepper, axis=None, nan_policy='raise')

    kurtosis_gauss = kurtosis(a=noise_gaussian,
                              axis=None,
                              fisher=False,
                              nan_policy='raise')
    skewness_gauss = skew(a=noise_gaussian, axis=None, nan_policy='raise')

    kurtosis_speckle = kurtosis(a=noise_speckle,
                                axis=None,
                                fisher=False,
                                nan_policy='raise')
    skewness_speckle = skew(a=noise_speckle, axis=None, nan_policy='raise')

    print("Salt & Pepper: Skewness {}, Kurtosis {}".format(
        skewness_sp, kurtosis_sp))
    print("Gaussian: Skewness {}, Kurtosis {}".format(skewness_gauss,
                                                      kurtosis_gauss))
    print("Speckle: Skewness {}, Kurtosis {}".format(skewness_speckle,
                                                     kurtosis_speckle))
Ejemplo n.º 10
0
    def describe(self, image, test_image: bool, sigma_psd=70/255, cutoff_freq=10, a=0.75, b=1.0):
        if isinstance(image, DatasetManager.Image):
            if test_image:
                image_data = image.test_data
            else:
                image_data = image.data
        else:
            image_data = image.copy()

        # Perform BM3D Filter
        image_bm3d_filtered = SharedFunctions.bm3d_filter(image_data, 70/255)
        # Perform Homomorphic filter, Note: This requires images to be normalised in range [0, 255]
        image_scaled_255 = ImageUtils.convert_float32_image_uint8(image_data)
        cutoff, a, b = 10, 0.75, 0.1
        image_homomorphic_filtered = SharedFunctions.homomorphic_filter(image_scaled_255, cutoff, a, b)
        image_homomorphic_filtered = ImageUtils.convert_uint8_image_float32(image_homomorphic_filtered)
        # Perform Median filter. Padding is required for median filter.
        image_padded = pad(array=image_data, pad_width=1, mode='constant', constant_values=0)
        image_median_filtered = np.zeros(image_padded.shape, dtype=np.float32)
        SharedFunctions.median_filter(image_padded, 3, 1, image_median_filtered)
        image_median_filtered = image_median_filtered[1:-1, 1:-1]  # Remove padding now median filter done

        # Subtract original image from filtered image to get noise only
        bm3d_noise = image_data - image_bm3d_filtered
        homomorphic_noise = image_data - image_homomorphic_filtered
        median_noise = image_data - image_median_filtered

        if self.save_img:
            if isinstance(image, DatasetManager.Image):
                GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(image_bm3d_filtered),
                                             os.path.join('BM3DELBP', 'NoiseClassifier', 'Filtered Images'),
                                             '{}-{}-{}-BM3D-filtered.png'.format(image.name, image.test_noise,
                                                                                 image.test_noise_val))
                GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(image_homomorphic_filtered),
                                             os.path.join('BM3DELBP', 'NoiseClassifier', 'Filtered Images'),
                                             '{}-{}-{}-homomorphic-filtered-cutoff_{}-a_{}-b_{}.png'.format(image.name,
                                                                                                            image.test_noise,
                                                                                                            image.test_noise_val,
                                                                                                            cutoff, a,
                                                                                                            b))
                GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(image_median_filtered),
                                             os.path.join('BM3DELBP', 'NoiseClassifier', 'Filtered Images'),
                                             '{}-{}-{}-median-filtered.png'.format(image.name, image.test_noise,
                                                                                   image.test_noise_val))
                GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(bm3d_noise),
                                             os.path.join('BM3DELBP', 'NoiseClassifier', 'Noise Estimates'),
                                             '{}-{}-{}-BM3D-noise-estimate.png'.format(image.name, image.test_noise,
                                                                                       image.test_noise_val))
                GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(homomorphic_noise),
                                             os.path.join('BM3DELBP', 'NoiseClassifier', 'Noise Estimates'),
                                             '{}-{}-{}-homomorphic-filtered-cutoff_{}-a_{}-b_{}.png'.format(image.name,
                                                                                                            image.test_noise,
                                                                                                            image.test_noise_val,
                                                                                                            cutoff, a,
                                                                                                            b))
                GenerateExamples.write_image(ImageUtils.convert_float32_image_uint8(median_noise),
                                             os.path.join('BM3DELBP', 'NoiseClassifier', 'Noise Estimates'),
                                             '{}-{}-{}-median-noise-estimate.png'.format(image.name, image.test_noise,
                                                                                         image.test_noise_val))
            else:
                raise ValueError('save_img set but not passed as DatasetManager.Image or BM3DELBPImage')

        kurtosis_bm3d = kurtosis(a=bm3d_noise, axis=None, fisher=False, nan_policy='raise')
        skewness_bm3d = skew(a=bm3d_noise, axis=None, nan_policy='raise')
        kurtosis_homomorphic = kurtosis(a=homomorphic_noise, axis=None, fisher=False, nan_policy='raise')
        skewness_homomorphic = skew(a=homomorphic_noise, axis=None, nan_policy='raise')
        kurtosis_median = kurtosis(a=median_noise, axis=None, fisher=False, nan_policy='raise')
        skewness_median = skew(a=median_noise, axis=None, nan_policy='raise')

        if GlobalConfig.get('debug'):
            print("BM3D Filtered Kurtosis:", kurtosis_bm3d, ", Skewness: ", skewness_bm3d)
            print("Homomorphic Filtered Kurtosis:", kurtosis_homomorphic, ", Skewness: ", skewness_homomorphic)
            print("Median Filtered Kurtosis:", kurtosis_median, ", Skewness: ", skewness_median)

        # Generate image featurevector of 6 characteristics
        featurevector = np.array([kurtosis_bm3d, skewness_bm3d,
                                  kurtosis_homomorphic, skewness_homomorphic,
                                  kurtosis_median, skewness_median])
        return featurevector
Ejemplo n.º 11
0
 def generate_salt_pepper_004(self, noise_classifier):
     salt_pepper_004_data = ImageUtils.add_salt_pepper_noise_skimage(
         self.data, 0.04)
     self.salt_pepper_004_noise_featurevector = noise_classifier.describe(
         salt_pepper_004_data, test_image=False)
Ejemplo n.º 12
0
 def generate_speckle_004(self, noise_classifier):
     speckle_data = ImageUtils.add_speckle_noise_skimage(self.data, 0.04)
     self.speckle_004_noise_featurevector = noise_classifier.describe(
         speckle_data, test_image=False)
Ejemplo n.º 13
0
 def generate_gauss_25(self, noise_classifier):
     gauss_25_data = ImageUtils.add_gaussian_noise_skimage(self.data, 25)
     self.gauss_25_noise_featurevector = noise_classifier.describe(
         gauss_25_data, test_image=False)
Ejemplo n.º 14
0
    def load_image(path):
        """
        Load a single greyscale image_scaled with the defined resize ratio, scales image_scaled to zero mean and unit variance
        and adds noise type (if configured).
        :param path: Path to the image
        :return: Loaded image_scaled, np.float32 ndarray.
        """
        image_name = path.split(os.sep)[-1].partition('.')[0]
        image_label = path.split(os.sep)[-2]  # Gets the image category
        # Scale train image
        train_data = cv2.resize(cv2.imread(path, cv2.IMREAD_GRAYSCALE), (0, 0),
                                fx=GlobalConfig.get('scale'),
                                fy=GlobalConfig.get('scale'))
        # Scale test image
        if GlobalConfig.get('test_scale') is not None:
            test_data = cv2.resize(cv2.imread(path, cv2.IMREAD_GRAYSCALE),
                                   (0, 0),
                                   fx=GlobalConfig.get('test_scale'),
                                   fy=GlobalConfig.get('test_scale'))
        else:
            test_data = train_data.copy(
            )  # Make a copy so that later transformations on test data don't cause issues

        algo = GlobalConfig.get('algorithm')
        # Convert image from uint8 to float32
        if algo == 'MRELBP' or algo == 'BM3DELBP' or algo == 'NoiseClassifier':
            #  Image gets scaled to 0 mean if using MRELBP, BM3DELBP or BM3DELBP's NoiseClassifier
            train_data = ImageUtils.scale_uint8_image_float32(train_data)
            test_data = ImageUtils.scale_uint8_image_float32(test_data)
        else:
            # Other algorithms do not require this scaling.
            train_data = ImageUtils.convert_uint8_image_float32(train_data)
            test_data = ImageUtils.convert_uint8_image_float32(test_data)

        # Apply filters
        if GlobalConfig.get('noise') is None:
            pass
        elif GlobalConfig.get('noise') == 'gaussian':
            if GlobalConfig.get('train_noise'):
                train_data = ImageUtils.add_gaussian_noise_skimage(
                    train_data, GlobalConfig.get('noise_val'))
            test_data = ImageUtils.add_gaussian_noise_skimage(
                test_data, GlobalConfig.get('noise_val'))
        elif GlobalConfig.get('noise') == 'speckle':
            if GlobalConfig.get('train_noise'):
                train_data = ImageUtils.add_speckle_noise_skimage(
                    train_data, GlobalConfig.get('noise_val'))
            test_data = ImageUtils.add_speckle_noise_skimage(
                test_data, GlobalConfig.get('noise_val'))
        elif GlobalConfig.get('noise') == 'salt-pepper':
            if GlobalConfig.get('train_noise'):
                train_data = ImageUtils.add_salt_pepper_noise_skimage(
                    train_data, GlobalConfig.get('noise_val'))
            test_data = ImageUtils.add_salt_pepper_noise_skimage(
                test_data, GlobalConfig.get('noise_val'))
        else:
            raise ValueError('Invalid image_scaled noise type defined')

        loaded_image = Image(train_data, image_name, image_label)
        loaded_image.test_data = test_data
        loaded_image.test_scale = GlobalConfig.get('test_scale')
        loaded_image.test_noise = GlobalConfig.get('noise')
        loaded_image.test_noise_val = GlobalConfig.get('noise_val')
        return loaded_image