def covR(data): #Calculation of image covariance matrix as a function of mask radius R #Find the image center-of-mass xbar, ybar, cov = image_raw_moments(data) center = [xbar, ybar] p2x, p2y = imtl.ImageFit(data, 1.0) print "Image center is at: ", p2x[0], p2y[0] center = [p2x[0], p2y[0]] #Define image size h, w = data.shape[:2] cov_array = np.zeros((m, 4)) #Loop over radius size (program core loop) for i in range(0, m): factor = int(max(semi_a0, semi_b0) / min(semi_a0, semi_b0)) semi_a = semi_a0 + i * step semi_b = semi_b0 + i * step mask = create_elliptical_mask(h, w, center, semi_a, semi_b, skew) temp = data.copy() #zero all the points outside of region of interest temp[~mask] = 0.0 xbart, ybart, covt = image_raw_moments(temp) cov_array[i, :] = np.ndarray.flatten(covt) print semi_a, semi_b, i #store calculated moments as a function of radius size for analysis return cov_array
pwd = 'examples/Normal_quad_scan_L2/' filename = pwd + 'X111_YAG_' + str(i) + '_bkg_0.png' filenamebg = pwd + 'X111_YAG_' + str(i) + '_img_0.png' data = imtl.Load(filename) bg = imtl.Load(filenamebg) data = data - bg data = data[0:2000, 0:2000] data = imtl.Normalize(data) #experimental data_rot = data.copy() data_rot = rotate(data, skew, reshape=False) x, y = imtl.ImageFit(data_rot, 1.0, True) print "Image center is at: ", int(x[0]), int(y[0]) print "Gaussian fit sizes (px): ", int(x[2]), int(y[2]) print "Gaussian fit sizes (m): ", cal * x[2], cal * y[2] sigma_max = int(np.max((x[2], y[2]))) sigma_min = int(np.min((x[2], y[2]))) #initial crop box = int(4.7 * sigma_max) data = imtl.AutoCrop(data, box, box) data = imtl.Denoise(data) #x,y = imtl.ImageFit(data,cal) #print "Gaussian fit: ",x,y if (quiet != True):
def image_moments(data, semiaxis_a, semiaxis_b): global radius0 radius0 = (semiaxis_a + semiaxis_b) / 2 global semi_a0, semi_b0 semi_a0 = int(2 * semiaxis_a) semi_b0 = int(2 * semiaxis_b) print "Inital mask radius: ", radius0 h, v = data.shape[:2] global step step = int((h / 2 - max(semi_a0, semi_b0)) / m) print "Step size is: ", step #Retrieve cov(R) data cov_array = covR(data) #Find the image center-of-mass xbar, ybar, cov = image_raw_moments(data) center = [xbar, ybar] p2x, p2y = imtl.ImageFit(data, 1.0) print "Image center is at: ", p2x[0], p2y[0] center = [p2x[0], p2y[0]] # ellipse = data.copy() # mask_ellipse = create_elliptical_mask(h,v,center,radius0,3*radius0,45.0) # ellipse[~mask_ellipse] = 0 # plt.figure() # plt.imshow(ellipse) # plt.show() #Calculate the correct radii for <xx>,<yy> and <xy> moments. The condition is min(d covR(R) / dR) # m = len(cov_array) #Retrieve the positions of max(d covR(R) / dR) # nx, ny, nxy = dcov_array_max(cov_array) #Radius is calculated as the location of derivative minimum # radiusX = radius0 + step*np.argmin(np.gradient(cov_array[nx:m-1,0])) + step*nx # radiusY = radius0 + step*np.argmin(np.gradient(cov_array[ny:m-1,3])) + step*ny # radiusXY = radius0 + step*np.argmin(np.abs(np.gradient(cov_array[nxy:m-1,1]))) + step*nxy minx, miny, minxy = dcov_array_min(cov_array) semi_aX = semi_a0 + step * minx semi_bX = semi_b0 + step * minx semi_aY = semi_a0 + step * miny semi_bY = semi_b0 + step * miny semi_aXY = semi_a0 + step * minxy semi_bXY = semi_b0 + step * minxy print "Mask ellipse X: ", semi_aX, semi_bX print "Mask ellipse Y: ", semi_aY, semi_bY print "Mask ellipse XY: ", semi_aXY, semi_bXY #Plotting covariance matrix elements for debugging plot_covarray(cov_array) #Create masks for <xx>,<yy> and <xy> moments maskX = create_elliptical_mask(h, v, center, semi_aX, semi_bX, skew) maskY = create_elliptical_mask(h, v, center, semi_aY, semi_bY, skew) maskXY = create_elliptical_mask(h, v, center, semi_aXY, semi_bXY, skew) #Prepare three copies of masked data for final moments calculation tempX = data.copy() tempY = data.copy() tempXY = data.copy() tempX[~maskX] = 0 tempY[~maskY] = 0 tempXY[~maskXY] = 0 # plt.figure() fig, ax = plt.subplots(1) extent = (0, len(tempXY[:, 0]), 0, len(tempXY[:, 1])) tempXY = np.flip(tempXY, 0) ax.imshow(imtl.Normalize(tempXY), extent=extent) cx = patches.Ellipse(center, 2 * semi_aX, 2 * semi_bX, skew, color='y', linewidth=3, fill=False) cy = patches.Ellipse(center, 2 * semi_aY, 2 * semi_bY, skew, color='g', linewidth=3, fill=False) cxy = patches.Ellipse(center, 2 * semi_aXY, 2 * semi_bXY, skew, color='m', linewidth=3, fill=False) ax.add_patch(cx) ax.add_patch(cy) ax.add_patch(cxy) plt.legend((cx, cy, cxy), (r'Mask $\langle xx \rangle$', r'Mask $\langle yy\rangle$', r'Mask $\langle xy\rangle$')) plt.xlabel('x (px)') plt.ylabel('y (px)') plt.tight_layout() plt.show() #Final calculation xxt, yyt, covx = image_raw_moments(tempX) xxt, yyt, covy = image_raw_moments(tempY) xxt, yyt, covxy = image_raw_moments(tempXY) return covx[0, 0], covy[1, 1], covxy[0, 1]