예제 #1
0
def closely(np_org_image, number_of_inters=3, struct_elem='rect', size=3):
    """Execute closely (erosion on dilated image) on a given image 

    Arguments:
        np_org_image {NumPy array} -- image for erosion

    Keyword Arguments:
        struct_elem {str} -- stuctural element used during erosion  (default: {'rect'})
            rect - rectange
            cross - cross
        size {int} -- size of the structural element. Should be odd and greater than 2 (default: {3})
        number_of_inters {int} -- number of inter images generated by this function (minimum/default: {3})

    Returns:
        [list of NumPy arrays] -- list containing original image, specified number of inter images and final image
    """

    #arguments validation
    bs.validate_number_of_inters(number_of_inters, closely.__defaults__[-1])

    processed_inters = 0
    images = []
    images.append(np_org_image)

    #grayscale
    np_image_2D, isConverted = bs.ensureGrayscale(np_org_image, info=True)
    if isConverted:
        images.append(np_image_2D)
        processed_inters += 1

    #binarization
    np_image_bin = bn.otsuBinarization(images[-1], only_threshold=False)
    processed_inters += 1
    images.append(np_image_bin)

    #dilate part
    np_image_dil = bn.dilate(images[-1], struct_elem, size)
    #calculate number of inters to erode operation
    dilate_inters = math.floor((number_of_inters - processed_inters) / 2 - 1)
    if dilate_inters > 0:
        images += bs.generateInterImages(images[-1], np_image_dil,
                                         processed_inters + dilate_inters,
                                         processed_inters)
        processed_inters += dilate_inters
    else:
        images.append(np_image_dil)

    #one from final dilate image
    processed_inters += 1

    #erode part
    np_final = bn.erode(np_image_dil, struct_elem, size)
    images += bs.generateInterImages(images[-1], np_final, number_of_inters,
                                     processed_inters)
    return images
예제 #2
0
def dilation(np_org_image, struct_elem='rect', size=3, number_of_inters=2):
    """Execute dilation on a given image

    Arguments:
        np_org_image {NumPy array} -- image for dilation

    Keyword Arguments:
        struct_elem {str} -- stuctural element used during dilation (default: {'rect'})
            rect - rectange
            cross - cross   
        size {int} -- size of the structural element. Should be odd and greater than 2 (default: {3})
        number_of_inters {int} -- number of inter images generated by this function (minimum/default: {2})

    Returns:
        [list of NumPy arrays] -- list containing original image, specified number of inter images and final image
    """

    #arguments validation
    bs.validate_number_of_inters(number_of_inters, dilation.__defaults__[-1])

    #variable initialization
    processed_inters = 0
    images = []
    images.append(np_org_image)

    #grayscale
    np_image_2D, isConverted = bs.ensureGrayscale(np_org_image, info=True)
    if isConverted:
        images.append(np_image_2D)
        processed_inters += 1

    #binarization
    np_image_bin = bn.otsuBinarization(images[-1], only_threshold=False)
    processed_inters += 1
    images.append(np_image_bin)

    #dilation
    np_final = bn.dilate(images[-1], struct_elem, size)
    images += bs.generateInterImages(images[-1], np_final, number_of_inters,
                                     processed_inters)
    return images
예제 #3
0
def binarization(np_org_image, threshold, number_of_inters=1):
    """Perform binarization on a given image using specified threshold value

    Arguments:
        np_org_image {NumPy array} -- image for binarization
        threshold {[type]} -- binarization threshold from range <0,255>

    Keyword Arguments:
        number_of_inters {int} -- number of inter images generated by this function (minimum/default: {2})

    Returns:
        [list of NumPy arrays] -- list containing original image, specified number of inter images and final image
    """

    #arguments validation
    #bs.validate_number_of_inters(number_of_inters, binarization.__defaults__[-1])
    if threshold > 255 or threshold < 0:
        raise ValueError(
            f"Threshold value out of <0,255> range. The value was {threshold}")

    processed_inters = 0
    images = []
    images.append(np_org_image)

    #grayscale
    np_image_2D, isConverted = bs.ensureGrayscale(np_image, info=True)
    if isConverted:
        images.append(np_image_2D)
        processed_inters += 1

    #binarization
    np_final = bn.thresholdBinarization(np_image_2D, threshold)
    #number_of_inters = 0 indicates that function was called by other api function
    images += bs.generateInterImages(images[-1], np_final, number_of_inters,
                                     processed_inters)
    return images