Python scipy.ndimage.morphology.binary_closing() Examples

The following are 3 code examples of scipy.ndimage.morphology.binary_closing(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.ndimage.morphology , or try the search function .
Example #1
Source File: morphologie.py    From TextileDefectDetection with GNU Affero General Public License v3.0 6 votes vote down vote up
def morphologie(img_name, target_dir, target_name):
    img = cv2.imread(img_name,cv2.IMREAD_GRAYSCALE)

    thresh_hor = 195
    thresh_ver = 60
    hor = cv2.threshold(img, thresh_hor, 255, cv2.THRESH_BINARY)[1]
    ver = 255-cv2.threshold(img, thresh_ver, 255, cv2.THRESH_BINARY)[1]

    mat = np.ones((5,5))
    hor = binary_opening(hor, structure=mat, iterations=2).astype(np.uint8) * 255
    #hor = binary_closing(hor, structure=mat, iterations=1).astype(np.uint8)*255

    #mat = np.ones((3,3))
    ver = binary_opening(ver, structure=mat, iterations=2).astype(np.uint8) * 255
    #ver = binary_closing(ver, structure=mat, iterations=1).astype(np.uint8)*255

    cv2.imwrite(os.path.join(target_dir, 'h' + target_name + '.png'), hor)
    cv2.imwrite(os.path.join(target_dir, 'v' + target_name + '.png'), ver) 
Example #2
Source File: find_pictures.py    From oldnyc with Apache License 2.0 5 votes vote down vote up
def ShowBinaryArray(b, title=None):
  im = Image.fromarray(255*np.uint8(b))
  im.show(im, title)

#showBinaryArray(B)
# this kills small features and introduces an 11px black border on every side
#B = binary_closing(B, structure=np.ones((11,11)))
#showBinaryArray(B)
#
#sys.exit(0) 
Example #3
Source File: linegen.py    From kraken with Apache License 2.0 4 votes vote down vote up
def degrade_line(im, eta=0.0, alpha=1.5, beta=1.5, alpha_0=1.0, beta_0=1.0):
    """
    Degrades a line image by adding noise.

    For parameter meanings consult [1].

    Args:
        im (PIL.Image): Input image
        eta (float):
        alpha (float):
        beta (float):
        alpha_0 (float):
        beta_0 (float):

    Returns:
        PIL.Image in mode '1'
    """
    logger.debug('Inverting and normalizing input image')
    im = pil2array(im)
    im = np.amax(im)-im
    im = im*1.0/np.amax(im)

    logger.debug('Calculating foreground distance transform')
    fg_dist = distance_transform_cdt(1-im, metric='taxicab')
    logger.debug('Calculating flip to white probability')
    fg_prob = alpha_0 * np.exp(-alpha * (fg_dist**2)) + eta
    fg_prob[im == 1] = 0
    fg_flip = np.random.binomial(1, fg_prob)

    logger.debug('Calculating background distance transform')
    bg_dist = distance_transform_cdt(im, metric='taxicab')
    logger.debug('Calculating flip to black probability')
    bg_prob = beta_0 * np.exp(-beta * (bg_dist**2)) + eta
    bg_prob[im == 0] = 0
    bg_flip = np.random.binomial(1, bg_prob)

    # flip
    logger.debug('Flipping')
    im -= bg_flip
    im += fg_flip

    logger.debug('Binary closing')
    sel = np.array([[1, 1], [1, 1]])
    im = binary_closing(im, sel)
    logger.debug('Converting to image')
    return array2pil(255-im.astype('B')*255)