예제 #1
0
def detect_contours(input_file, thresh_val=255, k_size=5, iterations=30):
    """
    >>> contours = detect_contours(test_image)
    >>> isinstance(detect_contours(test_image), list)
    True

    >>> detect_contours(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be a None object

    >>> detect_contours("")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be ''.

    >>> detect_contours("fakeRoute")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: Input file not found.

    >>> detect_contours(test_image, thresh_val=270)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: All threshold values must be between 0 and 255.

    >>> detect_contours(test_image, k_size=-10)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Kernel size value must be greater than 0.

    >>> detect_contours(test_image, iterations=-10)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Iterations value must be greater than 0.
    """

    gray = iu.get_image(input_file, 0)

    # Checking arguments
    check_threshold(thresh_val)
    check_kernel(k_size)
    check_iterations(iterations)



    gray = iu.pyramid_clean(gray)

    th2 = th.adaptive_threshold(gray, max_val=thresh_val,
                                mode=cv.ADAPTIVE_THRESH_MEAN_C)

    th2 = cv.erode(th2, kernel=(k_size, k_size), iterations=iterations)

    th2 = cv.bitwise_not(th2)

    contours, h = cv.findContours(th2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

    return contours
예제 #2
0
def detect_contours(input_file, thresh_val=255, k_size=5, iterations=30):
    """
    >>> contours = detect_contours(test_image)
    >>> isinstance(detect_contours(test_image), list)
    True

    >>> detect_contours(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be a None object

    >>> detect_contours("")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be ''.

    >>> detect_contours("fakeRoute")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: Input file not found.

    >>> detect_contours(test_image, thresh_val=270)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: All threshold values must be between 0 and 255.

    >>> detect_contours(test_image, k_size=-10)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Kernel size value must be greater than 0.

    >>> detect_contours(test_image, iterations=-10)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Iterations value must be greater than 0.
    """

    gray = iu.get_image(input_file, 0)

    # Checking arguments
    check_threshold(thresh_val)
    check_kernel(k_size)
    check_iterations(iterations)



    gray = iu.pyramid_clean(gray)

    th2 = th.adaptive_threshold(gray, max_val=thresh_val,
                                mode=cv.ADAPTIVE_THRESH_MEAN_C)

    th2 = cv.erode(th2, kernel=(k_size, k_size), iterations=iterations)

    th2 = cv.bitwise_not(th2)

    contours, h = cv.findContours(th2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

    return contours
예제 #3
0
def remove_bg(input_file, thresh_val=0, window_size=3, block_size=11, c=5,
              mode=0, thresh_type=0):
    """
    >>> isinstance(remove_bg(test_image), np.ndarray)
    True

    >>> isinstance(remove_bg(test_image, mode=1), np.ndarray)
    True

    >>> isinstance(remove_bg(test_image, mode=2), np.ndarray)
    True

    >>> remove_bg(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be a None object

    >>> remove_bg("")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be ''.

    >>> remove_bg("fakeRoute")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: Input file not found.

    >>> remove_bg(test_image, window_size=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Window size value must be greater than 0.

    >>> remove_bg(test_image, window_size=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Window size value must be odd.

    >>> remove_bg(test_image, block_size=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Block size value must be greater than 0.

    >>> remove_bg(test_image, block_size=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Block size value must be odd.

    >>> remove_bg(test_image, c='a')
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Constraint must be integer.

    >>> remove_bg(test_image, thresh_type=-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Threshold_type value must be between 0 and 4.

    >>> remove_bg(test_image, thresh_type=6)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Threshold_type value must be between 0 and 4.

    >>> remove_bg(test_image, mode=-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Thres_type value must be between 0 and 2 (0-Adapt-Gauss, 1-Adapt-Mean, 2-Simple).

    >>> remove_bg(test_image, mode=3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Thres_type value must be between 0 and 2 (0-Adapt-Gauss, 1-Adapt-Mean, 2-Simple).
    """

    # Checking arguments
    check_threshold(thresh_val)
    check_window_size(window_size)
    check_block_size(block_size)
    check_c(c)
    check_mode(mode)

    # Loading image
    image = iu.get_image(input_file)

    # Removing noise by blurring and thresholding
    image = cv.GaussianBlur(image, (window_size, window_size), 0)

    result = []

    if mode == 2:
        result = th.threshold(image, thresh_val, thresh_type=thresh_type)
    else:
        result = th.adaptive_threshold(image, block_size=block_size, c=c,
                                       mode=mode, thresh_type=thresh_type)

    return result
예제 #4
0
def clean(input_file, thresh_val=200, window_size=5, kernel_size=5):
    """
    >>> isinstance(clean(test_image), np.ndarray)
    True

    >>> clean(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be a None object

    >>> clean("")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be ''.

    >>> clean("fakeRoute")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: Input file not found.

    >>> clean(test_image, thresh_val=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: All threshold values must be between 0 and 255.

    >>> clean(test_image, thresh_val=260)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: All threshold values must be between 0 and 255.

    >>> clean(test_image, window_size=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Window size value must be greater than 0.

    >>> clean(test_image, window_size=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Window size value must be odd.

    >>> clean(test_image, kernel_size=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Kernel size value must be greater than 0.

    >>> clean(test_image, kernel_size=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Kernel size value must be odd.
    """

    # Checking arguments
    check_kernel_size(kernel_size)
    check_threshold(thresh_val)
    check_window_size(window_size)

    # Loading the image
    image = iu.get_image(input_file, 0)

    # Applying Gaussian and median blur and erode
    image = cv.GaussianBlur(image, (window_size, window_size), 0)
    image = cv.medianBlur(image, window_size)
    image = cv.erode(image, (kernel_size, kernel_size))

    return th.adaptive_threshold(image, thresh_val)
예제 #5
0
def clean(input_file,  thresh_val=200, window_size=5, kernel_size=5):
    """
    >>> isinstance(clean(test_image), np.ndarray)
    True

    >>> clean(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be a None object

    >>> clean("")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be ''.

    >>> clean("fakeRoute")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: Input file not found.

    >>> clean(test_image, thresh_val=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: All threshold values must be between 0 and 255.

    >>> clean(test_image, thresh_val=260)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: All threshold values must be between 0 and 255.

    >>> clean(test_image, window_size=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Window size value must be greater than 0.

    >>> clean(test_image, window_size=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Window size value must be odd.

    >>> clean(test_image, kernel_size=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Kernel size value must be greater than 0.

    >>> clean(test_image, kernel_size=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Kernel size value must be odd.
    """

    # Checking arguments
    check_kernel_size(kernel_size)
    check_threshold(thresh_val)
    check_window_size(window_size)

    # Loading the image
    image = iu.get_image(input_file, 0)

    # Applying Gaussian and median blur and erode
    image = cv.GaussianBlur(image, (window_size, window_size), 0)
    image = cv.medianBlur(image, window_size)
    image = cv.erode(image, (kernel_size, kernel_size))

    return th.adaptive_threshold(image, thresh_val)
예제 #6
0
def remove_bg(input_file,
              thresh_val=0,
              window_size=3,
              block_size=11,
              c=5,
              mode=0,
              thresh_type=0):
    """
    >>> isinstance(remove_bg(test_image), np.ndarray)
    True

    >>> isinstance(remove_bg(test_image, mode=1), np.ndarray)
    True

    >>> isinstance(remove_bg(test_image, mode=2), np.ndarray)
    True

    >>> remove_bg(None)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be a None object

    >>> remove_bg("")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: The input file can't be ''.

    >>> remove_bg("fakeRoute")
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    IOError: Input file not found.

    >>> remove_bg(test_image, window_size=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Window size value must be greater than 0.

    >>> remove_bg(test_image, window_size=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Window size value must be odd.

    >>> remove_bg(test_image, block_size=-3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Block size value must be greater than 0.

    >>> remove_bg(test_image, block_size=2)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Block size value must be odd.

    >>> remove_bg(test_image, c='a')
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Constraint must be integer.

    >>> remove_bg(test_image, thresh_type=-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Threshold_type value must be between 0 and 4.

    >>> remove_bg(test_image, thresh_type=6)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Threshold_type value must be between 0 and 4.

    >>> remove_bg(test_image, mode=-1)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Thres_type value must be between 0 and 2 (0-Adapt-Gauss, 1-Adapt-Mean, 2-Simple).

    >>> remove_bg(test_image, mode=3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    ValueError: Thres_type value must be between 0 and 2 (0-Adapt-Gauss, 1-Adapt-Mean, 2-Simple).
    """

    # Checking arguments
    check_threshold(thresh_val)
    check_window_size(window_size)
    check_block_size(block_size)
    check_c(c)
    check_mode(mode)

    # Loading image
    image = iu.get_image(input_file)

    # Removing noise by blurring and thresholding
    image = cv.GaussianBlur(image, (window_size, window_size), 0)

    result = []

    if mode == 2:
        result = th.threshold(image, thresh_val, thresh_type=thresh_type)
    else:
        result = th.adaptive_threshold(image,
                                       block_size=block_size,
                                       c=c,
                                       mode=mode,
                                       thresh_type=thresh_type)

    return result