def test_get_similarity_returns_valid_similarities_for_templates(): beam_contour, _ = get_largest_contour(beam_image_01) for key, item in vars(templates).items(): if isinstance(item, np.ndarray): template_contour, _ = get_largest_contour(to_gray(item)) reflexive_similarity = get_similarity(template_contour, template=key) assert(reflexive_similarity == 0.0) beam_similarity_str = get_similarity(beam_contour, template=key) assert(0 <= beam_similarity_str) beam_similarity_arr = get_similarity(beam_contour, template=template_contour) assert(beam_similarity_str == beam_similarity_arr)
def test_process_image_returns_correct_arrays(): resize = 1.0 kernel = (3, 3) uint_mode = "clip" thresh_mode = "otsu" thresh_factor = 3 for image in beam_images: process_array = process_image(image, kernel=kernel, uint_mode=uint_mode, thresh_mode=thresh_mode, resize=resize, thresh_factor=thresh_factor) # Check the shape is as expected assert (process_array.shape == (10, )) # Perform the same process try: image_prep = uint_resize_gauss(image, fx=resize, fy=resize, kernel=kernel) contour, area = get_largest_contour(image_prep, factor=thresh_factor, thesh_mode=thresh_mode) M = get_moments(contour=contour) centroid_y, centroid_x = [pos // resize for pos in get_centroid(M)] l, w = [val // resize for val in get_contour_size(contour=contour)] match = get_similarity(contour) except NoContoursDetected: area = -1 centroid_y, centroid_x = [-1, -1] l = -1 w = -1 match = -1 mean_raw = image.mean() mean_prep = image_prep.mean() sum_raw = image.sum() sum_prep = image_prep.sum() # Put it all together test_array = np.array([ mean_raw, mean_prep, sum_raw, sum_prep, area, centroid_x, centroid_y, l, w, match ]) assert (process_array.all() == test_array.all())
def test_get_similarity_raises_inputerror_on_invalid_template_type(): with pytest.raises(InputError): get_similarity(np.zeros((10)), template=False)
def test_get_similarity_raises_inputerror_on_non_contour_array(): with pytest.raises(InputError): get_similarity(np.zeros((10)), template=np.zeros((10,10)))
def process_image(image, resize=1.0, kernel=(13,13), uint_mode="scale", thresh_mode="otsu", thresh_factor=3): """ Processes the input image and returns a vector of numbers charcterizing the beam. Parameters ---------- image : np.ndarray Image to process resize : float, optional Resize the image before performing any processing. kernel : tuple, optional Size of kernel to use when running the gaussian filter. uint_mode : str, optional Conversion mode to use when converting to uint8. For extended documentation, see preprocessing.to_uint8. Valid modes are: ['clip', 'norm', 'scale'] thresh_mode : str, optional Thresholding mode to use. For extended documentation see preprocessing.threshold_image. Valid modes are: ['mean', 'top', 'bottom', 'adaptive', 'otsu'] thresh_factor : int, float Factor to pass to the mean threshold. Returns ------- np.ndarray Array containing all the relevant fields of the image """ # Preprocess with a gaussian filter image_prep = uint_resize_gauss(image, fx=resize, fy=resize, kernel=kernel, mode=uint_mode) # The main pipeline try: contour, area = get_largest_contour(image_prep, thesh_mode=thresh_mode, factor=thresh_factor) M = get_moments(contour=contour) centroid_y, centroid_x = [pos//resize for pos in get_centroid(M)] l, w = [val//resize for val in get_contour_size(contour=contour)] match = get_similarity(contour) # No beam on Image, set values to make this clear except NoContoursDetected: area = -1 centroid_y, centroid_x = [-1, -1] l = -1 w = -1 match = -1 # Basic info mean_raw = image.mean() mean_prep = image_prep.mean() sum_raw = image.sum() sum_prep = image_prep.sum() return np.array([sum_raw, sum_prep, mean_raw, mean_prep, area, centroid_x, centroid_y, l, w, match])