def processOneLineImage(gray_img, iTag): (_, img) = cv2.threshold(gray_img, 110, 255, cv2.THRESH_BINARY_INV) img = img[:, 2 : img.shape[1] - 2] scale = psegutils.estimate_scale(img) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 1)) closed = cv2.dilate(img, kernel, iterations=1) edges = cv2.Canny(closed, 60, 300) contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(edges, contours, -1, (255, 255, 255), 1) # cv2.imwrite('edges%s.png' % iTag,edges) boxmap = psegutils.compute_boxmap(img, scale, threshold=(0.4, 10), dtype="B") # combineBoxmap(boxmap) cv2.imwrite("box%s.png" % iTag, boxmap * 255) h_projection = hprojection(boxmap * 255) top, bottom = cropProjection(h_projection) regions = splitProjection(h_projection, top, bottom, 30, 2) # print iTag, top,bottom # print regions # print v_projection[1270:1450] if len(iTag) == 0: return regions, top, bottom for region in regions: topStart, TopEnd = region cr_img = cv2.getRectSubPix( gray_img, (gray_img.shape[1] - 4, TopEnd - topStart + 8), (gray_img.shape[1] / 2, (TopEnd + topStart) / 2) ) cv2.imwrite("%sx%d.png" % (iTag, topStart), cr_img) return regions, top, bottom
def processOneLineImage(gray_img, iTag): (_, img) = cv2.threshold(gray_img, 110, 255, cv2.THRESH_BINARY_INV) img = img[:, 2:img.shape[1]-2] scale = psegutils.estimate_scale(img) kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 1)) closed = cv2.dilate(img, kernel, iterations = 1) edges = cv2.Canny(closed,60,300) contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cv2.drawContours(edges,contours,-1,(255,255,255),1) #cv2.imwrite('edges%s.png' % iTag,edges) boxmap = psegutils.compute_boxmap(img,scale,threshold=(.4,10),dtype='B') #combineBoxmap(boxmap) cv2.imwrite('box%s.png' % iTag, boxmap*255) h_projection = hprojection(boxmap*255) top, bottom = cropProjection(h_projection) regions = splitProjection(h_projection, top, bottom,30,2) #print iTag, top,bottom #print regions #print v_projection[1270:1450] if len(iTag) == 0: return regions,top,bottom for region in regions: topStart, TopEnd = region cr_img =cv2.getRectSubPix(gray_img, (gray_img.shape[1]-4, TopEnd-topStart+8), (gray_img.shape[1]/2, (TopEnd+topStart)/2)) cv2.imwrite('%sx%d.png' % (iTag, topStart), cr_img) return regions,top,bottom
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): # use gradient filtering to find baselines boxmap = psegutils.compute_boxmap(binary, scale) cleaned = boxmap * binary grad = gaussian_filter(1.0 * cleaned, (max(4, 0.3 * scale), scale), order=(1, 0)) grad = uniform_filter(grad, (1.0, 1.0 * 6 * scale)) bottom = norm_max((grad < 0) * (-grad)) top = norm_max((grad > 0) * grad) return bottom, top, boxmap
def compute_colseps_morph(binary, scale, maxseps=3, minheight=20, maxwidth=5): """Finds extended vertical whitespace corresponding to column separators using morphological operations.""" boxmap = psegutils.compute_boxmap(binary, scale, (0.4, 5), dtype='B') bounds = morph.rb_closing(B(boxmap), (int(5 * scale), int(5 * scale))) bounds = maximum(B(1 - bounds), B(boxmap)) cols = 1 - morph.rb_closing(boxmap, (int(20 * scale), int(scale))) cols = morph.select_regions(cols, sl.aspect, min=args.csminaspect) cols = morph.select_regions(cols, sl.dim0, min=args.csminheight * scale, nbest=args.maxcolseps) cols = morph.r_erosion(cols, (int(0.5 + scale), 0)) cols = morph.r_dilation(cols, (int(0.5 + scale), 0), origin=(int(scale / 2) - 1, 0)) return cols
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_colseps_morph(self, binary, scale): """Finds extended vertical whitespace corresponding to column separators using morphological operations.""" boxmap = psegutils.compute_boxmap(binary, scale, dtype='B') bounds = morph.rb_closing(B(boxmap), (int(5 * scale), int(5 * scale))) bounds = np.maximum(B(1 - bounds), B(boxmap)) cols = 1 - morph.rb_closing(boxmap, (int(20 * scale), int(scale))) cols = morph.select_regions(cols, sl.aspect, min=self.parameter['csminaspect']) cols = morph.select_regions(cols, sl.dim0, min=self.parameter['csminheight'] * scale, nbest=self.parameter['maxcolseps']) cols = morph.r_erosion(cols, (int(0.5 + scale), 0)) cols = morph.r_dilation(cols, (int(0.5 + scale), 0), origin=(int(scale / 2) - 1, 0)) return cols
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 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