def stretch_hist_equalize(self, ch_nb): """Stretch the current image's colors by performing histogram equalization on channel *ch_nb*. """ LOG.info("Perform a histogram equalized contrast stretch.") if(self.channels[ch_nb].size == np.ma.count_masked(self.channels[ch_nb])): LOG.warning("Nothing to stretch !") return arr = self.channels[ch_nb] nwidth = 2048.0 carr = arr.compressed() imhist, bins = np.histogram(carr, nwidth, normed=True) cdf = imhist.cumsum() - imhist[0] cdf = cdf / cdf[-1] res = np.ma.empty_like(arr) res.mask = np.ma.getmaskarray(arr) res[~res.mask] = np.interp(carr, bins[:-1], cdf) self.channels[ch_nb] = res
def stretch_linear(self, ch_nb, cutoffs=(0.005, 0.005)): """Stretch linearly the contrast of the current image on channel *ch_nb*, using *cutoffs* for left and right trimming. """ LOG.debug("Perform a linear contrast stretch.") if((self.channels[ch_nb].size == np.ma.count_masked(self.channels[ch_nb])) or self.channels[ch_nb].min() == self.channels[ch_nb].max()): LOG.warning("Nothing to stretch !") return nwidth = 2048.0 arr = self.channels[ch_nb] carr = arr.compressed() hist, bins = np.histogram(carr, nwidth) ndim = carr.size left = 0 hist_sum = 0.0 i = 0 while i < nwidth and hist_sum < cutoffs[0]*ndim: hist_sum = hist_sum + hist[i] i = i + 1 left = bins[i-1] right = 0 hist_sum = 0.0 i = nwidth - 1 while i >= 0 and hist_sum < cutoffs[1]*ndim: hist_sum = hist_sum + hist[i] i = i - 1 right = bins[i+1] delta_x = (right - left) LOG.debug("Interval: left=%f,right=%f width=%f" %(left,right,delta_x)) if delta_x > 0.0: self.channels[ch_nb] = np.ma.array((arr - left) / delta_x, mask = arr.mask) else: self.channels[ch_nb] = np.ma.zeros(arr.shape) LOG.warning("Unable to make a contrast stretch!")
def crude_stretch(self, ch_nb, min_stretch = None, max_stretch = None): """Perform simple linear stretching (without any cutoff) on the channel *ch_nb* of the current image and normalize to the [0,1] range.""" if(min_stretch is None): min_stretch = self.channels[ch_nb].min() if(max_stretch is None): max_stretch = self.channels[ch_nb].max() if((not self.channels[ch_nb].mask.all()) and max_stretch - min_stretch > 0): stretched = ((self.channels[ch_nb].data - min_stretch) * 1.0 / (max_stretch - min_stretch)) self.channels[ch_nb] = np.ma.array(stretched, mask=self.channels[ch_nb].mask) else: LOG.warning("Nothing to stretch !")
def stretch_logarithmic(self, ch_nb, factor=100.): """Move data into range [1:factor] and do a normalized logarithmic enhancement. """ LOG.debug("Perform a logarithmic contrast stretch.") if ((self.channels[ch_nb].size == np.ma.count_masked(self.channels[ch_nb])) or (self.channels[ch_nb].min() == self.channels[ch_nb].max())): LOG.warning("Nothing to stretch !") return crange=(0., 1.0) arr = self.channels[ch_nb] b = float(crange[1] - crange[0])/np.log(factor) c = float(crange[0]) slope = (factor-1.)/float(arr.max() - arr.min()) arr = 1. + (arr - arr.min())*slope arr = c + b*np.log(arr) self.channels[ch_nb] = arr