Commit d03aa207 authored by Jay's avatar Jay
Browse files

More updates - looking good. Testing time.

parent 66f0dba6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -177,4 +177,4 @@ def pattern_match(template, image, upsampling=8, metric=cv2.TM_CCOEFF_NORMED, er
    shift_x = (matched_x - ((corrmap.shape[1]-1)/2)) / upsampling
    shift_y = (matched_y - ((corrmap.shape[0]-1)/2)) / upsampling

    return shift_x, shift_y , max_corr, corrmap
    return -shift_x, -shift_y , max_corr, corrmap
+21 −52
Original line number Diff line number Diff line
from math import modf, floor
from math import modf, floor, ceil
import numpy as np
from plio.io.io_gdal import GeoDataset
import scipy.ndimage as ndimage
@@ -35,6 +35,11 @@ class Roi():

    bottom_y : int
               The bottom image coordinate in imge space

    clip_center : tuple
                  on instantiation, set to (). When clip is called and the clipped_array
                  variable is set, the clip_center is set to the center of the, potentially
                  affine transformed, cliped_array.
    """
    def __init__(self, data, x, y, size_x=200, size_y=200, ndv=None, ndv_threshold=0.5, buffer=5):
        if not isinstance(data, GeoDataset):
@@ -48,15 +53,12 @@ class Roi():
        self._ndv_threshold = ndv_threshold
        self.buffer = buffer
        self.clipped_array = None
        self.clip_center = ()

    @property
    def center(self):
        return (self.x, self.y)

    @property
    def clip_center(self):
        return (self.size_x + 0.5, self.size_y + 0.5)

    @property
    def x(self):
        return self._whole_x + self._remainder_x
@@ -149,7 +151,6 @@ class Roi():
        no null pixels (as defined by the no data value (ndv)) are
        present.
        """
        print(self.ndv)
        if self.ndv == None:
            return True
        return np.isclose(self.ndv,self.array).all()
@@ -193,7 +194,7 @@ class Roi():
            self.size_x = size_x
        if size_y:
            self.size_y = size_y
        print('BUFFER: ', self.buffer)
        
        min_x = self._whole_x - self.size_x - self.buffer
        min_y = self._whole_y - self.size_y - self.buffer
        x_read_length = (self.size_x * 2) + 1 + (self.buffer * 2)
@@ -203,9 +204,9 @@ class Roi():
        if (np.asarray(pixels) < 0).any():
            raise IndexError('Image coordinates plus read buffer are outside of the available data. Please select a smaller ROI and/or a smaller read buffer.')
        
        # This data is an nd array that is larger than originally requested, because it may be affine warped.
        # This data is an nd-array that is larger than originally requested, because it may be affine warped.
        data = self.data.read_array(pixels=pixels, dtype=dtype)
        print('data shape: ', data.shape)

        # Now that the whole pixel array has been read, interpolate the array to align pixel edges
        xi = np.linspace(self._remainder_x, 
                         ((self.buffer*2) + self._remainder_x + (self.size_x*2)), 
@@ -215,64 +216,32 @@ class Roi():
                         (self.size_y*2+1)+(self.buffer*2))
        
        # the xi, yi are intentionally handed in backward, because the map_coordinates indexes column major
        # Maybe this operates in place?
        pixel_locked = ndimage.map_coordinates(data, 
                                       np.meshgrid(yi, xi, indexing='ij'),
                                       mode=mode,
                                       order=3)
        print('pixel locked shape', pixel_locked.shape)

        #pixel_locked = data

        if affine:
            # The cval is being set to the mean of the array,
            pixel_locked = tf.warp(pixel_locked, 
                         affine, #.inverse, 
                         order=3, 
                         mode='constant',
                         cval=0.1)
                         mode='reflect')

            array_center =  (np.array(pixel_locked.shape)[::-1] - 1) / 2.0
            self.original_array_center =  (np.array(pixel_locked.shape)[::-1] - 1) / 2.0
            # Return order is x,y
            new_center = affine.inverse(array_center)[0]
            self.clip_center = affine.inverse(self.original_array_center)[0]
            
            # +1 handles non-inclusive end indexing on the nd-array; indexing order is y, x
            pixel_locked = pixel_locked[floor(new_center[1])-self.size_y:floor(new_center[1])+self.size_y+1,
                                        floor(new_center[0])-self.size_x:floor(new_center[0])+self.size_x+1]
            pixel_locked = pixel_locked[floor(self.clip_center[1])-self.size_y:ceil(self.clip_center[1])+self.size_y,
                                        floor(self.clip_center[0])-self.size_x:ceil(self.clip_center[0])+self.size_x]
            
            print('post affine pixel locked shape', pixel_locked.shape)
            self.clipped_array = img_as_float32(pixel_locked)
            return
        
        """if affine:
            # The cval is being set to the mean of the array,
            d2 = tf.warp(data, 
                        affine,# .inverse, 
                        order=3, 
                        mode=mode)
            
            if self.buffer != 0:
                pixel_locked = d2[self.buffer:-self.buffer, 
                                  self.buffer:-self.buffer]

                return img_as_float32(pixel_locked)
            return d2
        else:
            return data"""

   
        """if affine:
            # The cval is being set to the mean of the array,
            pixel_locked = tf.warp(data, 
                                   affine.inverse, 
                                   order=3, 
                                   mode=mode)"""

        # UL, LR, C and then compute 

            if self.buffer != 0:
                pixel_locked = pixel_locked[self.buffer:-self.buffer, 
                                            self.buffer:-self.buffer]
        # Ohh is buffer doing something here? Yeah, post clip, so we should be safe.
        # Buffers don't matter - wtf
            self.clip_center = tuple(np.array(pixel_locked.shape)[::-1] / 2.)
            self.original_array_center = self.clip_center
            self.clipped_array = img_as_float32(pixel_locked)