LSSTApplications  19.0.0-10-g920eed2,19.0.0-11-g48a0200+2,19.0.0-18-gfc4e62b+17,19.0.0-2-g3b2f90d+2,19.0.0-2-gd671419+5,19.0.0-20-g5a5a17ab+15,19.0.0-21-g2644856+18,19.0.0-23-g84eeccb+6,19.0.0-24-g878c510+5,19.0.0-25-g6c8df7140,19.0.0-25-gb330496+5,19.0.0-3-g2b32d65+5,19.0.0-3-g8227491+16,19.0.0-3-g9c54d0d+16,19.0.0-3-gca68e65+12,19.0.0-3-gcfc5f51+5,19.0.0-3-ge110943+15,19.0.0-3-ge74d124,19.0.0-3-gfe04aa6+16,19.0.0-30-g9c3fd16+6,19.0.0-4-g06f5963+5,19.0.0-4-g3d16501+18,19.0.0-4-g4a9c019+5,19.0.0-4-g5a8b323,19.0.0-4-g66397f0+1,19.0.0-4-g8278b9b+1,19.0.0-4-g8557e14,19.0.0-4-g8964aba+17,19.0.0-4-ge404a01+16,19.0.0-5-g40f3a5a,19.0.0-5-g4db63b3,19.0.0-5-gfb03ce7+17,19.0.0-6-gbaebbfb+16,19.0.0-61-gec4c6e08+6,19.0.0-7-g039c0b5+16,19.0.0-7-gbea9075+4,19.0.0-7-gc567de5+17,19.0.0-70-g334bf3e+1,19.0.0-9-g463f923+16,b.20.0.x-g5487ab2134,v20.0.0.rc1
LSSTDataManagementBasePackage
utils.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-2017 LSST/AURA.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 
23 __all__ = ["clipImage", "resetFilters", "defineFilter",
24  "projectImage", "getProjectionIndices"]
25 
26 import numpy as np
27 
28 import lsst.afw.detection as afwDetect
29 from .maskedImage import MaskedImage, makeMaskedImage
30 from .image import Mask
31 from .filter import Filter, FilterProperty
32 
33 
34 def clipImage(im, minClip, maxClip):
35  """Clip an image to lie between minClip and maxclip (None to ignore)"""
36  if isinstance(im, MaskedImage):
37  mi = im
38  else:
39  mi = makeMaskedImage(im, Mask(im.getDimensions()))
40 
41  if minClip is not None:
43  mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False))
44  afwDetect.setImageFromFootprintList(
45  mi.getImage(), ds.getFootprints(), minClip)
46 
47  if maxClip is not None:
49  afwDetect.setImageFromFootprintList(
50  mi.getImage(), ds.getFootprints(), maxClip)
51 
52 
54  """Reset registry of filters and filter properties"""
55  Filter.reset()
56  FilterProperty.reset()
57 
58 
59 def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False):
60  """Define a filter and its properties in the filter registry"""
61  prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force)
62  Filter.define(prop)
63  if isinstance(alias, str):
64  Filter.defineAlias(name, alias)
65  else:
66  for a in alias:
67  Filter.defineAlias(name, a)
68 
69 
70 def getProjectionIndices(imageBBox, targetBBox):
71  """Get the indices to project an image
72 
73  Given an image and target bounding box,
74  calculate the indices needed to appropriately
75  slice the input image and target image to
76  project the image to the target.
77 
78  Parameters
79  ----------
80  imageBBox: Box2I
81  Bounding box of the input image
82  targetBBox: Box2I
83  Bounding box of the target image
84 
85  Returns
86  -------
87  targetSlices: tuple
88  Slices of the target image in the form (by, bx), (iy, ix).
89  imageIndices: tuple
90  Slices of the input image in the form (by, bx), (iy, ix).
91  """
92  def getMin(dXmin):
93  """Get minimum indices"""
94  if dXmin < 0:
95  bxStart = -dXmin
96  ixStart = 0
97  else:
98  bxStart = 0
99  ixStart = dXmin
100  return bxStart, ixStart
101 
102  def getMax(dXmax):
103  """Get maximum indices"""
104  if dXmax < 0:
105  bxEnd = None
106  ixEnd = dXmax
107  elif dXmax != 0:
108  bxEnd = -dXmax
109  ixEnd = None
110  else:
111  bxEnd = ixEnd = None
112  return bxEnd, ixEnd
113 
114  dXmin = targetBBox.getMinX() - imageBBox.getMinX()
115  dXmax = targetBBox.getMaxX() - imageBBox.getMaxX()
116  dYmin = targetBBox.getMinY() - imageBBox.getMinY()
117  dYmax = targetBBox.getMaxY() - imageBBox.getMaxY()
118 
119  bxStart, ixStart = getMin(dXmin)
120  byStart, iyStart = getMin(dYmin)
121  bxEnd, ixEnd = getMax(dXmax)
122  byEnd, iyEnd = getMax(dYmax)
123 
124  bx = slice(bxStart, bxEnd)
125  by = slice(byStart, byEnd)
126  ix = slice(ixStart, ixEnd)
127  iy = slice(iyStart, iyEnd)
128  return (by, bx), (iy, ix)
129 
130 
131 def projectImage(image, bbox, fill=0):
132  """Project an image into a bounding box
133 
134  Return a new image whose pixels are equal to those of
135  `image` within `bbox`, and equal to `fill` outside.
136 
137  Parameters
138  ----------
139  image: `afw.Image` or `afw.MaskedImage`
140  The image to project
141  bbox: `Box2I`
142  The bounding box to project onto.
143  fill: number
144  The value to fill the region of the new
145  image outside the bounding box of the original.
146 
147  Returns
148  -------
149  newImage: `afw.Image` or `afw.MaskedImage`
150  The new image with the input image projected
151  into its bounding box.
152  """
153  if image.getBBox() == bbox:
154  return image
155  (by, bx), (iy, ix) = getProjectionIndices(image.getBBox(), bbox)
156 
157  if isinstance(image, MaskedImage):
158  newImage = type(image.image)(bbox)
159  newImage.array[by, bx] = image.image.array[iy, ix]
160  newMask = type(image.mask)(bbox)
161  newMask.array[by, bx] = image.mask.array[iy, ix]
162  newVariance = type(image.image)(bbox)
163  newVariance.array[by, bx] = image.variance.array[iy, ix]
164  newImage = MaskedImage(image=newImage, mask=newMask, variance=newVariance, dtype=newImage.array.dtype)
165  else:
166  newImage = type(image)(bbox)
167  if fill != 0:
168  newImage.set(fill)
169  newImage.array[by, bx] = image.array[iy, ix]
170  return newImage
lsst::afw::image.utils.defineFilter
def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False)
Definition: utils.py:59
lsst::afw::image::Mask
Represent a 2-dimensional array of bitmask pixels.
Definition: Mask.h:77
lsst::afw::detection::FootprintSet
A set of Footprints, associated with a MaskedImage.
Definition: FootprintSet.h:53
lsst::afw::detection::Threshold
A Threshold is used to pass a threshold value to detection algorithms.
Definition: Threshold.h:43
lsst::afw::image.utils.resetFilters
def resetFilters()
Definition: utils.py:53
lsst::afw::detection
Definition: Footprint.h:50
type
table::Key< int > type
Definition: Detector.cc:163
lsst::afw::image.utils.clipImage
def clipImage(im, minClip, maxClip)
Definition: utils.py:34
lsst::afw::image.maskedImage.maskedImageContinued.MaskedImage
Definition: maskedImageContinued.py:39
lsst::afw::image::makeMaskedImage
MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > * makeMaskedImage(typename std::shared_ptr< Image< ImagePixelT >> image, typename std::shared_ptr< Mask< MaskPixelT >> mask=Mask< MaskPixelT >(), typename std::shared_ptr< Image< VariancePixelT >> variance=Image< VariancePixelT >())
A function to return a MaskedImage of the correct type (cf.
Definition: MaskedImage.h:1279
lsst::afw::image.utils.projectImage
def projectImage(image, bbox, fill=0)
Definition: utils.py:131
lsst::afw::image.utils.getProjectionIndices
def getProjectionIndices(imageBBox, targetBBox)
Definition: utils.py:70