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# 

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 

26import numpy as np 

27 

28import lsst.afw.detection as afwDetect 

29from .maskedImage import MaskedImage, makeMaskedImage 

30from .image import Mask 

31from .filter import Filter, FilterProperty 

32 

33 

34def 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: 

42 ds = afwDetect.FootprintSet( 

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: 

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

49 afwDetect.setImageFromFootprintList( 

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

51 

52 

53def resetFilters(): 

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

55 Filter.reset() 

56 FilterProperty.reset() 

57 

58 

59def 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 

70def 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 

131def 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