问题
I have an (1727,1853) size array (image), in which I have identified stars to model a point-spread function. Each index of the array corresponds to an image coordinate, however, the centroid of each star is given by a sub-pixel coordinate. I must do the following
Make a 2D slice of each star. I have done this using numpy's array slicing. However, it slices by index, and I have sub-pixel centroid coordinates, thus any kind of slice I make will place the star off-center.
After I make a 2D slice of each star, I must stack these arrays on top of each other to make a model of the point-spread function. This is simple as long as the sub-pixel centers of each star are aligned.
My question is what is the most efficient (and correct) way of aligning these sub-pixel coordinates and stacking each 2D slice together?
I hope this was clear. Any help would be much appreciated. Below is a 2D slice of one of the stars (not a very good one), however it is off center because numpy slices by index and the stars have sub-pixel coordinates for their centroids.
回答1:
You could express the centre coordinates of the pixels in each 'slice' relative to the centroid of the star, then compute a weighted 2D histogram.
First, some example data:
import numpy as np
from matplotlib import pyplot as plt
# pixel coordinates (integer)
x, y = np.mgrid[:100, :100]
# centroids (float)
cx, cy = np.random.rand(2, 9) * 100
# a Gaussian kernel to represent the PSF
def gausskern(x, y, cx, cy, sigma):
return np.exp(-((x - cx) ** 2 + (y - cy) ** 2) / (2 * sigma ** 2))
# (nstars, ny, nx)
stars = gausskern(x[None, ...], y[None, ...],
cx[:, None, None], cy[:, None, None], 10)
# add some noise for extra realism
stars += np.random.randn(*stars.shape) * 0.5
fig, ax = plt.subplots(3, 3, figsize=(5, 5))
for ii in xrange(9):
ax.flat[ii].imshow(stars[ii], cmap=plt.cm.hot)
ax.flat[ii].set_axis_off()
fig.tight_layout()
Weighted 2D histogram:
# (nstars, ny, nx) pixel coordinates relative to each centroid
dx = cx[:, None, None] - x[None, ...]
dy = cy[:, None, None] - y[None, ...]
# 2D weighted histogram
bins = np.linspace(-50, 50, 100)
h, xe, ye = np.histogram2d(dx.ravel(), dy.ravel(), bins=bins,
weights=stars.ravel())
fig, ax = plt.subplots(1, 1, subplot_kw={'aspect':'equal'})
ax.hold(True)
ax.pcolormesh(xe, ye, h, cmap=plt.cm.hot)
ax.axhline(0, ls='--', lw=2, c='w')
ax.axvline(0, ls='--', lw=2, c='w')
ax.margins(x=0, y=0)
来源:https://stackoverflow.com/questions/31713637/stacking-star-psfs-from-an-image-aligning-sub-pixel-centers