Esempio n. 1
0
    def calculateShiftsFFT(self, stack, reference, offsets=None, widths=None, crop=False):
        if DEBUG:
            print("Offsets = ", offsets)
            print("Widths = ", widths)
        data = stack.data
        if offsets is None:
            offsets = [0.0, 0.0]
        if widths is None:
            widths = [reference.shape[0], reference.shape[1]]
        fft2Function = numpy.fft.fft2
        if 1:
            DTYPE = numpy.float32
        else:
            DTYPE = numpy.float64
        image2 = numpy.zeros((widths[0], widths[1]), dtype=DTYPE)
        shape = image2.shape

        USE_APODIZATION_WINDOW = False
        apo = [10, 10]
        if USE_APODIZATION_WINDOW:
            # use apodization window
            window = numpy.outer(SpecfitFuns.slit([0.5, shape[0] * 0.5, shape[0] - 4 * apo[0], apo[0]],
                                          numpy.arange(float(shape[0]))),
                                 SpecfitFuns.slit([0.5, shape[1] * 0.5, shape[1] - 4 * apo[1], apo[1]],
                                          numpy.arange(float(shape[1])))).astype(DTYPE)
        else:
            window = numpy.zeros((shape[0], shape[1]), dtype=DTYPE)
            window[apo[0]:shape[0] - apo[0], apo[1]:shape[1] - apo[1]] = 1
        image2[:,:] = window * reference[offsets[0]:offsets[0]+widths[0],
                                         offsets[1]:offsets[1]+widths[1]]
        image2fft2 = fft2Function(image2)
        mcaIndex = stack.info.get('McaIndex')
        shifts = numpy.zeros((data.shape[mcaIndex], 2), numpy.float)
        image1 = numpy.zeros(image2.shape, dtype=DTYPE)
        total = float(data.shape[mcaIndex])
        if mcaIndex == 0:
            for i in range(data.shape[mcaIndex]):
                image1[:,:] = window * data[i][offsets[0]:offsets[0]+widths[0],
                                               offsets[1]:offsets[1]+widths[1]]

                image1fft2 = fft2Function(image1)
                shifts[i] = ImageRegistration.measure_offset_from_ffts(image2fft2,
                                                                       image1fft2)
                if DEBUG:
                    print("Index = %d shift = %.4f, %.4f" % (i, shifts[i][0], shifts[i][1]))
                self._progress = (100 * i) / total
        elif mcaIndex in [2, -1]:
            for i in range(data.shape[mcaIndex]):
                image1[:,:] = window * data[:,:,i][offsets[0]:offsets[0]+widths[0],
                                               offsets[1]:offsets[1]+widths[1]]

                image1fft2 = fft2Function(image1)
                shifts[i] = ImageRegistration.measure_offset_from_ffts(image2fft2,
                                                                       image1fft2)
                if DEBUG:
                    print("Index = %d shift = %.4f, %.4f" % (i, shifts[i][0], shifts[i][1]))
                self._progress = (100 * i) / total
        else:
            raise IndexError("Only stacks of images or spectra supported. 1D index should be 0 or 2")
        return shifts
Esempio n. 2
0
    def calculateShiftsFFT(self, stack, reference, offsets=None, widths=None, crop=False):
        if DEBUG:
            print("Offsets = ", offsets)
            print("Widths = ", widths)
        data = stack.data
        if offsets is None:
            offsets = [0.0, 0.0]
        if widths is None:
            widths = [reference.shape[0], reference.shape[1]]
        fft2Function = numpy.fft.fft2
        if 1:
            DTYPE = numpy.float32
        else:
            DTYPE = numpy.float64
        image2 = numpy.zeros((widths[0], widths[1]), dtype=DTYPE)
        shape = image2.shape

        USE_APODIZATION_WINDOW = False
        apo = [10, 10]
        if USE_APODIZATION_WINDOW:
            # use apodization window
            window = numpy.outer(SpecfitFuns.slit([0.5, shape[0] * 0.5, shape[0] - 4 * apo[0], apo[0]],
                                          numpy.arange(float(shape[0]))),
                                 SpecfitFuns.slit([0.5, shape[1] * 0.5, shape[1] - 4 * apo[1], apo[1]],
                                          numpy.arange(float(shape[1])))).astype(DTYPE)
        else:
            window = numpy.zeros((shape[0], shape[1]), dtype=DTYPE)
            window[apo[0]:shape[0] - apo[0], apo[1]:shape[1] - apo[1]] = 1
        image2[:,:] = window * reference[offsets[0]:offsets[0]+widths[0],
                                         offsets[1]:offsets[1]+widths[1]]
        image2fft2 = fft2Function(image2)
        mcaIndex = stack.info.get('McaIndex')
        shifts = numpy.zeros((data.shape[mcaIndex], 2), numpy.float)
        image1 = numpy.zeros(image2.shape, dtype=DTYPE)
        total = float(data.shape[mcaIndex])
        if mcaIndex == 0:
            for i in range(data.shape[mcaIndex]):
                image1[:,:] = window * data[i][offsets[0]:offsets[0]+widths[0],
                                               offsets[1]:offsets[1]+widths[1]]

                image1fft2 = fft2Function(image1)
                shifts[i] = ImageRegistration.measure_offset_from_ffts(image2fft2,
                                                                       image1fft2)
                if DEBUG:
                    print("Index = %d shift = %.4f, %.4f" % (i, shifts[i][0], shifts[i][1]))
                self._progress = (100 * i) / total
        elif mcaIndex in [2, -1]:
            for i in range(data.shape[mcaIndex]):
                image1[:,:] = window * data[:,:,i][offsets[0]:offsets[0]+widths[0],
                                               offsets[1]:offsets[1]+widths[1]]

                image1fft2 = fft2Function(image1)
                shifts[i] = ImageRegistration.measure_offset_from_ffts(image2fft2,
                                                                       image1fft2)
                if DEBUG:
                    print("Index = %d shift = %.4f, %.4f" % (i, shifts[i][0], shifts[i][1]))
                self._progress = (100 * i) / total
        else:
            raise IndexError("Only stacks of images or spectra supported. 1D index should be 0 or 2")
        return shifts
Esempio n. 3
0
 def shiftStack(self, stack, shifts, crop=False, filename=None):
     """
     """
     data = stack.data
     mcaIndex = stack.info['McaIndex']
     if mcaIndex not in [0, 2, -1]:
         raise IndexError(
             "Only stacks of images or spectra supported. 1D index should be 0 or 2"
         )
     if mcaIndex == 0:
         shape = data[mcaIndex].shape
     else:
         shape = data.shape[0], data.shape[1]
     d0_start, d0_end, d1_start, d1_end = \
               ImageRegistration.get_crop_indices(shape,
                                                  shifts[:, 0],
                                                  shifts[:, 1])
     window = numpy.zeros(shape, numpy.float32)
     window[d0_start:d0_end, d1_start:d1_end] = 1.0
     self._progress = 0.0
     total = float(data.shape[mcaIndex])
     if filename is not None:
         hdf = self.__hdf5
         dataGroup = hdf['/entry_000/Data']
         attributes = {}
         attributes['interpretation'] = "image"
         attributes['signal'] = numpy.int32(1)
         outputStack = self.getHDF5BufferIntoGroup(
             dataGroup,
             shape=(data.shape[mcaIndex], shape[0], shape[1]),
             name="data",
             dtype=numpy.float32,
             attributes=attributes)
     for i in range(data.shape[mcaIndex]):
         #tmpImage = ImageRegistration.shiftFFT(data[i], shifts[i])
         if mcaIndex == 0:
             tmpImage = ImageRegistration.shiftBilinear(data[i], shifts[i])
             #tmpImage = ImageRegistration.shiftImage(data[i], -shifts[i], method="fft")
             if filename is None:
                 stack.data[i] = tmpImage * window
             else:
                 outputStack[i] = tmpImage * window
         else:
             tmpImage = ImageRegistration.shiftBilinear(
                 data[:, :, i], shifts[i])
             if filename is None:
                 stack.data[:, :, i] = tmpImage * window
             else:
                 outputStack[i] = tmpImage * window
         if DEBUG:
             print("Index %d bilinear shifted" % i)
         self._progress = (100 * i) / total
Esempio n. 4
0
 def shiftStack(self, stack, shifts, crop=False, filename=None):
     """
     """
     data = stack.data
     mcaIndex = stack.info['McaIndex']
     if mcaIndex not in [0, 2, -1]:
          raise IndexError("Only stacks of images or spectra supported. 1D index should be 0 or 2")
     if mcaIndex == 0:
         shape = data[mcaIndex].shape
     else:
         shape = data.shape[0], data.shape[1]
     d0_start, d0_end, d1_start, d1_end = \
               ImageRegistration.get_crop_indices(shape,
                                                  shifts[:, 0],
                                                  shifts[:, 1])
     window = numpy.zeros(shape, numpy.float32)
     window[d0_start:d0_end, d1_start:d1_end] = 1.0
     self._progress = 0.0
     total = float(data.shape[mcaIndex])
     if filename is not None:
         hdf = self.__hdf5
         dataGroup = hdf['/entry_000/Data']
         attributes = {}
         attributes['interpretation'] = "image"
         attributes['signal'] = numpy.int32(1)
         outputStack = self.getHDF5BufferIntoGroup(dataGroup,
                                                   shape=(data.shape[mcaIndex],
                                                         shape[0],
                                                         shape[1]),
                                                   name="data",
                                                   dtype=numpy.float32,
                                                   attributes=attributes)
     for i in range(data.shape[mcaIndex]):
         #tmpImage = ImageRegistration.shiftFFT(data[i], shifts[i])
         if mcaIndex == 0:
             tmpImage = ImageRegistration.shiftBilinear(data[i], shifts[i])
             #tmpImage = ImageRegistration.shiftImage(data[i], -shifts[i], method="fft")
             if filename is None:
                 stack.data[i] = tmpImage * window
             else:
                 outputStack[i] = tmpImage * window
         else:
             tmpImage = ImageRegistration.shiftBilinear(data[:,:,i], shifts[i])
             if filename is None:
                 stack.data[:, :, i] = tmpImage * window
             else:
                 outputStack[i] = tmpImage * window
         if DEBUG:
             print("Index %d bilinear shifted" % i)
         self._progress = (100 * i) / total