/
showMultibandImage.py
53 lines (49 loc) · 2.59 KB
/
showMultibandImage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from astropy.visualization import make_lupton_rgb
from lsst.geom import Box2D
import matplotlib.pyplot as plt
def showRGB(image, bgr="gri", ax=None, fp=None, figsize=(8,8), stretch=1, Q=10, minimum=0):
"""Display an RGB color composite image with matplotlib.
Parameters
----------
image : `MultibandImage`
`MultibandImage` to display.
bgr : sequence
A 3-element sequence of filter names (i.e. keys of the exps dict) indicating what band
to use for each channel. If `image` only has three filters then this parameter is ignored
and the filters in the image are used.
ax : `matplotlib.axes.Axes`
Axis in a `matplotlib.Figure` to display the image.
If `axis` is `None` then a new figure is created.
fp: `lsst.afw.detection.Footprint`
Footprint that contains the peak catalog for peaks in the image.
If `fp` is `None` then no peak positions are plotted.
figsize: tuple
Size of the `matplotlib.Figure` created.
If `ax` is not `None` then this parameter is ignored.
stretch: int
The linear stretch of the image.
Q: int
The Asinh softening parameter.
minimum: float
The minimum value to plot (as black).
"""
# If the image only has 3 bands, reverse the order of the bands to produce the RGB image
if len(image) == 3:
bgr = image.filters
# Extract the primary image component of each Exposure with the .image property, and use .array to get a NumPy array view.
rgb = make_lupton_rgb(image_r=image[bgr[2]].array, # numpy array for the r channel
image_g=image[bgr[1]].array, # numpy array for the g channel
image_b=image[bgr[0]].array, # numpy array for the b channel
stretch=stretch, Q=Q, minimum=minimum) # parameters used to stretch and scale the pixel values
if ax is None:
fig = plt.figure(figsize=figsize)
ax = fig.add_subplot(1,1,1)
# Exposure.getBBox() returns a Box2I, a box with integer pixel coordinates that correspond to the centers of pixels.
# Matplotlib's `extent` argument expects to receive the coordinates of the edges of pixels, which is what
# this Box2D (a box with floating-point coordinates) represents.
integerPixelBBox = image[bgr[0]].getBBox()
bbox = Box2D(integerPixelBBox)
ax.imshow(rgb, interpolation='nearest', origin='lower', extent=(bbox.getMinX(), bbox.getMaxX(), bbox.getMinY(), bbox.getMaxY()))
if fp is not None:
for peak in fp.getPeaks():
ax.plot(peak.getIx(), peak.getIy(), "bx", mew=2)