def find_hist_peaks(img, drop_first_max=False):
'''Finds the first two interior peaks in the histogram; usually a good binary
threshold lies halfway between these two peaks.'''
import scipy.signal as sig
array = sitk.GetArrayFromImage(img)
hist = np.histogram(array, 16)
hist_counts = np.array(hist[0])
hist_centers = np.array(hist[1])
peaks = sig.argrelextrema(hist_counts, np.greater)[0]
if hist_counts[peaks[0]] < (0.01 * (hist_counts[peaks].mean())): # Don't allow a "noise" peak at the beginning.
peaks = peaks[1:]
if len(peaks) < 2: # Only one peak is found if the other peak is really at the edge.
peaks = np.array([0, peaks[0]])
elif hist_counts[0] > hist_counts[peaks[0]]:
if not drop_first_max:
peaks = np.array([0, peaks[0]])
elif drop_first_max:
peaks = np.array([peaks[1], peaks[2]])
i_L = hist_centers[peaks[0]]
i_FM = hist_centers[peaks[1]]
return i_L, i_FM
评论列表
文章目录