Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
width : int, None
The width (pix) of the subframe. The full image is used if set to None.
fwhm : int, None
Full width at half maximum (pix) of the Gaussian kernel. Not used if set to None.
Returns
-------
numpy.ndarray
Position (y, x) of the brightest pixel.
"""
if width is not None:
if center is None:
center = center_pixel(image)
image = crop_image(image, center, width)
if fwhm is None:
smooth = np.copy(image)
else:
sigma = fwhm / math.sqrt(8. * math.log(2.))
kernel = (fwhm * 2 + 1, fwhm * 2 + 1)
smooth = cv2.GaussianBlur(image, kernel, sigma)
# argmax[0] is the y position and argmax[1] is the y position
argmax = np.asarray(np.unravel_index(smooth.argmax(), smooth.shape))
if center is not None and width is not None:
argmax[0] += center[0] - (image.shape[0] - 1) // 2 # y
argmax[1] += center[1] - (image.shape[1] - 1) // 2 # x
def _crop_rotating_star(image: np.ndarray,
position: Union[Tuple[float, float], np.ndarray],
im_size: int,
filter_size: Optional[int]) -> np.ndarray:
starpos = locate_star(image=image,
center=tuple(position),
width=self.m_search_size,
fwhm=filter_size)
return crop_image(image=image,
center=tuple(starpos),
size=im_size)
second and third element are the inner and outer radii (pix) of the aperture.
Returns
-------
np.float64
Photometry value.
"""
check_pos_in = any(np.floor(position[:]-aperture[2]) < 0.)
check_pos_out = any(np.ceil(position[:]+aperture[2]) > image.shape[0])
if check_pos_in or check_pos_out:
phot = np.nan
else:
im_crop = crop_image(image, tuple(position), 2*int(math.ceil(aperture[2])))
npix = im_crop.shape[0]
x_grid = y_grid = np.linspace(-(npix-1)/2, (npix-1)/2, npix)
xx_grid, yy_grid = np.meshgrid(x_grid, y_grid)
rr_grid = np.sqrt(xx_grid**2 + yy_grid**2)
if aperture[0] == 'circular':
phot = np.sum(im_crop[rr_grid < aperture[2]])
elif aperture[0] == 'annulus':
phot = np.sum(im_crop[(rr_grid > aperture[1]) & (rr_grid < aperture[2])])
elif aperture[0] == 'ratio':
phot = np.sum(im_crop[rr_grid < aperture[1]]) / \
np.sum(im_crop[(rr_grid > aperture[1]) & (rr_grid < aperture[2])])
def _align_image(image_in: np.ndarray) -> np.ndarray:
offset = np.array([0., 0.])
for i in range(self.m_num_references):
if self.m_subframe is None:
tmp_offset, _, _ = phase_cross_correlation(ref_images[i, :, :],
image_in,
upsample_factor=self.m_accuracy)
else:
sub_in = crop_image(image_in, None, self.m_subframe)
sub_ref = crop_image(ref_images[i, :, :], None, self.m_subframe)
tmp_offset, _, _ = phase_cross_correlation(sub_ref,
sub_in,
upsample_factor=self.m_accuracy)
offset += tmp_offset
offset /= float(self.m_num_references)
if self.m_resize is not None:
offset *= self.m_resize
sum_before = np.sum(image_in)
tmp_image = rescale(image=np.asarray(image_in, dtype=np.float64),
scale=(self.m_resize, self.m_resize),
order=5,
axis=0))
if dist <= 2 and pixmax_new < pixmax:
break
max_pos = np.vstack((max_pos, [x_max_new, y_max_new]))
x_max = x_max_new
y_max = y_max_new
pixmax = pixmax_new
x_0 = x_0 - (ref_image_size-1)/2 + x_max
y_0 = y_0 - (ref_image_size-1)/2 + y_max
# create reference image around determined maximum
ref_center_frame = crop_image(image=center_frame_unsharp,
center=(int(y_0), int(x_0)),
size=ref_image_size)
# Fit the data using astropy.modeling
gauss_init = models.Gaussian2D(amplitude=np.amax(ref_center_frame),
x_mean=x_0,
y_mean=y_0,
x_stddev=1.,
y_stddev=1.,
theta=0.)
fit_gauss = fitting.LevMarLSQFitter()
y_grid, x_grid = np.mgrid[y_0-(ref_image_size-1)/2:y_0+(ref_image_size-1)/2+1,
x_0-(ref_image_size-1)/2:x_0+(ref_image_size-1)/2+1]
header, im_shape = self.read_header(filename)
# get the images of chop A and chop B
chopa, chopb = self.read_images(filename, im_shape)
if self.m_subtract:
chopa = chopa - chopb
chopb = -1. * np.copy(chopa)
if self.m_crop is not None:
chopa = crop_image(chopa,
center=self.m_crop[0:2],
size=self.m_crop[2],
copy=False)
chopb = crop_image(chopb,
center=self.m_crop[0:2],
size=self.m_crop[2],
copy=False)
if self.m_combine is not None:
if self.m_combine == 'mean':
chopa = np.mean(chopa, axis=0)
chopb = np.mean(chopb, axis=0)
elif self.m_combine == 'median':
chopa = np.median(chopa, axis=0)
chopb = np.median(chopb, axis=0)
header[self._m_config_port.get_attribute('NFRAMES')] = 1
self.m_size = int(math.ceil(self.m_size / pixscale))
print(f'New image size (pixels) = {self.m_size}')
if self.m_center is not None:
print(f'New image center (x, y) = {self.m_center}')
# Crop images chunk by chunk
start_time = time.time()
for i in range(len(frames[:-1])):
# Update progress bar
progress(i, len(frames[:-1]), 'Cropping images...', start_time)
# Select and crop images in the current chunk
images = self.m_image_in_port[frames[i]:frames[i+1], ]
images = crop_image(images, self.m_center, self.m_size, copy=False)
# Write cropped images to output port
self.m_image_out_port.append(images, data_dim=3)
# Save history and copy attributes
history = f'image size (pix) = {self.m_size}'
self.m_image_out_port.add_history('CropImagesModule', history)
self.m_image_out_port.copy_attributes(self.m_image_in_port)
self.m_image_out_port.close_port()
def _align_image(image_in: np.ndarray) -> np.ndarray:
offset = np.array([0., 0.])
for i in range(self.m_num_references):
if self.m_subframe is None:
tmp_offset, _, _ = phase_cross_correlation(ref_images[i, :, :],
image_in,
upsample_factor=self.m_accuracy)
else:
sub_in = crop_image(image_in, None, self.m_subframe)
sub_ref = crop_image(ref_images[i, :, :], None, self.m_subframe)
tmp_offset, _, _ = phase_cross_correlation(sub_ref,
sub_in,
upsample_factor=self.m_accuracy)
offset += tmp_offset
offset /= float(self.m_num_references)
if self.m_resize is not None:
offset *= self.m_resize
sum_before = np.sum(image_in)
tmp_image = rescale(image=np.asarray(image_in, dtype=np.float64),
scale=(self.m_resize, self.m_resize),
shift_yx[0] -= dither_y[index]
shift_yx[1] -= dither_x[index]
if npix % 2 == 0 and self.m_size is not None:
im_tmp = np.zeros((image.shape[0]+1, image.shape[1]+1))
im_tmp[:-1, :-1] = image
image = im_tmp
shift_yx[0] += 0.5
shift_yx[1] += 0.5
im_shift = shift_image(image, shift_yx, 'spline')
if self.m_size is not None:
im_crop = crop_image(im_shift, None, self.m_size)
im_storage.append(im_crop)
else:
im_storage.append(im_shift)
if ndim == 3:
self.m_image_out_port.append(im_storage[0], data_dim=3)
elif ndim == 4:
self.m_image_out_port.append(np.asarray(im_storage), data_dim=4)
print(f'Center [x, y] = [{x_center}, {y_center}]')
history = f'[x, y] = [{round(x_center[j], 2)}, {round(y_center[j], 2)}]'
self.m_image_out_port.copy_attributes(self.m_image_in_port)
self.m_image_out_port.add_history('WaffleCenteringModule', history)
self.m_image_out_port.close_port()