def test_get_moments_returns_correct_moments_of_contour(): circle_largest_cnt, _ = get_largest_contour(image=circle, thresh_mode="otsu") moments_cv2 = cv2.moments(circle_largest_cnt) moments_psb = get_moments(contour=circle_largest_cnt) for m in moments_cv2.keys(): assert(m in moments_psb.keys()) assert(moments_cv2[m] == moments_psb[m])
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_centroid_returns_correct_centroids(): moments = get_moments(image=circle) cent_x = int(moments['m10']/moments['m00']) cent_y = int(moments['m01']/moments['m00']) assert(get_centroid(moments) == (cent_x, cent_y))
def test_get_moments_raises_inputerror_on_no_inputs(): with pytest.raises(InputError): get_moments()
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])