コード例 #1
0
def convolve_with_kernel_preserve_zeros(vector_field,
                                        kernel=sobolev_kernel_1d,
                                        print_focus_coord_info=False):
    x_convolved = np.zeros_like(vector_field)
    y_convolved = np.zeros_like(vector_field)
    focus_coordinates = get_focus_coordinates()
    zero_check = np.abs(vector_field) < 1e-6
    for x in range(vector_field.shape[1]):
        y_convolved[:, x, 0] = np.convolve(vector_field[:, x, 0],
                                           kernel,
                                           mode='same')
        y_convolved[:, x, 1] = np.convolve(vector_field[:, x, 1],
                                           kernel,
                                           mode='same')
    y_convolved[zero_check] = 0.0
    for y in range(vector_field.shape[0]):
        x_convolved[y, :, 0] = np.convolve(y_convolved[y, :, 0],
                                           kernel,
                                           mode='same')
        x_convolved[y, :, 1] = np.convolve(y_convolved[y, :, 1],
                                           kernel,
                                           mode='same')
    x_convolved[zero_check] = 0.0
    np.copyto(vector_field, x_convolved)
    if print_focus_coord_info:
        new_gradient_at_focus = vector_field[focus_coordinates[1],
                                             focus_coordinates[0]]
        print(" H1 grad: {:s}[{:f} {:f}{:s}]".format(BOLD_GREEN,
                                                     -new_gradient_at_focus[0],
                                                     -new_gradient_at_focus[1],
                                                     RESET),
              sep='',
              end='')
    return vector_field
コード例 #2
0
    def __generate_initial_focus_neighborhood_log(field_size):
        focus_coordinates = get_focus_coordinates()
        neighborhood_log = {}
        for y in range(focus_coordinates[1] - 1, focus_coordinates[1] + 2):
            for x in range(focus_coordinates[0] - 1, focus_coordinates[0] + 2):
                if 0 <= x < field_size and 0 <= y < field_size:
                    neighborhood_log[(x, y)] = VoxelLog()

        return neighborhood_log
コード例 #3
0
 def plot_logged_sdf_and_warp_magnitudes(self):
     visualize_and_save_sdf_and_warp_magnitude_progression(
         get_focus_coordinates(), self.focus_neighborhood_log,
         self.out_path)
コード例 #4
0
    def __optimization_iteration_vectorized(self,
                                            warped_live_field,
                                            canonical_field,
                                            warp_field,
                                            band_union_only=True):

        live_gradient_y, live_gradient_x = np.gradient(warped_live_field)
        data_gradient_field = dt.compute_data_term_gradient_vectorized(
            warped_live_field, canonical_field, live_gradient_x,
            live_gradient_y)
        set_zeros_for_values_outside_narrow_band_union(warped_live_field,
                                                       canonical_field,
                                                       data_gradient_field)
        self.total_data_energy = \
            dt.compute_data_term_energy_contribution(warped_live_field, canonical_field) * self.data_term_weight
        smoothing_gradient_field = st.compute_smoothing_term_gradient_vectorized(
            warp_field)
        self.total_smoothing_energy = \
            st.compute_smoothing_term_energy(warp_field, warped_live_field,
                                             canonical_field) * self.smoothing_term_weight

        if self.visualizer.data_component_field is not None:
            np.copyto(self.visualizer.data_component_field,
                      data_gradient_field)
        if self.visualizer.smoothing_component_field is not None:
            np.copyto(self.visualizer.smoothing_component_field,
                      smoothing_gradient_field)
        if self.visualizer.level_set_component_field is not None:
            frame_info = getframeinfo(currentframe())
            print(
                "Warning: level set term not implemented in vectorized version, "
                "passed level_set_component_field is not None, {:s} : {:d}".
                format(frame_info.filename, frame_info.lineno))

        self.gradient_field = self.data_term_weight * data_gradient_field + \
                              self.smoothing_term_weight * smoothing_gradient_field

        if band_union_only:
            set_zeros_for_values_outside_narrow_band_union(
                warped_live_field, canonical_field, self.gradient_field)

        # *** Print information at focus voxel
        focus_x, focus_y = get_focus_coordinates()
        focus = (focus_y, focus_x)
        print("Point: ", focus_x, ",", focus_y, sep='', end='')
        dt.compute_local_data_term(warped_live_field,
                                   canonical_field,
                                   focus_x,
                                   focus_y,
                                   live_gradient_x,
                                   live_gradient_y,
                                   method=dt.DataTermMethod.BASIC)
        focus_data_gradient = data_gradient_field[focus]
        print(" Data grad: ",
              BOLD_GREEN,
              -focus_data_gradient,
              RESET,
              sep='',
              end='')

        st.compute_local_smoothing_term_gradient(
            warp_field,
            focus_x,
            focus_y,
            method=self.smoothing_term_method,
            copy_if_zero=False,
            isomorphic_enforcement_factor=self.isomorphic_enforcement_factor)
        focus_smoothing_gradient = smoothing_gradient_field[
            focus] * self.smoothing_term_weight
        print(" Smoothing grad (scaled): ",
              BOLD_GREEN,
              -focus_smoothing_gradient,
              RESET,
              sep='',
              end='')

        # ***
        if self.sobolev_smoothing_enabled:
            convolve_with_kernel_preserve_zeros(self.gradient_field,
                                                self.sobolev_kernel, True)

        np.copyto(warp_field,
                  -self.gradient_field * self.gradient_descent_rate)
        warp_lengths = np.linalg.norm(warp_field, axis=2)
        maximum_warp_length_at = np.unravel_index(np.argmax(warp_lengths),
                                                  warp_lengths.shape)
        maximum_warp_length = warp_lengths[maximum_warp_length_at]

        # ***
        print(" Warp: ",
              BOLD_GREEN,
              warp_field[focus],
              RESET,
              " Warp length: ",
              BOLD_GREEN,
              np.linalg.norm(warp_field[focus]),
              RESET,
              sep='')
        # ***

        get_and_print_interpolation_data(canonical_field, warped_live_field,
                                         warp_field, focus_x, focus_y)

        u_vectors = warp_field[:, :, 0].copy()
        v_vectors = warp_field[:, :, 1].copy()

        out_warped_live_field, (out_u_vectors, out_v_vectors) = \
            cpp_extension.resample(warped_live_field, canonical_field, u_vectors, v_vectors)

        np.copyto(warped_live_field, out_warped_live_field)

        # some entries might have been erased due to things in the live sdf becoming truncated
        warp_field[:, :, 0] = out_u_vectors
        warp_field[:, :, 1] = out_v_vectors

        return maximum_warp_length, Point2d(maximum_warp_length_at[1],
                                            maximum_warp_length_at[0])
コード例 #5
0
def convolve_with_kernel(vector_field,
                         kernel=sobolev_kernel_1d,
                         print_focus_coord_info=False):
    x_convolved = np.zeros_like(vector_field)
    y_convolved = np.zeros_like(vector_field)
    z_convolved = None
    if len(vector_field.shape) == 3 and vector_field.shape[2] == 2:
        focus_coordinates = get_focus_coordinates()

        for x in range(vector_field.shape[1]):
            y_convolved[:, x, 0] = np.convolve(vector_field[:, x, 0],
                                               kernel,
                                               mode='same')
            y_convolved[:, x, 1] = np.convolve(vector_field[:, x, 1],
                                               kernel,
                                               mode='same')

        for y in range(vector_field.shape[0]):
            x_convolved[y, :, 0] = np.convolve(y_convolved[y, :, 0],
                                               kernel,
                                               mode='same')
            x_convolved[y, :, 1] = np.convolve(y_convolved[y, :, 1],
                                               kernel,
                                               mode='same')

        if print_focus_coord_info:
            new_gradient_at_focus = vector_field[focus_coordinates[1],
                                                 focus_coordinates[0]]
            print(" H1 grad: {:s}[{:f} {:f}{:s}]".format(
                BOLD_GREEN, -new_gradient_at_focus[0],
                -new_gradient_at_focus[1], RESET),
                  sep='',
                  end='')
        np.copyto(vector_field, x_convolved)

    elif len(vector_field.shape) == 4 and vector_field.shape[3] == 3:
        z_convolved = np.zeros_like(vector_field)
        for z in range(vector_field.shape[0]):
            for y in range(vector_field.shape[1]):
                for i_val in range(3):
                    x_convolved[z, y, :,
                                i_val] = np.convolve(vector_field[z, y, :,
                                                                  i_val],
                                                     kernel,
                                                     mode='same')
        for z in range(vector_field.shape[0]):
            for x in range(vector_field.shape[2]):
                for i_val in range(3):
                    y_convolved[z, :, x,
                                i_val] = np.convolve(x_convolved[z, :, x,
                                                                 i_val],
                                                     kernel,
                                                     mode='same')
        for y in range(vector_field.shape[1]):
            for x in range(vector_field.shape[2]):
                for i_val in range(3):
                    z_convolved[:, y, x,
                                i_val] = np.convolve(y_convolved[:, y, x,
                                                                 i_val],
                                                     kernel,
                                                     mode='same')
        np.copyto(vector_field, z_convolved)
    else:
        raise ValueError(
            "Can only process tensors with 3 dimensions (where last dimension is 2) or "
            "tensors with 4 dimensions (where last dimension is 3), i.e. 2D & 3D vector fields"
        )

    return vector_field
コード例 #6
0
def mark_focus_coordinate_on_sdf_image(image, scale=8):
    focus_coordinates = get_focus_coordinates()
    return mark_point_on_sdf_image(
        image, Point2d(focus_coordinates[0], focus_coordinates[1]), scale)