def bright_object_detection(image): """ Perform bright object detection on an array image.""" # Store all intermediate steps in a dictionary. Useful for debugging. steps = dict() steps['input'] = image # Reduce noise using a median filter. med_filter_size = (MED_SIZE, MED_SIZE, MED_SIZE) steps['median'] = ndimg.median_filter(steps['input'], med_filter_size) # Convert median filtered image to grayscale. steps['luminance'] = scikits.image.color.rgb2gray(steps['median']) * 255. # Compute local pixel average. k_avg = np.ones((AVG_SIZE, AVG_SIZE)) / AVG_SIZE**2 steps['average'] = ndimg.convolve(steps['luminance'], k_avg) # Compute local pixel variance. steps['diff_mean'] = steps['luminance'] - steps['average'] steps['diff_mean_sq'] = steps['diff_mean'] * steps['diff_mean'] steps['variance'] = ndimg.convolve(steps['diff_mean_sq'], k_avg) # Compute binary threshold image using mahalonobis distance. Use the sign # of the difference between the pixel and its local mean to ignore dark # pixels. steps['maha_sq'] = (steps['diff_mean'] > 0) * steps['diff_mean_sq'] / \ steps['variance'] steps['thresh_maha'] = (steps['maha_sq'] > (NUM_STDDEV * NUM_STDDEV)) # Integrate global illumination effects by taking a top percentage of # intensities from the detected light regions. steps['masked_regions_lum'] = steps['thresh_maha'] * steps['luminance'] steps['masked_regions_hist'] = pymorph.histogram(steps['masked_regions_lum']) steps['global_bright_thresh'] = int((len(steps['masked_regions_hist']) * \ (1.0 - GLOBAL_BRIGHT_PCT)) + 0.5) steps['thresh_global'] = steps['masked_regions_lum'] >= \ steps['global_bright_thresh'] # Morphological operations on detected blobs. steps['detect_erode'] = pymorph.erode(steps['thresh_global']) steps['detect_dilate'] = pymorph.dilate(steps['detect_erode']) # Count bright objects. Connected components and raw pixels. steps['detect_labels'] = pymorph.label(steps['detect_dilate']) steps['bright_blob_count'] = steps['detect_labels'].max() steps['bright_pixel_count'] = sum(steps['masked_regions_hist'] [steps['global_bright_thresh']:]) return steps
# <demo> auto print "Detected light regions using maha with {0} "\ "standard deviations:".format(NUM_STDDEV) plab.imshow(pymorph.overlay(steps['luminance'].astype('uint8'), steps['thresh_maha'])) # <demo> stop # <demo> auto ############################################################################### # Integrate global illumination effects by taking a top percentage of # intensities from the detected light regions. steps['masked_regions_lum'] = steps['thresh_maha'] * steps['luminance'] steps['masked_regions_hist'] = pymorph.histogram(steps['masked_regions_lum']) steps['global_bright_thresh'] = int((len(steps['masked_regions_hist']) * \ (1.0 - GLOBAL_BRIGHT_PCT)) + 0.5) steps['thresh_global'] = steps['masked_regions_lum'] >= \ steps['global_bright_thresh'] print "Global filtered mask:" plab.imshow(pymorph.overlay(steps['luminance'].astype('uint8'), steps['thresh_global'])) ############################################################################### # Morpohological operations on detected blobs. # <demo> stop # <demo> auto steps['detect_erode'] = pymorph.erode(steps['thresh_global'])
def test_histogram(): A = (np.random.rand(200, 300) * 255).astype(np.uint8) H = pymorph.histogram(A) for v, c in enumerate(H): assert (A == v).sum() == c
def test_histogram(): A = (np.random.rand(200,300)*255).astype(np.uint8) H = pymorph.histogram(A) for v,c in enumerate(H): assert (A == v).sum() == c