def correct_drift(img1, img2, display_cc=False): ''' Returns two ImagePlus objects that are drift corrected to each other. The first image is not changed. The second image is a new image that is shifted using ImageJ's Translate. :param img1: The reference image. :param img2: The image to be shifted. :param display_cc: Activate displaying the CrossCorrelation image (default False). ''' img1_cc, img2_cc = cc.scale_to_power_of_two([img1, img2]) result = cc.perform_correlation(img1_cc, img2_cc) x_off, y_off = cc.get_shift(result) # style after maximum detection if not display_cc: result.hide() else: result.copyScale(img1) cc.style_cc(result) if x_off == 0 and y_off == 0: print('No drift has been detected.') return img1, img2 title = img2.getTitle() img2_dk = Duplicator().run(img2) img2_dk.setTitle('DK-' + title) IJ.run(img2_dk, 'Translate...', 'x=%d y=%d interpolation=None' % (-x_off, -y_off)) img2_dk.copyScale(img2) img2_dk.show() return img1, img2_dk
def get_drift_matrix_cc(images): ''' Returns the drift matrix of the given images. Cross correlation is used for drift detection. :param images: A list containing ImagePlus objects. ''' def get_drift(i, j, _images): '''Returns the drift of imagej compared to image i''' cc_img = cc.perform_correlation(_images[i], _images[j]) offset = cc.get_drift(cc_img) ''' DEBUG print 'Reference: %s at index %i' % (images[i],i) print 'Drifted image: %s at index %i' % (images[j],j) print 'Offset: %d,%d\n' % offset ''' return offset images_cc = cc.scale_to_power_of_two(images) drift_matrix = [[]] * len(images) for i, _ in enumerate(drift_matrix): drift_matrix[i] = [(0, 0)] * len(images) # print 'Initial matrix: ', drift_matrix for i in range(len(images)): # print 'i=%i: ' % i, range(i + 1, len(images)) for j in range(i + 1, len(images)): img_drift = get_drift(i, j, images_cc) ''' DEBUG print 'Appending to %i/%i:' % (i, j) print shift ''' # print 'Matrix element: ', i, j # print 'Adding value: ', img_drift drift_matrix[i][j] = img_drift drift_matrix[j][i] = tuple([-val for val in img_drift]) # print 'Drift matrix:' # pprint(drift_matrix) return drift_matrix
def get_drift_vector_cc(images): ''' Returns the drift vector of the given images. Cross correlation is used for drift detection. :param images: A list containing ImagePlus objects. ''' def get_drift(i, j, _images): '''Returns the drift of imagej compared to image i''' cc_img = cc.perform_correlation(_images[i], _images[j]) offset = cc.get_drift(cc_img) ''' DEBUG print 'Reference: %s at index %i' % (images[i],i) print 'Drifted image: %s at index %i' % (images[j],j) print 'Offset: %d,%d\n' % offset ''' return offset images_cc = cc.scale_to_power_of_two(images) drift_vector = [None] * len(images) drift_vector[0] = (0., 0.) # print 'Initial vector: ', drift_vector for i in range(1, len(images)): img_drift = get_drift(i - 1, i, images_cc) # print 'Vector element: ', i # print 'Adding value: ', img_drift drift_vector[i] = img_drift drift_vector[i] = tuple([a + b for a, b in zip(drift_vector[i - 1], drift_vector[i])]) # print 'Drift vector:' # pprint(drift_vector) return drift_vector