def clean_noise(image, mask=None):
    """
    Goal: Cleaning orange band noise with mask

    Applied mask on image then erode and dilate on 3 iteration.
    Applied subtract image and mask and add to image modify before
    And finally, erode and dilate again

    :param image: Binary Image
    :param mask:
    :return: Binary image
    """
    if mask is not None:
        image_modify = cv2.bitwise_and(image, mask)
    else:
        image_modify = image

    image_modify = ocv2.erode(image_modify, iterations=3)
    image_modify = ocv2.dilate(image_modify, iterations=3)

    if mask is not None:
        res = cv2.subtract(image, mask)
    else:
        res = image

    res = cv2.add(res, image_modify)

    res = ocv2.erode(res)
    res = ocv2.dilate(res)

    return res
Beispiel #2
0
def top_binarization_hsv(image, configuration):
    """

    :param image:
    :param configuration:
    :return:
    """
    configuration.print_value()

    # c = configuration.cubicle_domain
    # image_cropped = image[c[0]:c[1], c[2]:c[3]]
    hsv_image = ocv2.bgr2hsv(image)

    #   =======================================================================
    #   Main area segmentation
    main_area_seg = cv2.medianBlur(hsv_image, ksize=9)
    main_area_seg = cv2.inRange(main_area_seg,
                                configuration.roi_main.hsv_min,
                                configuration.roi_main.hsv_max)

    main_area_seg = ocv2.dilate(main_area_seg, iterations=2)
    main_area_seg = ocv2.erode(main_area_seg, iterations=2)
    #
    # mask_cropped = configuration.roi_main.mask[c[0]:c[1], c[2]:c[3]]
    # main_area_seg = cv2.bitwise_and(main_area_seg,
    #                                 main_area_seg,
    #                                 mask=mask_cropped)

    # image_out = numpy.zeros([image.shape[0], image.shape[1]], 'uint8')
    # image_out[c[0]:c[1], c[2]:c[3]] = main_area_seg

    return main_area_seg
def clean_noise(binary_image, mask=None):
    """
    Goal: Cleaning orange band noise with mask

    Applied mask on image then erode and dilate on 3 iteration.
    Applied subtract image and mask and add to image modify before
    And finally, erode and dilate again

    Parameters
    ----------
    binary_image : numpy.ndarray
        2-D array

    mask : numpy.ndarray, optional
        Array of same shape as `image`. Only points at which mask == True
        will be processed.

    Returns
    -------
    out : numpy.ndarray
        Binary Image
    """
    # ==========================================================================
    # Check Parameters
    if not isinstance(binary_image, numpy.ndarray):
        raise TypeError('binary_image must be a numpy.ndarray')

    if binary_image.ndim != 2:
        raise ValueError('binary_image must be 2D array')

    if mask is not None:
        if not isinstance(mask, numpy.ndarray):
            raise TypeError('mask must be a numpy.ndarray')
        if mask.ndim != 2:
            raise ValueError('mask must be 2D array')
    # ==========================================================================
    if mask is not None:
        out = cv2.bitwise_and(binary_image, mask)
    else:
        out = binary_image.copy()

    element = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))
    out = cv2.erode(out, element, iterations=3)
    out = cv2.dilate(out, element, iterations=3)

    if mask is not None:
        res = cv2.subtract(binary_image, mask)
        out = cv2.add(res, out)

    out = ocv2.erode(out)
    out = ocv2.dilate(out)

    return out
Beispiel #4
0
def side_binarization_hsv(image, configuration):
    """
    Binarization of side image for Lemnatech  cabin based on hsv segmentation.

    Based on Michael pipeline
    :param image: BGR image
    :param configuration: Object BinarizationConfig
    :return: Binary image
    """

    # elementMorph = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3))

    c = configuration.cubicle_domain
    image_cropped = image[c[0]:c[1], c[2]:c[3]]
    hsv_image = ocv2.bgr2hsv(image_cropped)

    #   =======================================================================
    #   Main area segmentation
    main_area_seg = cv2.medianBlur(hsv_image, ksize=9)
    main_area_seg = cv2.inRange(main_area_seg,
                                configuration.roi_main.hsv_min,
                                configuration.roi_main.hsv_max)

    main_area_seg = ocv2.dilate(main_area_seg, iterations=2)
    main_area_seg = ocv2.erode(main_area_seg, iterations=2)

    mask_cropped = configuration.roi_main.mask[c[0]:c[1], c[2]:c[3]]
    main_area_seg = cv2.bitwise_and(main_area_seg,
                                    main_area_seg,
                                    mask=mask_cropped)

    #   =======================================================================
    #   Band area segmentation
    background_cropped = configuration.background[c[0]:c[1], c[2]:c[3]]
    hsv_background = ocv2.bgr2hsv(background_cropped)
    grayscale_background = hsv_background[:, :, 0]

    grayscale_image = hsv_image[:, :, 0]

    band_area_seg = cv2.subtract(grayscale_image, grayscale_background)
    retval, band_area_seg = cv2.threshold(band_area_seg,
                                          122,
                                          255,
                                          cv2.THRESH_BINARY)

    mask_cropped = configuration.roi_orange_band.mask[c[0]:c[1], c[2]:c[3]]
    band_area_seg = cv2.bitwise_and(band_area_seg,
                                    band_area_seg,
                                    mask=mask_cropped)

    band_area_seg = ocv2.erode(band_area_seg, iterations=5)
    band_area_seg = ocv2.dilate(band_area_seg, iterations=5)

    #   =======================================================================
    #   Pot area segmentation
    pot_area_seg = cv2.inRange(hsv_image,
                               configuration.roi_pot.hsv_min,
                               configuration.roi_pot.hsv_max)

    mask_cropped = configuration.roi_pot.mask[c[0]:c[1], c[2]:c[3]]
    pot_area_seg = cv2.bitwise_and(pot_area_seg,
                                   pot_area_seg,
                                   mask=mask_cropped)

    #   =======================================================================
    #   Full segmented image
    image_seg = cv2.add(main_area_seg, band_area_seg)
    image_seg = cv2.add(image_seg, pot_area_seg)

    image_out = numpy.zeros([image.shape[0], image.shape[1]], 'uint8')
    image_out[c[0]:c[1], c[2]:c[3]] = image_seg

    return image_out