Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of afw. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22__all__ = ["clipImage", "resetFilters", "defineFilter", 

23 "projectImage", "getProjectionIndices"] 

24 

25from deprecated.sphinx import deprecated 

26 

27import numpy as np 

28 

29import lsst.afw.detection as afwDetect 

30from .maskedImage import MaskedImage, makeMaskedImage 

31from .image import Mask 

32from ._filter import Filter, FilterProperty 

33 

34 

35def clipImage(im, minClip, maxClip): 

36 """Clip an image to lie between minClip and maxclip (None to ignore)""" 

37 if isinstance(im, MaskedImage): 

38 mi = im 

39 else: 

40 mi = makeMaskedImage(im, Mask(im.getDimensions())) 

41 

42 if minClip is not None: 

43 ds = afwDetect.FootprintSet( 

44 mi, afwDetect.Threshold(-minClip, afwDetect.Threshold.VALUE, False)) 

45 afwDetect.setImageFromFootprintList( 

46 mi.getImage(), ds.getFootprints(), minClip) 

47 

48 if maxClip is not None: 

49 ds = afwDetect.FootprintSet(mi, afwDetect.Threshold(maxClip)) 

50 afwDetect.setImageFromFootprintList( 

51 mi.getImage(), ds.getFootprints(), maxClip) 

52 

53 

54@deprecated(reason=("Removed with no replacement (FilterLabels do not need to be reset)." 

55 " Will be removed after v22."), category=FutureWarning, version="v22") 

56def resetFilters(): 

57 """Reset registry of filters and filter properties""" 

58 Filter.reset() 

59 FilterProperty.reset() 

60 

61 

62@deprecated(reason=("Removed with no replacement (but see lsst::afw::image::TransmissionCurve for how to set" 

63 "and retrieve filter wavelength information). Will be removed after v22."), 

64 category=FutureWarning, version="v22") 

65def defineFilter(name, lambdaEff, lambdaMin=np.nan, lambdaMax=np.nan, alias=[], force=False): 

66 """Define a filter and its properties in the filter registry""" 

67 prop = FilterProperty(name, lambdaEff, lambdaMin, lambdaMax, force) 

68 Filter.define(prop) 

69 if isinstance(alias, str): 

70 Filter.defineAlias(name, alias) 

71 else: 

72 for a in alias: 

73 Filter.defineAlias(name, a) 

74 

75 

76def getProjectionIndices(imageBBox, targetBBox): 

77 """Get the indices to project an image 

78 

79 Given an image and target bounding box, 

80 calculate the indices needed to appropriately 

81 slice the input image and target image to 

82 project the image to the target. 

83 

84 Parameters 

85 ---------- 

86 imageBBox: Box2I 

87 Bounding box of the input image 

88 targetBBox: Box2I 

89 Bounding box of the target image 

90 

91 Returns 

92 ------- 

93 targetSlices: tuple 

94 Slices of the target image in the form (by, bx), (iy, ix). 

95 imageIndices: tuple 

96 Slices of the input image in the form (by, bx), (iy, ix). 

97 """ 

98 def getMin(dXmin): 

99 """Get minimum indices""" 

100 if dXmin < 0: 

101 bxStart = -dXmin 

102 ixStart = 0 

103 else: 

104 bxStart = 0 

105 ixStart = dXmin 

106 return bxStart, ixStart 

107 

108 def getMax(dXmax): 

109 """Get maximum indices""" 

110 if dXmax < 0: 

111 bxEnd = None 

112 ixEnd = dXmax 

113 elif dXmax != 0: 

114 bxEnd = -dXmax 

115 ixEnd = None 

116 else: 

117 bxEnd = ixEnd = None 

118 return bxEnd, ixEnd 

119 

120 dXmin = targetBBox.getMinX() - imageBBox.getMinX() 

121 dXmax = targetBBox.getMaxX() - imageBBox.getMaxX() 

122 dYmin = targetBBox.getMinY() - imageBBox.getMinY() 

123 dYmax = targetBBox.getMaxY() - imageBBox.getMaxY() 

124 

125 bxStart, ixStart = getMin(dXmin) 

126 byStart, iyStart = getMin(dYmin) 

127 bxEnd, ixEnd = getMax(dXmax) 

128 byEnd, iyEnd = getMax(dYmax) 

129 

130 bx = slice(bxStart, bxEnd) 

131 by = slice(byStart, byEnd) 

132 ix = slice(ixStart, ixEnd) 

133 iy = slice(iyStart, iyEnd) 

134 return (by, bx), (iy, ix) 

135 

136 

137def projectImage(image, bbox, fill=0): 

138 """Project an image into a bounding box 

139 

140 Return a new image whose pixels are equal to those of 

141 `image` within `bbox`, and equal to `fill` outside. 

142 

143 Parameters 

144 ---------- 

145 image: `afw.Image` or `afw.MaskedImage` 

146 The image to project 

147 bbox: `Box2I` 

148 The bounding box to project onto. 

149 fill: number 

150 The value to fill the region of the new 

151 image outside the bounding box of the original. 

152 

153 Returns 

154 ------- 

155 newImage: `afw.Image` or `afw.MaskedImage` 

156 The new image with the input image projected 

157 into its bounding box. 

158 """ 

159 if image.getBBox() == bbox: 

160 return image 

161 (by, bx), (iy, ix) = getProjectionIndices(image.getBBox(), bbox) 

162 

163 if isinstance(image, MaskedImage): 

164 newImage = type(image.image)(bbox) 

165 newImage.array[by, bx] = image.image.array[iy, ix] 

166 newMask = type(image.mask)(bbox) 

167 newMask.array[by, bx] = image.mask.array[iy, ix] 

168 newVariance = type(image.image)(bbox) 

169 newVariance.array[by, bx] = image.variance.array[iy, ix] 

170 newImage = MaskedImage(image=newImage, mask=newMask, variance=newVariance, dtype=newImage.array.dtype) 

171 else: 

172 newImage = type(image)(bbox) 

173 if fill != 0: 

174 newImage.set(fill) 

175 newImage.array[by, bx] = image.array[iy, ix] 

176 return newImage