def calculate_threshold(sweep, trusted_range): from scipy.ndimage.measurements import histogram from thresholding import maximum_deviation import numpy threshold_list = [] for i, flex_image in enumerate(sweep): # Get image as numpy array image = flex_image.as_numpy_array() # Cap pixels to within trusted range image.shape = -1 ind = numpy.where(image < trusted_range[0]) image[ind] = trusted_range[0] ind = numpy.where(image > trusted_range[1]) image[ind] = trusted_range[1] # Histogram the pixels histo = histogram(image, trusted_range[0], trusted_range[1], trusted_range[1]) histo = histo / numpy.sum(histo) # Calculate the threshold and add to list threshold = maximum_deviation(histo) threshold_list.append(threshold) # Return mean threshold return numpy.mean(threshold_list)
def calculate_threshold(image, trusted_range): from scipy.ndimage.measurements import histogram from thresholding import maximum_deviation import numpy # Cap pixels to within trusted range image.shape = -1 ind = numpy.where(image < trusted_range[0]) image[ind] = trusted_range[0] ind = numpy.where(image > trusted_range[1]) image[ind] = trusted_range[1] # Histogram the pixels histo = histogram(image, trusted_range[0], trusted_range[1], trusted_range[1]) histo = histo / numpy.sum(histo) # Calculate the threshold and add to list return maximum_deviation(histo)
image.shape = -1 ind = numpy.where(image < trusted_range[0]) image[ind] = trusted_range[0] ind = numpy.where(image > trusted_range[1]) image[ind] = trusted_range[1] mean = numpy.mean(image) sdev = numpy.std(image) var = sdev ** 2 temp = histogram(image, trusted_range[0], trusted_range[1], trusted_range[1]) temp = temp / numpy.sum(temp) histo.append(temp) # threshold = bhthreshold(temp) threshold = maximum_deviation(temp) # threshold = percentage_threshold(temp) # threshold = normal_threshold(temp) threshold_list.append(threshold) print( "{0} - Mean: {1}, Sdev: {2}, Var: {3}, Thres: {4}".format( i, mean, sdev, var, threshold ) ) print( "Threshold - Mean: {0}, Sdev: {1}".format( numpy.mean(threshold_list), numpy.std(threshold_list) ) )
image.shape = -1 ind = numpy.where(image < trusted_range[0]) image[ind] = trusted_range[0] ind = numpy.where(image > trusted_range[1]) image[ind] = trusted_range[1] mean = numpy.mean(image) sdev = numpy.std(image) var = sdev **2 temp = histogram(image, trusted_range[0], trusted_range[1], trusted_range[1]) temp = temp / numpy.sum(temp) histo.append(temp) #threshold = bhthreshold(temp) threshold = maximum_deviation(temp) #threshold = percentage_threshold(temp) # threshold = normal_threshold(temp) threshold_list.append(threshold) print "{0} - Mean: {1}, Sdev: {2}, Var: {3}, Thres: {4}".format( i, mean, sdev, var, threshold) print "Threshold - Mean: {0}, Sdev: {1}".format( numpy.mean(threshold_list), numpy.std(threshold_list)) mean_thresh = numpy.mean(threshold_list) image = sweep[start:stop].to_array().as_numpy_array() mask = image > mean_thresh