Esempio n. 1
0
 def findDotsCorrelateoff(self, baseimg, offsetimg, iy, ix):
     '''finds new spots using each section correlated with the center'''
     hsp = self.hsp
     r = int(N.floor(self.diameter / 2.))
     bot_base = self.y_center_base - r * self.px_spacing
     left_base = self.x_center_base - r * self.px_spacing
     vert_base = int(bot_base + iy * self.px_spacing)
     horiz_base = int(left_base + ix * self.px_spacing)
     bot_offset = self.y_center_offset - r * self.px_spacing
     left_offset = self.x_center_offset - r * self.px_spacing
     vert_offset = int(bot_offset + iy * self.px_spacing)
     horiz_offset = int(left_offset + ix * self.px_spacing)
     sec = offsetimg[(vert_offset - hsp):(vert_offset + hsp),
                     (horiz_offset - hsp):(horiz_offset + hsp)]
     secbase = baseimg[(vert_base - hsp):(vert_base + hsp),
                       (horiz_base - hsp):(horiz_base + hsp)]
     # indsec = N.where(sec == N.amax(sec))
     # indsecbase = N.where(sec == N.amax(secbase))
     sec = self.suback(sec)
     secbase = self.suback(secbase)
     seccorr = corr(1.0 * secbase, 1.0 * sec[::-1, ::-1], mode='full')
     #        secbasecorr = corr(1.0*secbase, 1.0*secbase[::-1,::-1], mode='full')
     px, py = self.Parabolicfit(seccorr)
     #        self.CorrCenter = N.unravel_index(secbasecorr.argmax(), secbasecorr.shape)
     gradx = self.CorrCenter[1] - px
     grady = self.CorrCenter[0] - py
     self.im[0, iy * 2 * self.hsp:iy * 2 * self.hsp + 2 * self.hsp,
             ix * 2 * self.hsp:ix * 2 * self.hsp + 2 * self.hsp] = secbase
     self.im[1, iy * 2 * self.hsp:iy * 2 * self.hsp + 2 * self.hsp,
             ix * 2 * self.hsp:ix * 2 * self.hsp + 2 * self.hsp] = sec
     return gradx, grady
Esempio n. 2
0
def crop_align(img, imgc):
    """Find crop in img aligned to imgc."""

    if np.amax(img) < np.amax(imgc) // 2:
        return imgc

    _py, _px = int(0.05 * imgc.shape[0]), int(0.05 * imgc.shape[1])
    imgp = np.pad(img, [[_py], [_px], [0]])

    imgpg = np.mean(imgp.astype(np.float32), -1)
    imgc = np.mean(imgc.astype(np.float32), -1)

    imgpc = np.sum(imgc**2) - 2 * corr(imgpg, imgc, 'valid')
    imgpc = imgpc + corr(imgpg**2, np.ones_like(imgc), 'valid')

    amin = np.unravel_index(np.argmin(imgpc), imgpc.shape)
    return imgp[amin[0]:(amin[0] + imgc.shape[0]),
                amin[1]:(amin[1] + imgc.shape[1]), :]
Esempio n. 3
0
 def _check_winner(self) -> Optional[Color]:
     """:return: color of the winner. `None` if unfinished or for a draw"""
     patterns = [
         np.ones(5, dtype=np.int8).reshape(1, 5),
         np.ones(5, dtype=np.int8).reshape(5, 1),
         np.eye(5, dtype=np.int8),
         np.fliplr(np.eye(5, dtype=np.int8))
     ]
     black = (self.chessboard_data == Color.BLACK).astype(np.int8)
     white = (self.chessboard_data == Color.WHITE).astype(np.int8)
     black_win = max(
         [np.max(corr(black, p, mode='same')) for p in patterns]) == 5
     white_win = max(
         [np.max(corr(white, p, mode='same')) for p in patterns]) == 5
     if black_win == white_win:  # draw
         return None
     elif black_win:
         return Color.BLACK
     else:  # white_win
         return Color.WHITE
Esempio n. 4
0
 def __init__(self):
     # set up inital parameters to determine size of the scene composition
     #self.adapt = opencv.adaptivethreshold()
     self.radius = 17  # 1/2 the total number of lenslets in linear direction
     self.x_center = 1022
     self.y_center = 586
     self.px_spacing = 32.85  # spacing between each lenslet
     self.barrier = 256.0  # maximum intensity value accepted by the camera
     self.threshold = 100.0  # minimum intensity value accepted by the camera
     self.nx = 2 * self.radius  # number of lenslets x-direction
     self.ny = 2 * self.radius  # number of lenslets y-direction
     self.hsp = 8  # size of subimage is 2*hsp
     self.calfactor = (.00586 / 6.7) * (
         150)  # pixel size * focalLength * pitch
     self.NaN_Mask = self.mask_nan()
     self.Zero_Mask = self.mask_zero()
     # set up seccorr center
     section = N.ones((2 * self.hsp, 2 * self.hsp))
     sectioncorr = corr(1.0 * section,
                        1.0 * section[::-1, ::-1],
                        mode='full')
     self.CorrCenter = N.unravel_index(sectioncorr.argmax(),
                                       sectioncorr.shape)
     # Set up T and W filters for waffle removal
     # T
     self.T_Filter = N.zeros((3, 3))
     self.T_Filter[0, 1] = self.T_Filter[1, 0] = self.T_Filter[
         1, 2] = self.T_Filter[2, 1] = 0.25
     self.T_Filter[1, 1] = 1
     self.T_Filter = 0.5 * self.T_Filter
     # W
     self.W_Filter = N.ones((3, 3))
     self.W_Filter[0, 0] = self.W_Filter[0, 2] = self.W_Filter[
         2, 0] = self.W_Filter[2, 2] = 0.25
     self.W_Filter[0, 1] = self.W_Filter[1, 0] = self.W_Filter[
         1, 2] = self.W_Filter[2, 1] = 0.5
     self.W_Filter = 0.25 * self.W_Filter
     self.Test_Waffle = N.zeros((3, 3))
     self.Test_Waffle2 = N.zeros((3, 3))
     self.Test_Waffle[0, 1] = self.Test_Waffle[1, 0] = self.Test_Waffle[
         1, 2] = self.Test_Waffle[2, 1] = 1.0
     self.Test_Waffle2[0, 0] = self.Test_Waffle2[0, 2] = self.Test_Waffle2[
         1, 1] = self.Test_Waffle2[2, 0] = self.Test_Waffle2[2, 2] = 1.0
     # initialize Arrays
     self.gradx = N.zeros((self.ny, self.nx))
     self.grady = N.zeros((self.ny, self.nx))
     self.gradx_guide = N.zeros((self.ny, self.nx))
     self.grady_guide = N.zeros((self.ny, self.nx))
     self.gradx_Ref = N.zeros((self.ny, self.nx))
     self.grady_Ref = N.zeros((self.ny, self.nx))
     self.hudgins_prep()
Esempio n. 5
0
    def matched_filter(self, matching_template, signal, method='lfilt'):
        """ Implementation of a matched filter, using either the FIR method
        using an lfilter, or by using correlation directly.

        matching_template - waveform to match to, must be smaller than signal
                            [1-D numpy array]
        signal -            signal under test, must be longer than
                            matching_template [1-D numpy array]
        fs -                sample rate of the signal [Hz, float]
        method:
                            'lfilt' - Uses scipys lfilter with the reversed
                                      template acting as the b coefficients.
                            'conv'- using scipys convolution function to
                                    convolve the reversed template with the
                                    signal.
                            'fftconv' - using scipys fftconv function function
                                        to convolve the reversed template with
                                        the signal.
                            'corr'- using scipys correlate function to directly
                                    correlate the template to the signal.
        """

        if method is 'lfilt':
            matching_template = matching_template[::-1]  # flip the template
            matched_signal = lfilt(matching_template, [1.0], signal)

        elif method is 'conv':
            matching_template = matching_template[::-1]  # flip the template
            matched_signal = conv(signal,
                                  matching_template,
                                  mode='same',
                                  method='direct')

        elif method is 'fftconv':
            matching_template = matching_template[::-1]  # flip the template
            matched_signal = conv(signal,
                                  matching_template,
                                  mode='same',
                                  method='fft')

        elif method is 'corr':
            matched_signal = corr(signal,
                                  matching_template,
                                  mode='same',
                                  method='auto')

        return matched_signal
Esempio n. 6
0
 def __init__(self):
     # set up inital parameters to determine size of the scene composition
     #        self.radius = 8 # 1/2 the total number of lenslets in linear direction
     self.diameter = 16
     self.x_center_base = 1186
     self.y_center_base = 903
     self.x_center_offset = 1186
     self.y_center_offset = 903
     self.px_spacing = 26  # spacing between each lenslet
     self.hsp = 12  # size of subimage is 2*hsp
     self.calfactor = (.0065 / 4.1) * (150
                                       )  # pixel size * focalLength * pitch
     # set up seccorr center
     section = N.ones((2 * self.hsp, 2 * self.hsp))
     sectioncorr = corr(1.0 * section,
                        1.0 * section[::-1, ::-1],
                        mode='full')
     self.CorrCenter = N.unravel_index(sectioncorr.argmax(),
                                       sectioncorr.shape)
Esempio n. 7
0
 def findDotsCorrelateoff(self, baseimg, offsetimg, iy, ix):
     '''finds new spots using each section correlated with the center'''
     hsp = self.hsp
     bot = self.y_center - (self.radius) * self.px_spacing
     left = self.x_center - (self.radius) * self.px_spacing
     vert = int(bot + iy * self.px_spacing)
     horiz = int(left + ix * self.px_spacing)
     sec = offsetimg[(vert - hsp):(vert + hsp), (horiz - hsp):(horiz + hsp)]
     secbase = baseimg[(vert - hsp):(vert + hsp),
                       (horiz - hsp):(horiz + hsp)]
     seccorr = corr(1.0 * secbase, 1.0 * sec[::-1, ::-1], mode='full')
     #seccorrnorm = seccorr*(1.0/N.sum(seccorr))
     try:
         # Corrects for sub-images leaving the sub-field of view by setting the local gradient to zero
         px, py = self.Parabolicfit(seccorr)
     except:  #IndexError
         px, py = self.CorrCenter[1], self.CorrCenter[0]
         #print ix,iy, "Scene Left SubImage"
     gradx = self.CorrCenter[1] - px
     grady = self.CorrCenter[0] - py
     return gradx, grady
Esempio n. 8
0
def signal_sync(reference, signal):
    """
    Returns the optimal shift to maximize correlation between reference signal and the analyzed signal to be shifted.

    Parameters
    ----------

    reference:
        Leading (static) signal;
    signal:
        Signal to be shifted.

    Returns
    -------

    shift: int
        the shift for signal, corrected because of distorsion in correlation.
    """
    corr_data = corr(reference, signal)
    shift = np.argmax(corr_data)
    return shift - int(len(corr_data) / 2)
Esempio n. 9
0
 def findDotsRef(self, image, iy, ix):
     ''' find dot location using correlations and parabolic fit'''
     #image = self.high_pass_filter(image)
     hsp = self.hsp
     bot = self.y_center - (self.radius) * self.px_spacing
     left = self.x_center - (self.radius) * self.px_spacing
     vert = int(bot + iy * self.px_spacing)
     horiz = int(left + ix * self.px_spacing)
     vertmiddle = int(self.y_center)
     horizmiddle = int(self.x_center)
     sec = image[(vert - hsp):(vert + hsp), (horiz - hsp):(horiz + hsp)]
     #sec = self.Threshold_local(self,sec)
     #sec = self.high_pass_filter(sec)
     secmiddle = image[(vertmiddle - hsp):(vertmiddle + hsp),
                       (horizmiddle - hsp):(horizmiddle + hsp)]
     #secmiddle = self.Threshold_local(self,secmiddle)
     #secmiddle = self.high_pass_filter(secmiddle)
     seccorr = corr(1.0 * secmiddle, 1.0 * sec[::-1, ::-1], mode='full')
     px, py = self.Parabolicfit(seccorr)
     gradx = px
     grady = py
     return gradx, grady