def deskew(args,image, image_param):
# Deskew the given image based on the horizontal line
# Calculate the angle of the points between 20% and 80% of the line
uintimage = get_uintimg(image)
binary = get_binary(args, uintimage)
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)
create_dir(image_param.pathout+os.path.normcase("/deskew/"))
deskew_path = "%s_deskew.%s" % (image_param.pathout+os.path.normcase("/deskew/")+image_param.name, args.extension)
deskewinfo = open(image_param.pathout+os.path.normcase("/deskew/")+image_param.name + "_deskewangle.txt", "w")
deskewinfo.write("Deskewangle:\t%d" % deskewangle)
deskewinfo.close()
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
评论列表
文章目录