Example #1
0
 def test_patch_distance(self):
     """ Test the squared euclidean distance between patches.
     """
     for patch1, patch2, metric in zip(self.patches1, self.patches2,
                                       self.metrics):
         dist = patch_distance(patch1, patch2)
         self.assertEqual(dist, metric)
Example #2
0
    def _get_denoised_patch(self, index):
        """ Method that compute the denoised patch at a specific location.

        Parameters
        ----------
        index: array
            the patch central location.

        Returns
        -------
        patch: aray
            the denoised patch.
        weight: float
            the patch power (ie., the sum of all the weights associated to
            the neighbor patches).
        """
        # Get patch around the current location.
        central_patch = get_patch(
            index, self.to_denoise_array, self.full_patch_size)

        if not self.use_cython:
            # Intern parameters
            # > maximum weight of patches
            wmax = 0
            # > sum of weights: used for normalization
            wsum = 0

            # Allocate the patch result
            patch = numpy.zeros(self.full_patch_size, dtype=numpy.single)

            # Compute the search region
            search_elements = self._get_search_elements(index)

            # Go through all the search indices
            for neighbor_index in search_elements:

                # Optimize the speed of the patch search
                if self.use_optimized_strategy: 
                    is_valid_neighbor = self._check_speed(
                        tuple(index), tuple(neighbor_index))
                else:
                    is_valid_neighbor = True

                # If we are dealing with a valid neighbor
                if is_valid_neighbor:

                    # Create the neighbor patch
                    neighbor_patch = get_patch(
                        neighbor_index, self.to_denoise_array, self.full_patch_size)

                    # Compute the weight associated to the distance between the
                    # central and neighbor patches
                    weight = numpy.exp(
                        - patch_distance(central_patch, neighbor_patch) /
                        self.range_bandwidth)

                    # Keep trace of the maximum weight
                    if weight > wmax:
                        wmax = weight

                    # Update the weight sum
                    wsum += weight

                    # Update the result denoised patch
                    patch += neighbor_patch * weight
        else:
            # Use compiled code to do the same steps as the upper python version
            patch, wsum, wmax = get_average_patch(
                self.to_denoise_array, index, self.half_spatial_bandwidth,
                self.half_patch_size, self.full_patch_size, self.range_bandwidth,
                self.mean_array, self.variance_array, self.use_optimized_strategy,
                self.lower_mean_threshold, self.lower_variance_threshold,
                self.nb_of_threads)

        # Deal with the central patch based on the user parameters
        # > add the central patch
        if self.central_point_strategy == "add":
            patch += central_patch
            wsum += 1.
        # > remove the central patch
        elif self.central_point_strategy == "remove":
            pass
        # > use a weighted central patch
        elif self.central_point_strategy == "weight":
            patch += (1. - wmax) * central_patch
            wsum += (1. - wmax)
        # > raise value error
        else:
            raise ValueError("Unexpected central point strategy '{0}'.".format(
                self.central_point_strategy))

        # Normalize the denoised patch if the total weight is big enough
        if wsum > 0.0001:
            patch /= wsum
        # Otherwise can't denoise properly, copy the central patch to the 
        # denoise patch
        else:
            patch = central_patch

        return patch, wsum        
Example #3
0
 def test_patch_distance(self):
     """ Test the squared euclidean distance between patches.
     """
     for patch1, patch2, metric in zip(self.patches1, self.patches2, self.metrics):
         dist = patch_distance(patch1, patch2)
         self.assertEqual(dist, metric)