ocrd_cis/ocropy/common.py
def binarize(image,
             perc=90, # percentage for local whitelevel filters (ocropy: 80)
             range=10, # range (in pixels) for local whitelevel filters (ocropy: 20)
             zoom=1.0, # zoom factor for local whitelevel filters (ocropy: 0.5)
             escale=1.0, # exp scale for region mask during threshold estimation
             bignore=0.1, # ignore this much of the border for threshold estimation
             threshold=0.5, # final binarization threshold (ocropy: 0.5)
             lo=5, # percentile for black estimation (ocropy: 5)
             hi=90, # percentile for white estimation (ocropy: 90)
             maxskew=2, # maximum angle (in degrees) for skew estimation
             skewsteps=8, # steps per degree
             nrm=False): # output grayscale normalized
    """Binarize/grayscale-normalize image via locally adaptive thresholding.
    
    Return a tuple:
    - if ``nrm``, then the grayscale-normalized float array (bg=1.0),
      otherwise the binarized integer array (bg=1 / fg=0)
    - if ``maxskew>0``, then the deskewing angle (in degrees counter-cw) applied,
      otherwise zero
    """
    extreme = (np.sum(image < 0.05) + np.sum(image > 0.95)) * 1.0 / np.prod(image.shape)
    if extreme > 0.95:
        comment = "no-normalization"
        flat = image
    else:
        comment = ""
        # if not, we need to flatten it by estimating the local whitelevel
        flat = estimate_local_whitelevel(image, zoom, perc, range)
    if maxskew > 0:
        flat, angle = estimate_skew(flat, bignore, maxskew, skewsteps)
    else:
        angle = 0
    lo, hi = estimate_thresholds(flat, bignore, escale, lo, hi)
    if np.isnan(lo) or np.isnan(hi):
        LOG.warning("cannot estimate binarization thresholds (is the image empty?)")
    else:
        # normalize the image:
        flat -= lo
        if hi - lo:
            flat /= (hi - lo)
    flat = np.clip(flat, 0, 1)
    bin = 1 * (flat > threshold)
    LOG.debug("binarization: lo-hi (%.2f %.2f) angle %4.1f %s", lo, hi, angle, comment)
    return flat if nrm else bin, angle
Binarize/grayscale-normalize image via locally adaptive thresholding.
    
    Return a tuple:
    - if ``nrm``, then the grayscale-normalized float array (bg=1.0),
      otherwise the binarized integer array (bg=1 / fg=0)
    - if ``maxskew>0``, then the deskewing angle (in degrees counter-cw) applied,
      otherwise zero
    
Binarize/grayscale-normalize image via locally adaptive thresholding.
Binarize / grayscale - normalize image via locally adaptive thresholding .
def binarize(image,
             perc=90, 
             range=10, 
             zoom=1.0, 
             escale=1.0, 
             bignore=0.1, 
             threshold=0.5, 
             lo=5, 
             hi=90, 
             maxskew=2, 
             skewsteps=8, 
             nrm=False): 
    extreme = (np.sum(image < 0.05) + np.sum(image > 0.95)) * 1.0 / np.prod(image.shape)
    if extreme > 0.95:
        comment = "no-normalization"
        flat = image
    else:
        comment = ""
        flat = estimate_local_whitelevel(image, zoom, perc, range)
    if maxskew > 0:
        flat, angle = estimate_skew(flat, bignore, maxskew, skewsteps)
    else:
        angle = 0
    lo, hi = estimate_thresholds(flat, bignore, escale, lo, hi)
    if np.isnan(lo) or np.isnan(hi):
        LOG.warning("cannot estimate binarization thresholds (is the image empty?)")
    else:
        flat -= lo
        if hi - lo:
            flat /= (hi - lo)
    flat = np.clip(flat, 0, 1)
    bin = 1 * (flat > threshold)
    LOG.debug("binarization: lo-hi (%.2f %.2f) angle %4.1f %s", lo, hi, angle, comment)
    return flat if nrm else bin, angle
def binarize ( image , perc = 90 , range = 10 , zoom = 1.0 , escale = 1.0 , bignore = 0.1 , threshold = 0.5 , lo = 5 , hi = 90 , maxskew = 2 , skewsteps = 8 , nrm = False ) : extreme = ( np . sum ( image < 0.05 ) + np . sum ( image > 0.95 ) ) * 1.0 / np . prod ( image . shape ) if extreme > 0.95 : comment = "no-normalization" flat = image else : comment = "" flat = estimate_local_whitelevel ( image , zoom , perc , range ) if maxskew > 0 : flat , angle = estimate_skew ( flat , bignore , maxskew , skewsteps ) else : angle = 0 lo , hi = estimate_thresholds ( flat , bignore , escale , lo , hi ) if np . isnan ( lo ) or np . isnan ( hi ) : LOG . warning ( "cannot estimate binarization thresholds (is the image empty?)" ) else : flat -= lo if hi - lo : flat /= ( hi - lo ) flat = np . clip ( flat , 0 , 1 ) bin = 1 * ( flat > threshold ) LOG . debug ( "binarization: lo-hi (%.2f %.2f) angle %4.1f %s" , lo , hi , angle , comment ) return flat if nrm else bin , angle
Binarize/grayscale-normalize image via locally adaptive thresholding.
Binarize / grayscale - normalize image via locally adaptive thresholding .
# percentage for local whitelevel filters (ocropy: 80) # range (in pixels) for local whitelevel filters (ocropy: 20) # zoom factor for local whitelevel filters (ocropy: 0.5) # exp scale for region mask during threshold estimation # ignore this much of the border for threshold estimation # final binarization threshold (ocropy: 0.5) # percentile for black estimation (ocropy: 5) # percentile for white estimation (ocropy: 90) # maximum angle (in degrees) for skew estimation # steps per degree # output grayscale normalized """Binarize/grayscale-normalize image via locally adaptive thresholding.
    
    Return a tuple:
    - if ``nrm``, then the grayscale-normalized float array (bg=1.0),
      otherwise the binarized integer array (bg=1 / fg=0)
    - if ``maxskew>0``, then the deskewing angle (in degrees counter-cw) applied,
      otherwise zero
    """ # if not, we need to flatten it by estimating the local whitelevel # normalize the image:
image
perc
range
zoom
escale
bignore
threshold
lo
hi
maxskew
skewsteps
nrm