def test_threshold_sauvola_iterable_window_size(self): ref = np.array([[False, False, False, True, True], [False, False, True, True, True], [False, False, True, True, False], [False, True, True, True, False], [True, True, False, False, False]]) thres = threshold_sauvola(self.image, window_size=(3, 5), k=0.2, r=128) out = self.image > thres assert_array_equal(ref, out)
def test_threshold_sauvola(self): ref = np.array( [[False, False, False, True, True], [False, False, True, True, True], [False, False, True, True, False], [False, True, True, True, False], [True, True, False, False, False]] ) thres = threshold_sauvola(self.image, window_size=3, k=0.2, r=128) out = self.image > thres assert_equal(ref, out)
def deskew(args,image, image_param): # Deskew # Calculate the angle of the points between 20% and 80% of the line uintimage = get_uintimg(image) thresh = th.threshold_sauvola(uintimage, args.threshwindow, args.threshweight) binary = uintimage > thresh binary = 1-binary #inverse binary binary = np.rot90(binary,args.horlinepos) labels, numl = measurements.label(binary) objects = measurements.find_objects(labels) deskew_path = None for i, b in enumerate(objects): linecoords = Linecoords(image, i, b) # The line has to be bigger than minwidth, smaller than maxwidth, stay in the top (30%) of the img, # only one obj allowed and the line isn't allowed to start contact the topborder of the image if int(args.minwidthhor * image_param.width) < get_width(b) < int(args.maxwidthhor * image_param.width) \ and int(image_param.height * args.minheighthor) < get_height(b) < int(image_param.height * args.maxheighthor) \ and int(image_param.height * args.minheighthormask) < (linecoords.height_start+linecoords.height_stop)/2 < int(image_param.height * args.maxheighthormask) \ and linecoords.height_start != 0: pixelwidth = set_pixelground(binary[b].shape[1]) arr = np.arange(1, pixelwidth(args.deskewlinesize) + 1) mean_y = [] #Calculate the mean value for every y-array for idx in range(pixelwidth(args.deskewlinesize)): value_y = measurements.find_objects(labels[b][:, idx + pixelwidth((1.0-args.deskewlinesize)/2)] == i + 1)[0] mean_y.append((value_y[0].stop + value_y[0].start) / 2) polyfit_value = np.polyfit(arr, mean_y, 1) deskewangle = np.arctan(polyfit_value[0]) * (360 / (2 * np.pi)) args.ramp = True deskew_image = transform.rotate(image, deskewangle) deskew_path = "%s_deskew.%s" % (image_param.pathout+image_param.name, args.extension) image_param.deskewpath = deskew_path with warnings.catch_warnings(): #Transform rotate convert the img to float and save convert it back warnings.simplefilter("ignore") misc.imsave(deskew_path, deskew_image) break return deskew_path
def linecoords_analyse(args,origimg, image_param, clippingmask): image = get_uintimg(origimg) origimg = np.rot90(origimg, args.horlinepos) thresh = th.threshold_sauvola(image, args.threshwindow, args.threshweight) binary = image > thresh binary = 1-binary #inverse binary binary = np.rot90(binary, args.horlinepos) labels, numl = measurements.label(binary) objects = measurements.find_objects(labels) count_height = 0 count_width = 0 pixelheight = set_pixelground(image_param.height) pixelwidth = set_pixelground(image_param.width) list_linecoords = [] # Init list of linecoordinates the format is: [0]: width.start, width.stopt, # [1]:height.start, height.stop, [2]: Type of line [B = blank, L = vertical line] for i, b in enumerate(objects): # The line has to be bigger than minwidth, smaller than maxwidth, stay in the top (30%) of the img, # only one obj allowed and the line isn't allowed to start contact the topborder of the image linecoords = Linecoords(labels, i, b) if pixelwidth(args.minwidthhor) < get_width(b) < pixelwidth(args.maxwidthhor) \ and pixelheight(args.minheighthor) < get_height(b) < pixelheight(args.maxheighthor) \ and pixelheight(args.minheighthormask) < linecoords.height_stop < pixelheight(args.maxheighthormask) \ and count_width == 0 \ and linecoords.height_start != 0: # Distance Calculation - defining the clippingmask border = get_mindist(b, image_param.width) topline_width_stop = b[0].stop + 2 # Lowest Point of object + 2 Pixel if clippingmask.user == None: clippingmask.width_start = border clippingmask.width_stop = image_param.width - border #if clippingmask.width_start > pixelwidth(args.minwidthmask): # clippingmask.width_start = pixelwidth(args.minwidthmask) #if clippingmask.width_stop < pixelwidth(1.0-args.minwidthmask): # clippingmask.width_stop = pixelwidth(1.0-args.minwidthmask) clippingmask.height_start = copy.deepcopy(topline_width_stop) clippingmask.height_stop = 0 # Test for cropping the area under the first vertical line #roi = image[hobj_bottom:image_param.height, border:image_param.width - border] # region of interest #imsave("%s_crop.%s" % (image_param.pathout+image_param.name, args.extension), roi) # Get coordinats of the line labels[b][labels[b] == i + 1] = 0 count_width += 1 if pixelheight(args.minheightver) < get_height(b) < pixelheight(args.maxheightver) \ and pixelwidth(args.minwidthver) < get_width(b) < pixelwidth(args.maxwidthver) \ and pixelwidth(args.minwidthvermask) < (linecoords.width_start+linecoords.width_stop)/2 < pixelwidth(args.maxwidthvermask) \ and float(get_width(b))/float(get_height(b)) < args.maxgradientver: linecoords.segmenttype = 'L' # Defaultvalue for segmenttype 'P' for horizontal lines if count_height == 0: if b[0].start - topline_width_stop > pixelheight(args.minsizeblank+args.minsizeblankobolustop): blankline = Linecoords(labels,i,b) blankline.segmenttype = 'B' blankline.height_start = topline_width_stop blankline.height_stop = linecoords.height_start blankline.width_start = border blankline.width_stop = image_param.width - border blankline.middle = int(((linecoords.width_start+linecoords.width_stop)-1)/2) list_linecoords.append(copy.deepcopy(blankline)) count_height += 1 if args.ramp != None: whiteout_ramp(origimg, linecoords) list_linecoords.append(copy.deepcopy(linecoords)) count_height += 1 else: # Should fix to short vertical lines, in the height to top if they appear before any B Part in the image if topline_width_stop > 0: linecoords.height_start = topline_width_stop + pixelheight(args.addstartheightab) list_linecoords.append(copy.deepcopy(linecoords)) count_height += 1 if args.ramp != None: whiteout_ramp(origimg, linecoords) elif list_linecoords[count_height - 1].height_stop < b[0].stop: #Test argument to filter braces if b[0].start - list_linecoords[count_height - 1].height_stop > pixelheight(args.minsizeblank): blankline = Linecoords(labels,i,b) blankline.segmenttype = 'B' blankline.height_start = list_linecoords[count_height - 1].height_stop blankline.height_stop = linecoords.height_start blankline.width_start = border blankline.width_stop = image_param.width - border blankline.middle = int(((linecoords.width_start+linecoords.width_stop)-1)/2) list_linecoords.append(copy.deepcopy(blankline)) count_height += 1 list_linecoords.append(copy.deepcopy(linecoords)) if args.ramp != None: whiteout_ramp(origimg, linecoords) count_height += 1 labels[b][labels[b] == i + 1] = 0 else: if args.ramp != None: whiteout_ramp(origimg, linecoords) print b[0].stop list_linecoords[count_height - 1].height_stop = b[0].stop labels[b][labels[b] == i + 1] = 0 #imsave("%s_EDIT%d.%s" % (image_param.pathout, linecoords.object_value, args.extension), image) return list_linecoords, border, topline_width_stop
def get_binary(args, image): thresh = th.threshold_sauvola(image, args.threshwindow, args.threshweight) binary = image > thresh binary = 1 - binary # inverse binary binary = np.rot90(binary, args.horlinepos) return binary