Пример #1
0
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)
Пример #2
0
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])
Пример #3
0
def test_get_largest_contour_returns_largest_contour_of_contours():
    lenna_thr = threshold_image(to_gray(lenna), mode="otsu")
    _, lenna_cnts_cv2, _ = cv2.findContours(lenna_thr, 1, 2)
    lenna_cnts_area = np.array([cv2.contourArea(cnt) for cnt in lenna_cnts_cv2])
    lenna_largest_area_cv2 = lenna_cnts_area.max()
    lenna_largest_cnt_cv2 = lenna_cnts_cv2[np.argmax(lenna_cnts_area)]
    lenna_largest_cnt_psb, lenna_largest_area_psb = get_largest_contour(
        contours=lenna_cnts_cv2, thresh_mode="otsu")
    assert(lenna_largest_area_cv2 == lenna_largest_area_psb)
    assert(lenna_largest_cnt_cv2.all() == lenna_largest_cnt_psb.all())
Пример #4
0
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())
Пример #5
0
def test_get_largest_contour_raises_inputerror_on_no_inputs():
    with pytest.raises(InputError):
        get_largest_contour()
Пример #6
0
def test_get_contour_size_returns_correct_contour_size_of_contour():
    circle_largest_cnt, _ = get_largest_contour(image=circle,
                                                thresh_mode="otsu")
    _, _, w, l = cv2.boundingRect(circle_largest_cnt)
    assert((l, w) == get_contour_size(contour=circle_largest_cnt))
Пример #7
0
def test_get_bounding_box_returns_correct_bounding_box_of_contour():
    circle_largest_cnt, _ = get_largest_contour(image=circle,
                                                thresh_mode="otsu")
    circle_bounding_rect_cv2 = cv2.boundingRect(circle_largest_cnt)
    assert(circle_bounding_rect_cv2 == get_bounding_box(
        contour=circle_largest_cnt))
Пример #8
0
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])