def get_fmt_correlation(image1, image2, high_pass=0.15): image1 = get_centered(image1) image2 = get_centered(image2) fmt1 = get_fmt(image1, high_pass) fmt2 = get_fmt(image2, high_pass) correlation = correlate2d(np.abs(fmt1), np.abs(fmt2)) #magnitude argmax = np.unravel_index(correlation.argmax(), correlation.shape) peak = correlation[argmax] relrow = argmax[0] - correlation.shape[0] / 2. relcol = argmax[1] - correlation.shape[1] / 2. return peak, (relrow, relcol)
def get_mask(shape, window, center=None): """ Draw a mask of the given shape and window. If center is not given it will be the shape center. """ array = np.zeros(shape) window2d = radial_extrusion(window) array_center = shape[0] / 2., shape[1] / 2. c_row, c_col = array_center radious = window.shape[0] / 2. top, bottom = (c_row - radious), (c_row + radious) left, right = (c_col - radious), (c_col + radious) try: array[top:bottom, left:right] = window2d except ValueError: print center print array[top:bottom, left:right].shape, window2d.shape print "array[%d:%d, %d:%d] = [](%s)" % (top, bottom, left, right, window2d.shape) raise if center: array = get_centered(array, center, reverse=True) return array
def get_auto_mask(spectrum, softness=0, radious_scale=.8, zero_scale=1.3, cuttop=0, centered=True): """ Try to filter spurious data out. """ shape = spectrum.shape rows, cols = shape center = (rows / 2., cols / 2.) intensity = get_intensity(spectrum) circles = get_circles(intensity, 3, 50) circles = sorted([(circle[1], circle[0], circle[2]) for circle in circles if get_distance(center, circle[0]) > 10])[-2:] circles = sorted([(circle[1], circle[0], circle[2]) for circle in circles]) virtual_order, real_order = circles zero_order = (center, 0, 0) peak_center, peak_height, peak_radious = real_order peak_radious = min([(abs(shape[0] / 3.5 - peak[2]), peak[2]) for peak in circles])[1] windowmaker = lambda x: np.kaiser(x, softness) window = get_holed_window(windowmaker, peak_radious * radious_scale) mask = get_mask(shape, window, peak_center) zerowindow = get_holed_window(windowmaker, peak_radious * zero_scale) zeromask = 1 - get_mask(shape, zerowindow, zero_order[0]) mask *= zeromask masked_intensity = mask * intensity cutoff = masked_intensity > (masked_intensity.max() - masked_intensity.ptp() * cuttop) mask[cutoff] = 0 masked = mask * spectrum if centered: masked = get_centered(masked, peak_center) mask = get_centered(mask, peak_center) centered = get_centered(intensity, peak_center) return mask, masked, centered
def get_wide_segment(array, startpoint, endpoint): """ Returns an array with the values inside of the 2x1 rectangle so that the centers of the shorter sides are at the start and end point. """ row0, col0 = startpoint row1, col1 = endpoint drow = row1 - row0 dcol = col1 - col0 hyp = (drow**2 + dcol**2)**.5 center = (row0 + row1) / 2., (col0 + col1) / 2. centered = get_centered(array, center) rotated = rotate(centered, np.arctan2(drow, dcol) * 360 / tau, mode='wrap') rrows, rcols = rotated.shape rrowc = rrows / 2. rcolc = rcols / 2. col0 = rcolc - hyp / 2. col1 = rcolc + hyp / 2. croped = rotated[rrowc - hyp / 2:rrowc + hyp / 2, col0:col1] sumed = croped.sum(0) return sumed
def get_wide_segment(array, startpoint, endpoint): """ Returns an array with the values inside of the 2x1 rectangle so that the centers of the shorter sides are at the start and end point. """ row0, col0 = startpoint row1, col1 = endpoint drow = row1 - row0 dcol = col1 - col0 hyp = (drow ** 2 + dcol ** 2) ** .5 center = (row0 + row1) / 2., (col0 + col1) / 2. centered = get_centered(array, center) rotated = rotate(centered, np.arctan2(drow, dcol) * 360 / tau, mode='wrap') rrows, rcols = rotated.shape rrowc = rrows / 2. rcolc = rcols / 2. col0 = rcolc - hyp / 2. col1 = rcolc + hyp / 2. croped = rotated[rrowc - hyp / 2: rrowc + hyp / 2, col0:col1] sumed = croped.sum(0) return sumed