def compute_gradmaps(binary, scale): # use gradient filtering to find baselines boxmap = psegutils.compute_boxmap(binary, scale, (0.4, 5)) cleaned = boxmap * binary ####imsave('/home/gupta/Documents/cleaned.png', cleaned) ####imsave('/home/gupta/Documents/boxmap.png', boxmap) # DSAVE("cleaned",cleaned) if args.usegauss: # this uses Gaussians grad = gaussian_filter( 1.0 * cleaned, (args.vscale * 0.3 * scale, args.hscale * 6 * scale), order=(1, 0)) else: # this uses non-Gaussian oriented filters grad = gaussian_filter( 1.0 * cleaned, (max(4, args.vscale * 0.3 * scale), args.hscale * scale), order=(1, 0)) grad = uniform_filter(grad, (args.vscale, args.hscale * 6 * scale)) bottom = ocrolib.norm_max((grad < 0) * (-grad)) top = ocrolib.norm_max((grad > 0) * grad) testseeds = zeros(binary.shape, 'i') ####imsave('/home/gupta/Documents/grad.png', grad) ####imsave('/home/gupta/Documents/top.png', [testseeds,1.0*top,binary]) ####imsave('/home/gupta/Documents/bottom.png', [testseeds,1.0*bottom,binary]) return bottom, top, boxmap
def compute_gradmaps(binary,scale,vscale=1.0,hscale=1.0,usegauss=False): # use gradient filtering to find baselines boxmap = psegutils.compute_boxmap(binary,scale) cleaned = boxmap*binary #DSAVE("cleaned",cleaned) if usegauss: # this uses Gaussians grad = gaussian_filter(1.0*cleaned,(vscale*0.3*scale, hscale*6*scale),order=(1,0)) else: # this uses non-Gaussian oriented filters grad = gaussian_filter(1.0*cleaned,(max(4,vscale*0.3*scale), hscale*scale),order=(1,0)) grad = uniform_filter(grad,(vscale,hscale*6*scale)) bottom = ocrolib.norm_max((grad<0)*(-grad)) top = ocrolib.norm_max((grad>0)*grad) return bottom,top,boxmap
def compute_gradmaps(binary, scale, usegauss, vscale, hscale): # use gradient filtering to find baselines boxmap = psegutils.compute_boxmap(binary, scale) cleaned = boxmap * binary DSAVE("cleaned", cleaned) if usegauss: # this uses Gaussians grad = gaussian_filter(1.0 * cleaned, (vscale * 0.3 * scale, hscale * 6 * scale), order=(1, 0)) else: # this uses non-Gaussian oriented filters grad = gaussian_filter(1.0 * cleaned, (max(4, vscale * 0.3 * scale), hscale * scale), order=(1, 0)) grad = uniform_filter(grad, (vscale, hscale * 6 * scale)) bottom = ocrolib.norm_max((grad < 0) * (-grad)) top = ocrolib.norm_max((grad > 0) * grad) return bottom, top, boxmap
def getSegmentizedImage(binary, scale): """ Function that give every pixel of every text character on a same line the same value so it can be easily separated latter This function find the upper and lower boundaries of text lines with a filter using the first derivative of gaussian kernel to get horizontal change of intensity and stretching it ---------------------------- @args: binary: 2D array Represent a clean, straight binarized image from witch we will find the seeds scale: double Represent the size of the mean object @return: segmentation: 2D array Represent a picture with different pixel value for each line of text. ---------------------------- The segmentization give us independant text lines. Rotating the picture to make lines more straight could improve result. Some args could be added to play on the stretching of the blur or the threeshold for finding the mask boundaries """ boxmap = ocrolib.compute_boxmap(binary, scale, threshold=(.25, 4)) cleaned_image = boxmap * binary # Finding horizontals gradient gradient = ndi.gaussian_filter(1.0 * cleaned_image, (0.5 * scale, scale * 6), order=(1, 0)) gradient = ndi.uniform_filter(gradient, (1, 20)) # Find the bottom (whiter) and top (darker) zones from grad bottom = ocrolib.norm_max((gradient < 0) * (-gradient)) top = ocrolib.norm_max((gradient > 0) * gradient) seeds = getLineSeed(cleaned_image, scale, bottom, top) # Label the seeds then group them by line inside boxmaps seeds, _ = ocrolib.morph.label(seeds) labels = ocrolib.morph.propagate_labels(boxmap, seeds, conflict=1) spread = ocrolib.morph.spread_labels(seeds, maxdist=(scale / 4)) labels = np.where(labels > 0, labels, spread * binary) return labels * binary
def compute_gradmaps(self, binary, scale): # use gradient filtering to find baselines boxmap = psegutils.compute_boxmap(binary, scale) cleaned = boxmap * binary if self.parameter['usegauss']: # this uses Gaussians grad = gaussian_filter(1.0 * cleaned, (self.parameter['vscale'] * 0.3 * scale, self.parameter['hscale'] * 6 * scale), order=(1, 0)) else: # this uses non-Gaussian oriented filters grad = gaussian_filter( 1.0 * cleaned, (max(4, self.parameter['vscale'] * 0.3 * scale), self.parameter['hscale'] * scale), order=(1, 0)) grad = uniform_filter(grad, (self.parameter['vscale'], self.parameter['hscale'] * 6 * scale)) bottom = ocrolib.norm_max((grad < 0) * (-1 * grad)) top = ocrolib.norm_max((grad > 0) * grad) return bottom, top, boxmap
def compute_gradmaps(binary, scale): # use gradient filtering to find baselines binaryary = morph.r_opening(binary.astype(bool), (1, 1)) # CMND boxmap = compute_boxmap(binaryary, scale, binary) cleaned = boxmap * binaryary if args.usegauss: # this uses Gaussians grad = gaussian_filter( 1.0 * cleaned, (args.vscale * 0.3 * scale, args.hscale * 6 * scale), order=(1, 0)) else: # this uses non-Gaussian oriented filters grad = gaussian_filter( 1.0 * cleaned, (max(4, args.vscale * 0.3 * scale), args.hscale * 1.0 * scale), order=(1, 0)) grad = uniform_filter(grad, (args.vscale, args.hscale * 1 * scale)) # CMND bottom = ocrolib.norm_max((grad < 0) * (-grad)) # bottom = minimum_filter(bottom,(2,6*scale)) top = ocrolib.norm_max((grad > 0) * grad) # top = minimum_filter(top,(2,6*scale)) return bottom, top, boxmap