def onViewHist(self): self.I2 = self.Ilast if self.I2.ndim==2 : # calculate the histogram h = myHist.imhist(self.I2) # and show the histogram markerline, stemlines, baseline = plt.stem(np.arange(0, 256), h, '-') plt.setp(markerline, 'marker', '.', 'markerfacecolor', 'none') plt.setp(stemlines, 'color', 'b') plt.setp(baseline, 'color','black', 'linewidth', 3) plt.plot(np.arange(0, 256), h, color = 'black', linewidth = 2) plt.title("Histogram") plt.xlabel("Intensity Values (8 bit) rk") plt.ylabel("p(rk)") plt.show() else : # calculate the histogram for each R, G and B components hR = myHist.imhist( self.I2[:, :, 0]) hG = myHist.imhist( self.I2[:, :, 1]) hB = myHist.imhist( self.I2[:, :, 2]) # and show the output plt.plot(np.arange(0, 256), hR, color = 'red', linewidth = 2, label = 'Red Intensities') plt.plot(np.arange(0, 256), hG, color = 'green', linewidth = 2, label = 'Blue Intensities') plt.plot(np.arange(0, 256), hB, color = 'blue', linewidth = 2, label = 'Green Intensities') plt.legend(loc='upper left') plt.title("Histogram") plt.xlabel("Intensity Values (8 bit) rk") plt.ylabel("p(rk)") plt.show()
def gbThresh(im): if im.ndim == 3: im = gray(im) h = myHist.imhist(im) i = np.arange(0, 256) # all intensity values globalMean = np.uint8(np.sum(h * i)) # global mean T = globalMean # initialize threshold as globalMean while True: M1 = np.uint16(np.sum(h[0:T] * np.arange(0, T)) / np.sum(h[0:T])) M2 = np.uint16(np.sum(h[T:255] * np.arange(T, 255)) / np.sum(h[T:255])) T2 = (M1 + M2) / 2 if abs(T2 - T) <= 1: break T = T2 return T, h
def otsu(im): if im.ndim == 3: im = gray(im) h = myHist.imhist(im) cdf = np.array(myHist.cumsum(h)) # cumulative distribution function i = np.arange(0, 256) # all intensity values cumMean = np.array(myHist.cumsum(h * i)) # cumulative mean for all intnsities globalMean = np.sum(h * i) # global mean # calculate between class variance varB = np.floor(np.power(globalMean * cdf - cumMean, 2) / (cdf * (1 - cdf))) # threshold k* is the value of k in varB(k) for which varB(k) is maximum thresh = np.where(varB == np.nanmax(varB)) thresh = np.mean(thresh) return thresh, h