def centerOfMass(img, yx, window=5): """ find peak by center of mass in a 2D image img: a 2D image array yx: (y,x) in the image window: a window where CM calculation is performed on return yx """ # prepare small image s = N.array([window, window]) c = s / 2. yx = N.round_(yx) yx -= c yi, xi = N.indices(s) yi += yx[0] xi += yx[1] cc = img[yi, xi] # calculate center of mass yxi = N.indices(s) yxi *= cc yxi = yxi.T vv = N.sum(yxi, axis=0) vv = N.sum(vv, axis=0) yxs = vv / float(N.sum(cc)) yxs += yx return yxs
def arr_edgeFilter(img, sigma=1.5): """ average-deviation with a gaussian prefilter img must be in an even shape """ if sigma: g = gaussianArrND(img.shape, sigma) g = F.shift(g) img = F.convolve(img.astype(N.float32), g) gr = N.gradient(img.astype(N.float32)) ff = N.sum(N.power(gr, 2), 0) return ff
def img2polar2D(img, center, final_radius=None, initial_radius=None, phase_width=360): """ img: array center: coordinate y, x final_radius: ending radius initial_radius: starting radius phase_width: npixles / circle """ if img.ndim > 2 or len(center) > 2: raise ValueError, 'this function only support 2D, you entered %i-dim array and %i-dim center coordinate' % ( img.ndim, len(center)) if initial_radius is None: initial_radius = 0 if final_radius is None: rad0 = N.ceil(N.array(img.shape) - center) final_radius = min((min(rad0), min(N.ceil(center)))) if phase_width is None: phase_width = N.sum(img.shape[-2:]) * 2 theta, R = np.meshgrid(np.linspace(0, 2 * np.pi, phase_width), np.arange(initial_radius, final_radius)) Ycart, Xcart = polar2cart2D(R, theta, center) Ycart = N.where(Ycart >= img.shape[0], img.shape[0] - 1, Ycart) Xcart = N.where(Xcart >= img.shape[1], img.shape[1] - 1, Xcart) Ycart = Ycart.astype(int) Xcart = Xcart.astype(int) polar_img = img[Ycart, Xcart] polar_img = np.reshape(polar_img, (final_radius - initial_radius, phase_width)) return polar_img