API
utils.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 from hcipy import *
3 import numpy as np
4 from skimage import feature
5 
6 def measure_center_position(image, threshold=1, mask_diameter=20):
7  center_mask = make_circular_aperture(mask_diameter)(image.grid)
8  mask = center_mask * ((image / np.nanstd(image)) < threshold)
9 
10  if np.sum(mask) > 0:
11  xc = np.sum(mask * image.grid.x) / np.sum(mask)
12  yc = np.sum(mask * image.grid.y) / np.sum(mask)
13  return np.array([xc, yc])
14  else:
15  return np.array([0.0, 0.0])
16 
17 def knife_edge_dist(image, theta=-0.47, mask_diameter=200, threshold=0.5):
18  '''Measure the distance from the knife edge focal plane mask to the center of an image.
19 
20 
21  Parameters
22  ----------
23  image : array of floats
24  Default: None.
25  theta: integer
26  The knife edge mask angle with respect to the image's x-axis. Default: 0.
27  mask_diameter: The diameter of the circular mask applied to the image for edge detection. Default: 200.
28  threshold: The sigma used to mask pixels along the circular mask edge. Default: 0.5.
29 
30  Returns
31  -------
32  np.array()
33  A NumPy array containing the knife edge distance in X and Y with respect to the image center.
34  '''
35  THETA = np.deg2rad(theta) # Angle measured from x-axis
36 
37  grid = image.grid
38 
39  center_mask = make_circular_aperture(mask_diameter)(grid)
40  mask = ((image / np.std(image)) < threshold)
41 
42  # Apply circular aperture mask to thresholded field
43  masked_im = (center_mask*mask)
44 
45  # Detect edge and convert edge arr back to Field obj
46  edge = feature.canny(masked_im.shaped, sigma=4) # Widened Gaussian filter for edge detection on noisier images
47 
48  edge_field = Field(edge.ravel(), image.grid)
49 
50  # Get x, y edge coords from edge Field obj
51  x_edge = image.grid.x[edge_field>0]
52  y_edge = image.grid.y[edge_field>0]
53 
54  # Project x, y coords onto x and y axes
55  d_normal = x_edge * np.sin(THETA) + y_edge * np.cos(THETA)
56  d_parallel = x_edge * np.sin(THETA + np.pi/2) + y_edge * np.cos(THETA + np.pi/2)
57  d = np.hypot(d_normal, d_parallel)
58 
59  # Identify minimum absolute distance from the origin
60  min_dist_indx = np.argmin(abs(d))
61 
62  return np.array([0.0, y_edge[min_dist_indx]])
def measure_center_position(image, threshold=1, mask_diameter=20)
Definition: utils.py:6
def knife_edge_dist(image, theta=-0.47, mask_diameter=200, threshold=0.5)
Definition: utils.py:17