def FindOffset(FixedImage, MovingImage, MinOverlap=0.0, MaxOverlap=1.0, FFT_Required=True, FixedImageShape=None, MovingImageShape=None): '''return an alignment record describing how the images overlap. The alignment record indicates how much the moving image must be rotated and translated to align perfectly with the FixedImage. If adjusting control points the peak can be added to the fixed image's control point, or subtracted from the warped image's control point (accounting for any transform used to create the warped image) to align the images. :param ndarray FixedImage: Target space we are registering into :param ndarray MovingImage: Source space we are coming from :param float MinOverlap: The minimum amount of overlap by area the registration must have :param float MaxOverlap: The maximum amount of overlap by area the registration must have :param bool FFT_Required: True by default, if False the input images are in FFT space already :param tuple FixedImageShape: Defaults to None, if specified it contains the size of the fixed image before padding. Used to calculate mask for valid overlap values. :param tuple MovingImageShape: Defaults to None, if specified it contains the size of the moving image before padding. Used to calculate mask for valid overlap values. ''' # Find peak requires both the fixed and moving images have equal size assert((FixedImage.shape[0] == MovingImage.shape[0]) and (FixedImage.shape[1] == MovingImage.shape[1])) #nornir_imageregistration.ShowGrayscale([FixedImage, MovingImage]) if FixedImageShape is None: FixedImageShape = FixedImage.shape if MovingImageShape is None: MovingImageShape = MovingImage.shape CorrelationImage = None if FFT_Required: CorrelationImage = ImagePhaseCorrelation(FixedImage, MovingImage) else: CorrelationImage = FFTPhaseCorrelation(FixedImage, MovingImage, delete_input=False) CorrelationImage = fftpack.fftshift(CorrelationImage) # Crop the areas that cannot overlap CorrelationImage -= CorrelationImage.min() CorrelationImage /= CorrelationImage.max() # Timer.Start('Find Peak') OverlapMask = nornir_imageregistration.GetOverlapMask(FixedImageShape, MovingImageShape, CorrelationImage.shape, MinOverlap, MaxOverlap) (peak, weight) = FindPeak(CorrelationImage, OverlapMask) del CorrelationImage record = nornir_imageregistration.AlignmentRecord(peak=peak, weight=weight) return record
def testSquareOverlapMask(self): FixedImageSize = np.asarray((128, 128), dtype=np.int32) MovingImageSize = np.asarray((128, 128), dtype=np.int32) CorrelationImageSize = FixedImageSize + MovingImageSize mask = nornir_imageregistration.GetOverlapMask(FixedImageSize, MovingImageSize, CorrelationImageSize, MinOverlap=0.25, MaxOverlap=0.75) np.testing.assert_equal(CorrelationImageSize, mask.shape) self.assertTrue( nornir_imageregistration.ShowGrayscale( [mask], title="Square Overlap Mask: 25% Min overlap, 75% max overlap", PassFail=True))
def testSquareOddHeightOverlapMask(self): FixedImageSize = np.asarray((31, 64), dtype=np.int32) MovingImageSize = np.asarray((32, 64), dtype=np.int32) CorrelationImageSize = FixedImageSize + MovingImageSize mask = nornir_imageregistration.GetOverlapMask(FixedImageSize, MovingImageSize, CorrelationImageSize, MinOverlap=0.25, MaxOverlap=0.75) np.testing.assert_equal(CorrelationImageSize, mask.shape, "Dimensions of output mask is incorrect") self.assertTrue( nornir_imageregistration.ShowGrayscale( [mask], title= "Square Overlap Mask with odd height dimension: 25% Min overlap, 75% max overlap", PassFail=True))
def testMismatchedOverlapMask_MovingLarger(self): FixedImageSize = np.asarray((64, 64), dtype=np.int32) MovingImageSize = np.asarray((64, 256), dtype=np.int32) CorrelationImageSize = FixedImageSize + MovingImageSize mask = nornir_imageregistration.GetOverlapMask(FixedImageSize, MovingImageSize, CorrelationImageSize, MinOverlap=0.25, MaxOverlap=0.75) np.testing.assert_equal(CorrelationImageSize, mask.shape) self.assertTrue( nornir_imageregistration.ShowGrayscale( [mask], title= "Mismatched Overlap Mask (Moving Larger): 25% Min overlap, 75% max overlap", PassFail=True)) return