ocrd_cis/ocropy/common.py
def check_region(binary, zoom=1.0):
    """Validate binary as a plausible text region image.

    Given a binarized, inverted image as Numpy array `binary`
    (with 0 for white and 1 for black),
    check the array has the right dimensions, is in fact inverted,
    and does not have too few or too many connected black components.

    Returns an error report, or None if valid.
    """
    if np.prod(binary.shape)==0: return "image dimensions are zero"
    if len(binary.shape)==3: return "image is not monochrome %s"%(binary.shape,)
    if np.amax(binary)==np.amin(binary): return "image is blank"
    if np.mean(binary)<np.median(binary): return "image may be inverted"
    h,w = binary.shape
    if h<45/zoom: return "image not tall enough for a region image %s"%(binary.shape,)
    if h>5000/zoom: return "image too tall for a region image %s"%(binary.shape,)
    if w<100/zoom: return "image too narrow for a region image %s"%(binary.shape,)
    if w>5000/zoom: return "image too wide for a region image %s"%(binary.shape,)
    # zoom factor (DPI relative) and 4 (against fragmentation from binarization)
    slots = int(w*h*1.0/(30*30)*zoom*zoom) * 4
    _,ncomps = measurements.label(binary)
    if ncomps<5: return "too few connected components for a region image (got %d)"%(ncomps,)
    if ncomps>slots and ncomps>10: return "too many connected components for a region image (%d > %d)"%(ncomps,slots)
    return None
Validate binary as a plausible text region image.

    Given a binarized, inverted image as Numpy array `binary`
    (with 0 for white and 1 for black),
    check the array has the right dimensions, is in fact inverted,
    and does not have too few or too many connected black components.

    Returns an error report, or None if valid.
    
Validate binary as a plausible text region image.
Given a binarized, inverted image as Numpy array `binary`
(with 0 for white and 1 for black),
check the array has the right dimensions, is in fact inverted,
and does not have too few or too many connected black components.

Returns an error report, or None if valid.
Validate binary as a plausible text region image . Given a binarized inverted image as Numpy array ` binary ` ( with 0 for white and 1 for black ) check the array has the right dimensions is in fact inverted and does not have too few or too many connected black components . Returns an error report or None if valid .
def check_region(binary, zoom=1.0):
    if np.prod(binary.shape)==0: return "image dimensions are zero"
    if len(binary.shape)==3: return "image is not monochrome %s"%(binary.shape,)
    if np.amax(binary)==np.amin(binary): return "image is blank"
    if np.mean(binary)<np.median(binary): return "image may be inverted"
    h,w = binary.shape
    if h<45/zoom: return "image not tall enough for a region image %s"%(binary.shape,)
    if h>5000/zoom: return "image too tall for a region image %s"%(binary.shape,)
    if w<100/zoom: return "image too narrow for a region image %s"%(binary.shape,)
    if w>5000/zoom: return "image too wide for a region image %s"%(binary.shape,)
    slots = int(w*h*1.0/(30*30)*zoom*zoom) * 4
    _,ncomps = measurements.label(binary)
    if ncomps<5: return "too few connected components for a region image (got %d)"%(ncomps,)
    if ncomps>slots and ncomps>10: return "too many connected components for a region image (%d > %d)"%(ncomps,slots)
    return None
def check_region ( binary , zoom = 1.0 ) : if np . prod ( binary . shape ) == 0 : return "image dimensions are zero" if len ( binary . shape ) == 3 : return "image is not monochrome %s" % ( binary . shape , ) if np . amax ( binary ) == np . amin ( binary ) : return "image is blank" if np . mean ( binary ) < np . median ( binary ) : return "image may be inverted" h , w = binary . shape if h < 45 / zoom : return "image not tall enough for a region image %s" % ( binary . shape , ) if h > 5000 / zoom : return "image too tall for a region image %s" % ( binary . shape , ) if w < 100 / zoom : return "image too narrow for a region image %s" % ( binary . shape , ) if w > 5000 / zoom : return "image too wide for a region image %s" % ( binary . shape , ) slots = int ( w * h * 1.0 / ( 30 * 30 ) * zoom * zoom ) * 4 _ , ncomps = measurements . label ( binary ) if ncomps < 5 : return "too few connected components for a region image (got %d)" % ( ncomps , ) if ncomps > slots and ncomps > 10 : return "too many connected components for a region image (%d > %d)" % ( ncomps , slots ) return None
Validate binary as a plausible text region image.
Validate binary as a plausible text region image .
"""Validate binary as a plausible text region image.

    Given a binarized, inverted image as Numpy array `binary`
    (with 0 for white and 1 for black),
    check the array has the right dimensions, is in fact inverted,
    and does not have too few or too many connected black components.

    Returns an error report, or None if valid.
    """ # zoom factor (DPI relative) and 4 (against fragmentation from binarization)
binary
zoom