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

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

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

# 

# LSST Data Management System 

# Copyright 2015-2016 LSST/AURA 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <https://www.lsstcorp.org/LegalNotices/>. 

# 

 

import numpy as np 

 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

from lsst.afw.display.displayLib import replaceSaturatedPixels, getZScale 

 

 

def computeIntensity(imageR, imageG=None, imageB=None): 

"""!Return a naive total intensity from the red, blue, and green intensities 

@param imageR intensity of image that'll be mapped to red; or intensity if imageG and imageB are None 

@param imageG intensity of image that'll be mapped to green; or None 

@param imageB intensity of image that'll be mapped to blue; or None 

 

Inputs may be MaskedImages, Images, or numpy arrays and the return is of the same type 

""" 

if imageG is None or imageB is None: 

assert imageG is None and imageB is None, \ 

"Please specify either a single image or red, green, and blue images" 

return imageR 

 

imageRGB = [imageR, imageG, imageB] 

 

for i, c in enumerate(imageRGB): 

46 ↛ 47line 46 didn't jump to line 47, because the condition on line 46 was never true if hasattr(c, "getImage"): 

c = imageRGB[i] = c.getImage() 

if hasattr(c, "getArray"): 

imageRGB[i] = c.getArray() 

 

intensity = (imageRGB[0] + imageRGB[1] + imageRGB[2])/float(3) 

# 

# Repack into whatever type was passed to us 

# 

Image = afwImage.ImageU if intensity.dtype == 'uint16' else afwImage.ImageF 

 

57 ↛ 58line 57 didn't jump to line 58, because the condition on line 57 was never true if hasattr(imageR, "getImage"): # a maskedImage 

intensity = afwImage.makeMaskedImage(Image(intensity)) 

elif hasattr(imageR, "getArray"): 

intensity = Image(intensity) 

 

return intensity 

 

 

class Mapping: 

"""!Baseclass to map red, blue, green intensities into uint8 values""" 

 

def __init__(self, minimum=None, image=None): 

"""!Create a mapping 

@param minimum Intensity that should be mapped to black (a scalar or array for R, G, B) 

@param image The image to be used to calculate the mapping. 

 

If provided, also the default for makeRgbImage() 

""" 

self._uint8Max = float(np.iinfo(np.uint8).max) 

 

try: 

len(minimum) 

except TypeError: 

minimum = 3*[minimum] 

assert len(minimum) == 3, "Please provide 1 or 3 values for minimum" 

 

self.minimum = minimum 

self._image = image 

 

def makeRgbImage(self, imageR=None, imageG=None, imageB=None, 

xSize=None, ySize=None, rescaleFactor=None): 

"""!Convert 3 arrays, imageR, imageG, and imageB into a numpy RGB image 

@param imageR Image to map to red (if None, use the image passed to the ctor) 

@param imageG Image to map to green (if None, use imageR) 

@param imageB Image to map to blue (if None, use imageR) 

@param xSize Desired width of RGB image (or None). If ySize is None, preserve aspect ratio 

@param ySize Desired height of RGB image (or None) 

@param rescaleFactor Make size of output image rescaleFactor*size of the input image (or None) 

 

N.b. images may be afwImage.Images or numpy arrays 

""" 

if imageR is None: 

99 ↛ 100line 99 didn't jump to line 100, because the condition on line 99 was never true if self._image is None: 

raise RuntimeError( 

"You must provide an image (or pass one to the constructor)") 

imageR = self._image 

 

if imageG is None: 

imageG = imageR 

if imageB is None: 

imageB = imageR 

 

imageRGB = [imageR, imageG, imageB] 

for i, c in enumerate(imageRGB): 

if hasattr(c, "getImage"): 

c = imageRGB[i] = c.getImage() 

113 ↛ 110line 113 didn't jump to line 110, because the condition on line 113 was never false if hasattr(c, "getArray"): 

imageRGB[i] = c.getArray() 

 

116 ↛ 117line 116 didn't jump to line 117, because the condition on line 116 was never true if xSize is not None or ySize is not None: 

assert rescaleFactor is None, "You may not specify a size and rescaleFactor" 

h, w = imageRGB[0].shape 

if ySize is None: 

ySize = int(xSize*h/float(w) + 0.5) 

elif xSize is None: 

xSize = int(ySize*w/float(h) + 0.5) 

 

size = (ySize, xSize) # n.b. y, x order for scipy 

125 ↛ 126line 125 didn't jump to line 126, because the condition on line 125 was never true elif rescaleFactor is not None: 

size = float(rescaleFactor) # an int is intepreted as a percentage 

else: 

size = None 

 

130 ↛ 131line 130 didn't jump to line 131, because the condition on line 130 was never true if size is not None: 

try: 

import scipy.misc 

except ImportError as e: 

raise RuntimeError( 

"Unable to rescale as scipy.misc is unavailable: %s" % e) 

 

for i, im in enumerate(imageRGB): 

imageRGB[i] = scipy.misc.imresize( 

im, size, interp='bilinear', mode='F') 

 

return np.dstack(self._convertImagesToUint8(*imageRGB)).astype(np.uint8) 

 

def intensity(self, imageR, imageG, imageB): 

"""!Return the total intensity from the red, blue, and green intensities 

 

This is a naive computation, and may be overridden by subclasses 

""" 

return computeIntensity(imageR, imageG, imageB) 

 

def mapIntensityToUint8(self, intensity): 

"""Map an intensity into the range of a uint8, [0, 255] (but not converted to uint8)""" 

with np.errstate(invalid='ignore', divide='ignore'): # n.b. np.where can't and doesn't short-circuit 

return np.where(intensity <= 0, 0, 

np.where(intensity < self._uint8Max, intensity, self._uint8Max)) 

 

def _convertImagesToUint8(self, imageR, imageG, imageB): 

"""Use the mapping to convert images imageR, imageG, and imageB to a triplet of uint8 images""" 

imageR = imageR - self.minimum[0] # n.b. makes copy 

imageG = imageG - self.minimum[1] 

imageB = imageB - self.minimum[2] 

 

fac = self.mapIntensityToUint8(self.intensity(imageR, imageG, imageB)) 

 

imageRGB = [imageR, imageG, imageB] 

with np.errstate(invalid="ignore"): # suppress NAN warnings 

for c in imageRGB: 

c *= fac 

# individual bands can still be < 0, even if fac isn't 

c[c < 0] = 0 

 

pixmax = self._uint8Max 

# copies -- could work row by row to minimise memory usage 

r0, g0, b0 = imageRGB 

 

# n.b. np.where can't and doesn't short-circuit 

with np.errstate(invalid='ignore', divide='ignore'): 

for i, c in enumerate(imageRGB): 

c = np.where(r0 > g0, 

np.where(r0 > b0, 

np.where(r0 >= pixmax, c*pixmax/r0, c), 

np.where(b0 >= pixmax, c*pixmax/b0, c)), 

np.where(g0 > b0, 

np.where(g0 >= pixmax, c*pixmax/g0, c), 

np.where(b0 >= pixmax, c*pixmax/b0, c))).astype(np.uint8) 

c[c > pixmax] = pixmax 

 

imageRGB[i] = c 

 

return imageRGB 

 

 

class LinearMapping(Mapping): 

"""!A linear map map of red, blue, green intensities into uint8 values""" 

 

def __init__(self, minimum=None, maximum=None, image=None): 

"""!A linear stretch from [minimum, maximum]; if one or both are omitted use image minimum/maximum to set them 

 

@param minimum Intensity that should be mapped to black (a scalar or array for R, G, B) 

@param maximum Intensity that should be mapped to white (a scalar) 

@param image Image to estimate minimum/maximum if not explicitly set 

""" 

 

if minimum is None or maximum is None: 

assert image is not None, "You must provide an image if you don't set both minimum and maximum" 

 

stats = afwMath.makeStatistics(image, afwMath.MIN | afwMath.MAX) 

207 ↛ 209line 207 didn't jump to line 209, because the condition on line 207 was never false if minimum is None: 

minimum = stats.getValue(afwMath.MIN) 

209 ↛ 212line 209 didn't jump to line 212, because the condition on line 209 was never false if maximum is None: 

maximum = stats.getValue(afwMath.MAX) 

 

Mapping.__init__(self, minimum, image) 

self.maximum = maximum 

 

215 ↛ 216line 215 didn't jump to line 216, because the condition on line 215 was never true if maximum is None: 

self._range = None 

else: 

assert maximum - minimum != 0, "minimum and maximum values must not be equal" 

self._range = float(maximum - minimum) 

 

def mapIntensityToUint8(self, intensity): 

"""Return an array which, when multiplied by an image, returns that image mapped to the range of a 

uint8, [0, 255] (but not converted to uint8) 

 

The intensity is assumed to have had minimum subtracted (as that can be done per-band) 

""" 

with np.errstate(invalid='ignore', divide='ignore'): # n.b. np.where can't and doesn't short-circuit 

return np.where(intensity <= 0, 0, 

np.where(intensity >= self._range, 

self._uint8Max/intensity, self._uint8Max/self._range)) 

 

 

class ZScaleMapping(LinearMapping): 

"""!A mapping for a linear stretch chosen by the zscale algorithm 

(preserving colours independent of brightness) 

 

x = (I - minimum)/range 

""" 

 

def __init__(self, image, nSamples=1000, contrast=0.25): 

"""!A linear stretch from [z1, z2] chosen by the zscale algorithm 

@param image Image whose parameters are desired 

@param nSamples The number of samples to use to estimate the zscale parameters 

@param contrast The number of samples to use to estimate the zscale parameters 

""" 

 

if not hasattr(image, "getArray"): 

image = afwImage.ImageF(image) 

z1, z2 = getZScale(image, nSamples, contrast) 

 

LinearMapping.__init__(self, z1, z2, image) 

 

 

class AsinhMapping(Mapping): 

"""!A mapping for an asinh stretch (preserving colours independent of brightness) 

 

x = asinh(Q (I - minimum)/range)/Q 

 

This reduces to a linear stretch if Q == 0 

 

See http://adsabs.harvard.edu/abs/2004PASP..116..133L 

""" 

 

def __init__(self, minimum, dataRange, Q=8): 

Mapping.__init__(self, minimum) 

 

# 32bit floating point machine epsilon; sys.float_info.epsilon is 64bit 

epsilon = 1.0/2**23 

269 ↛ 270line 269 didn't jump to line 270, because the condition on line 269 was never true if abs(Q) < epsilon: 

Q = 0.1 

else: 

Qmax = 1e10 

273 ↛ 274line 273 didn't jump to line 274, because the condition on line 273 was never true if Q > Qmax: 

Q = Qmax 

 

if False: 

self._slope = self._uint8Max/Q # gradient at origin is self._slope 

else: 

frac = 0.1 # gradient estimated using frac*range is _slope 

self._slope = frac*self._uint8Max/np.arcsinh(frac*Q) 

 

self._soften = Q/float(dataRange) 

 

def mapIntensityToUint8(self, intensity): 

"""Return an array which, when multiplied by an image, returns that image mapped to the range of a 

uint8, [0, 255] (but not converted to uint8) 

 

The intensity is assumed to have had minimum subtracted (as that can be done per-band) 

""" 

with np.errstate(invalid='ignore', divide='ignore'): # n.b. np.where can't and doesn't short-circuit 

return np.where(intensity <= 0, 0, np.arcsinh(intensity*self._soften)*self._slope/intensity) 

 

 

class AsinhZScaleMapping(AsinhMapping): 

"""!A mapping for an asinh stretch, estimating the linear stretch by zscale 

 

x = asinh(Q (I - z1)/(z2 - z1))/Q 

 

See AsinhMapping 

""" 

 

def __init__(self, image, Q=8, pedestal=None): 

"""! 

Create an asinh mapping from an image, setting the linear part of the stretch using zscale 

 

@param image The image to analyse, or a list of 3 images to be converted to an intensity image 

@param Q The asinh softening parameter 

@param pedestal The value, or array of 3 values, to subtract from the images; or None 

 

N.b. pedestal, if not None, is removed from the images when calculating the zscale 

stretch, and added back into Mapping.minimum[] 

""" 

try: 

assert len(image) in (1, 3,), "Please provide 1 or 3 images" 

except TypeError: 

image = [image] 

 

if pedestal is not None: 

try: 

assert len(pedestal) in ( 

1, 3,), "Please provide 1 or 3 pedestals" 

except TypeError: 

pedestal = 3*[pedestal] 

 

image = list(image) # needs to be mutable 

for i, im in enumerate(image): 

327 ↛ 326line 327 didn't jump to line 326, because the condition on line 327 was never false if pedestal[i] != 0.0: 

328 ↛ 329line 328 didn't jump to line 329, because the condition on line 328 was never true if hasattr(im, "getImage"): 

im = im.getImage() 

330 ↛ 333line 330 didn't jump to line 333, because the condition on line 330 was never false if hasattr(im, "getArray"): 

im = im.getArray() 

 

image[i] = im - pedestal[i] # n.b. a copy 

else: 

pedestal = len(image)*[0.0] 

 

image = computeIntensity(*image) 

 

zscale = ZScaleMapping(image) 

# zscale.minimum is always a triple 

dataRange = zscale.maximum - zscale.minimum[0] 

minimum = zscale.minimum 

 

for i, level in enumerate(pedestal): 

minimum[i] += level 

 

AsinhMapping.__init__(self, minimum, dataRange, Q) 

self._image = image # support self.makeRgbImage() 

 

 

def makeRGB(imageR, imageG=None, imageB=None, minimum=0, dataRange=5, Q=8, fileName=None, 

saturatedBorderWidth=0, saturatedPixelValue=None, 

xSize=None, ySize=None, rescaleFactor=None): 

"""Make a set of three images into an RGB image using an asinh stretch and optionally write it to disk 

 

If saturatedBorderWidth is non-zero, replace saturated pixels with saturatedPixelValue. Note 

that replacing saturated pixels requires that the input images be MaskedImages. 

""" 

359 ↛ 360line 359 didn't jump to line 360, because the condition on line 359 was never true if imageG is None: 

imageG = imageR 

361 ↛ 362line 361 didn't jump to line 362, because the condition on line 361 was never true if imageB is None: 

imageB = imageR 

 

364 ↛ 371line 364 didn't jump to line 371, because the condition on line 364 was never false if saturatedBorderWidth: 

365 ↛ 366line 365 didn't jump to line 366, because the condition on line 365 was never true if saturatedPixelValue is None: 

raise ValueError( 

"saturatedPixelValue must be set if saturatedBorderWidth is set") 

replaceSaturatedPixels(imageR, imageG, imageB, 

saturatedBorderWidth, saturatedPixelValue) 

 

asinhMap = AsinhMapping(minimum, dataRange, Q) 

rgb = asinhMap.makeRgbImage(imageR, imageG, imageB, 

xSize=xSize, ySize=ySize, rescaleFactor=rescaleFactor) 

 

375 ↛ 378line 375 didn't jump to line 378, because the condition on line 375 was never false if fileName: 

writeRGB(fileName, rgb) 

 

return rgb 

 

 

def displayRGB(rgb, show=True): 

"""!Display an rgb image using matplotlib 

@param rgb The RGB image in question 

@param show If true, call plt.show() 

""" 

import matplotlib.pyplot as plt 

plt.imshow(rgb, interpolation='nearest', origin="lower") 

if show: 

plt.show() 

return plt 

 

 

def writeRGB(fileName, rgbImage): 

"""!Write an RGB image to disk 

@param fileName The output file. The suffix defines the format, and must be supported by matplotlib 

@param rgbImage The image, as made by e.g. makeRGB 

 

Most versions of matplotlib support png and pdf (although the eps/pdf/svg writers may be buggy, 

possibly due an interaction with useTeX=True in the matplotlib settings). 

 

If your matplotlib bundles pil/pillow you should also be able to write jpeg and tiff files. 

""" 

import matplotlib.image 

matplotlib.image.imsave(fileName, rgbImage) 

 

# 

# Support the legacy API 

# 

 

 

class asinhMappingF: # noqa N801 

"""!@deprecated Object used to support legacy API""" 

 

def __init__(self, minimum, dataRange, Q): 

self.minimum = minimum 

self.dataRange = dataRange 

self.Q = Q 

 

 

class _RgbImageF: 

"""!@deprecated Object used to support legacy API""" 

 

def __init__(self, imageR, imageG, imageB, mapping): 

"""!@deprecated Legacy API""" 

asinh = AsinhMapping(mapping.minimum, mapping.dataRange, mapping.Q) 

self.rgb = asinh.makeRgbImage(imageR, imageG, imageB) 

 

def write(self, fileName): 

"""!@deprecated Legacy API""" 

writeRGB(fileName, self.rgb) 

 

 

def RgbImageF(imageR, imageG, imageB, mapping): 

"""!@deprecated Legacy API""" 

return _RgbImageF(imageR, imageG, imageB, mapping)