Commit c4a43a3d authored by Laura, Jason R's avatar Laura, Jason R
Browse files

Merge branch 'subpixelapi' into 'subpixelapi'

Updates to ROI and affine transfromations

See merge request astrogeology/autocnet!650
parents 78a16cab ede173f9
Loading
Loading
Loading
Loading
+36 −30
Original line number Diff line number Diff line
@@ -1701,23 +1701,6 @@ def subpixel_register_point_smart(pointid,
        cost = None
        destination_node = nodes[measure.imageid]

        try:
            affine = estimate_local_affine(source_node.geodata, 
                                           destination_node.geodata,
                                           source.apriorisample,
                                           source.aprioriline,
                                           30,
                                           30)
        except Exception as e:
            print(e)
            m = {'id': measure.id,
                 'sample':measure.apriorisample,
                 'line':measure.aprioriline,
                 'status':False,
                 'choosername':chooser}
            updated_measures.append([None, None, m])
            continue

        # Compute the baseline metrics using the smallest window
        size_x = np.inf
        size_y = np.inf
@@ -1741,12 +1724,21 @@ def subpixel_register_point_smart(pointid,
                             size_y=size_y, 
                             buffer=10)

        _, baseline_corr, _ = subpixel_template(reference_roi, 
                                                moving_roi,
                                                affine=affine)
        try:
            baseline_affine = estimate_local_affine(reference_roi,
                                                    moving_roi)
        except Exception as e:
            print(e)
            m = {'id': measure.id,
                 'sample':measure.apriorisample,
                 'line':measure.aprioriline,
                 'status':False,
                 'choosername':chooser}
            updated_measures.append([None, None, m])
            continue

        reference_clip = reference_roi.clip()
        moving_clip = moving_roi.clip(affine)
        moving_clip = moving_roi.clip(baseline_affine)
        if np.isnan(reference_clip).any() or np.isnan(moving_clip).any():
            print('Unable to process due to NaN values in the input data.')
            m = {'id': measure.id,
@@ -1763,6 +1755,10 @@ def subpixel_register_point_smart(pointid,
            updated_measures.append([None, None, m])
            continue

        _, baseline_corr, _ = subpixel_template(reference_roi, 
                                                moving_roi,
                                                affine=baseline_affine)
        
        baseline_mi = 0 #mutual_information_match(reference_roi, moving_roi, affine=affine)
        
        print(f'Baseline MI: {baseline_mi} | Baseline Corr: {baseline_corr}')
@@ -1783,7 +1779,17 @@ def subpixel_register_point_smart(pointid,
                                 size_y=match_kwargs['template_size'][1], 
                                 buffer=10)
            

            try:
               affine = estimate_local_affine(reference_roi, moving_roi)
            except Exception as e:
                print(e)
                m = {'id': measure.id,
                     'sample':measure.apriorisample,
                     'line':measure.aprioriline,
                     'status':False,
                     'choosername':chooser}
                updated_measures.append([None, None, m])
                continue
            
            updated_affine, maxcorr, temp_corrmap = subpixel_template(reference_roi,
                                                                      moving_roi,
@@ -2008,12 +2014,6 @@ def validate_candidate_measure(measure_to_register,
        line = measure_to_register['line']

        print(f'Validating measure: {measure_to_register_id} on image: {source_image.name}')
        try:
            affine = estimate_local_affine(source_node.geodata, destination_node.geodata, sample, line, 30,30)
        except:
            print('Unable to transform image to reference space. Likely too close to the edge of the non-reference image. Setting ignore=True')
            return [np.inf] * len(parameters)


        dists = []
        for parameter in parameters:
@@ -2032,6 +2032,12 @@ def validate_candidate_measure(measure_to_register,
                                    size_y=match_kwargs['template_size'][1],
                                    buffer=10)

            try:
                affine = estimate_local_affine(reference_roi, moving_roi)
            except:
                print('Unable to transform image to reference space. Likely too close to the edge of the non-reference image. Setting ignore=True')
                return [np.inf] * len(parameters)

            updated_affine, maxcorr, temp_corrmap = subpixel_template(reference_roi,
                                                                      moving_roi,
                                                                      affine=affine)
+6 −19
Original line number Diff line number Diff line
@@ -91,11 +91,10 @@ def estimate_affine_from_sensors(reference_image,
    return affine


def estimate_local_affine(reference_image, moving_image, center_x, center_y, size_x, size_y):
def estimate_local_affine(reference_roi, moving_roi):
    """
    estimate_local_affine
    Applies the affine transfromation calculated in estimate_affine_from_sensors to the moving region of interest (ROI).
    
    similar to estimate_affine_from_sensors, but for regions of interest (ROI).

    Parameters
    ----------
@@ -105,16 +104,6 @@ def estimate_local_affine(reference_image, moving_image, center_x, center_y, siz
    moving_image : plio.io.io_gdal.GeoDataset
                   Image that is expected to move around during the matching process, 
                   points are projected onto this image to compute an affine  
    center_sample : number
                    center x (aka center sample) of the ROI in reference image pixel space
    center_line : number
                  center y (aka center line) of the ROI in reference image pixel space
    size_x : number
             distance from the center to the end of the ROI window in the x (aka sample) direction. 
             This is in reference image pixel space, resulting roi shape is (sizey*2, sizex*2)
    size_y : number
             distance from the center to the end of the ROI window in the y (aka line) direction, 
             This is in reference image pixel space, resulting roi shape is (sizey*2, sizex*2)

    Returns
    -------
@@ -122,15 +111,13 @@ def estimate_local_affine(reference_image, moving_image, center_x, center_y, siz
        Affine matrix to transform the moving image onto the center image
    """
    # get initial affine
    affine_transform = estimate_affine_from_sensors(reference_image, moving_image, center_x, center_y)

    ref_center = (center_x, center_y)
    affine_transform = estimate_affine_from_sensors(reference_roi.data, moving_roi.data, reference_roi.x, reference_roi.y)
    ref_center = (reference_roi.x, reference_roi.y)

    # MOVING NO AFFINE; Get the full moving image area so that an applied affine transformation that 
    # adds no data around 1+ edge does not fool the to be applied matcher.
    # The affine transformed center is a better match than the a priori sensor coords at this point.
    affine_center = affine_transform(ref_center)[0]
    moving_roi = Roi(moving_image, *affine_center, size_x=size_x, size_y=size_y)

    # The above coordinate transformation to get the center of the ROI handles translation. 
    # So, we only need to rotate/shear/scale the ROI. Omitting scale, which should be 1 (?) results
+6 −6
Original line number Diff line number Diff line
@@ -143,7 +143,7 @@ class Roi():
    @property
    def center(self):
        ie = self.image_extent
        return ((ie[1] - ie[0])-1 + self.buffer*2)/2. + 0.5, ((ie[3]-ie[2])-1 + self.buffer*2)/2. + 0.5
        return ((ie[1] - ie[0])-1)/2. + 0.5, ((ie[3]-ie[2])-1)/2. + 0.5

    @property
    def is_valid(self):
@@ -219,6 +219,10 @@ class Roi():
                                       mode=mode,
                                       order=3)

        if self.buffer != 0:
            pixel_locked = pixel_locked[self.buffer:-self.buffer, 
                                       self.buffer:-self.buffer]

        if affine:
            # The cval is being set to the mean of the array,
            pixel_locked = tf.warp(pixel_locked, 
@@ -226,8 +230,4 @@ class Roi():
                                   order=3, 
                                   mode=mode)

        if self.buffer != 0:
            return img_as_float32(pixel_locked[self.buffer:-self.buffer, 
                                       self.buffer:-self.buffer])
        else:
        return img_as_float32(pixel_locked)