def findRotationScale(image1, image2, window=None, highpass=None):
        if image1.shape != image2.shape:
                raise ValueError

        shape = image1.shape
        if window is None:
                window = numextension.hanning(shape[0], shape[1], a=0.54)
        image1 = image1 * window
        image2 = image2 * window

        s = None
        image1 = swapQuadrants(numpy.fft.rfft2(image1, s=s))
        image2 = swapQuadrants(numpy.fft.rfft2(image2, s=s))
        shape = image1.shape

        image1 = numpy.absolute(image1)
        image2 = numpy.absolute(image2)

        if highpass is None:
                highpass = numextension.highpass(*shape)
        image1 *= highpass
        image2 *= highpass

        args = (shape[0]/2, shape[0]/2,
                                        shape[0]/2.0,
                                        0.0, min(shape[0]/2.0, shape[1]),
                                        -math.pi/2.0, math.pi/2.0)
        image1, base, phiscale = numextension.logpolar(image1, *args)
        image2, base, phiscale = numextension.logpolar(image2, *args)
        #mrc.write(image1, 'lp1.mrc')
        #mrc.write(image2, 'lp2.mrc')

        pc = phaseCorrelate(image1, image2)
        #mrc.write(pc, 'pc.mrc')
        peak, value = findPeak(pc)
        logrho, theta = peak
        rotation = math.degrees(theta/phiscale)
        scale = base**-logrho

        return rotation, scale, value
def findRotationScale(image1, image2, window=None, highpass=None):
    if image1.shape != image2.shape:
        raise ValueError

    shape = image1.shape
    if window is None:
        window = numextension.hanning(shape[0], shape[1], a=0.54)
    image1 = image1 * window
    image2 = image2 * window

    s = None
    image1 = swapQuadrants(numpy.fft.rfft2(image1, s=s))
    image2 = swapQuadrants(numpy.fft.rfft2(image2, s=s))
    shape = image1.shape

    image1 = numpy.absolute(image1)
    image2 = numpy.absolute(image2)

    if highpass is None:
        highpass = numextension.highpass(*shape)
    image1 *= highpass
    image2 *= highpass

    args = (shape[0] / 2, shape[0] / 2, shape[0] / 2.0, 0.0,
            min(shape[0] / 2.0, shape[1]), -math.pi / 2.0, math.pi / 2.0)
    image1, base, phiscale = numextension.logpolar(image1, *args)
    image2, base, phiscale = numextension.logpolar(image2, *args)
    #mrc.write(image1, 'lp1.mrc')
    #mrc.write(image2, 'lp2.mrc')

    pc = phaseCorrelate(image1, image2)
    #mrc.write(pc, 'pc.mrc')
    peak, value = findPeak(pc)
    logrho, theta = peak
    rotation = math.degrees(theta / phiscale)
    scale = base**-logrho

    return rotation, scale, value
        o = (-(o[0] + shape[0]/2.0), -(o[1] + shape[1]/2.0))
        o = numpy.dot(m, o)

        return scipy.ndimage.affine_transform(image, m, o, shape)

def findTranslation(image1, image2):
        pc = phaseCorrelate(image1, image2)
        return findPeak(pc)

if __name__ == '__main__':
        from pyami import mrc
        import sys
        import time

        window = numextension.hanning(1024, 1024, a=0.54)
        highpass = numextension.highpass(1024, 513)

        for i in range(16):
        #for i in [0]:
                #for j in range(9):
                for j in [4]:
                        f1 = '04dec17b_000%d_0000%dgr.mrc' % (749 + i, j + 1)
                        f2 = '05jan20a_000%d_0000%dgr.mrc' % (749 + i, j + 1)
                        image1 = mrc.read(f1)
                        image2 = mrc.read(f2)
                        #image2 = rotateScaleOffset(image1, 0.0, 1.0, (0.0, 0.0))

                        '''
                        result = findRotationScaleTranslation(image1, image2, window, highpass)
                        #rotation, scale, shift, value = result
                        rotation, scale, shift, value, image = result
    return scipy.ndimage.affine_transform(image, m, o, shape)


def findTranslation(image1, image2):
    pc = phaseCorrelate(image1, image2)
    return findPeak(pc)


if __name__ == '__main__':
    from pyami import mrc
    import sys
    import time

    window = numextension.hanning(1024, 1024, a=0.54)
    highpass = numextension.highpass(1024, 513)

    for i in range(16):
        #for i in [0]:
        #for j in range(9):
        for j in [4]:
            f1 = '04dec17b_000%d_0000%dgr.mrc' % (749 + i, j + 1)
            f2 = '05jan20a_000%d_0000%dgr.mrc' % (749 + i, j + 1)
            image1 = mrc.read(f1)
            image2 = mrc.read(f2)
            #image2 = rotateScaleOffset(image1, 0.0, 1.0, (0.0, 0.0))
            '''
                        result = findRotationScaleTranslation(image1, image2, window, highpass)
                        #rotation, scale, shift, value = result
                        rotation, scale, shift, value, image = result
                        Mrc.numeric_to_mrc(image, '%d_%d.mrc' % (749 + i, j + 1))