Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_deskew(image, expected_angle):
root_folder = 'results/{}'.format(image)
if not os.path.exists(root_folder):
os.makedirs(root_folder)
image = io.imread(os.path.join(os.path.dirname(__file__), 'deskew-{}.png'.format(image)))
grayscale = rgb2gray(image)
angle = determine_skew(grayscale)
print(angle - expected_angle.expected)
assert angle == expected_angle
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output", default=None, help="Output file name")
parser.add_argument("--sigma", default=3.0, help="The use sigma")
parser.add_argument("--num-peaks", default=20, help="The used num peaks")
parser.add_argument("--background", help="The used background color")
parser.add_argument(default=None, dest="input", help="Input file name")
options = parser.parse_args()
image = io.imread(options.input)
grayscale = rgb2gray(image)
angle = determine_skew(grayscale, sigma=options.sigma, num_peaks=options.num_peaks)
if options.output is None:
print("Estimated angle: {}".format(angle))
else:
if options.background:
try:
background = [int(c) for c in options.background.split(",")]
except: # pylint: disable=bare-except
print("Wrong background color, should be r,g,b")
sys.exit(1)
rotated = rotate(image, angle, resize=True, cval=-1) * 255
pos = np.where(rotated == -255)
rotated[pos[0], pos[1], :] = background
else:
rotated = rotate(image, angle, resize=True) * 255
io.imsave(options.output, rotated.astype(np.uint8))
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),2)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-4)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
image,M= deskew.fastDeskew(image)
return image
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),3)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-3)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
image,M = deskew.fastDeskew(image)
return image
def run(self):
if self.input_file:
self.deskew()
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-i', '--input', default = None, dest = 'input_file', help = 'Input file name')
parser.add_option('-d', '--display', default = None, dest = 'display_image', help = "display the rotated image")
parser.add_option('-o', '--output', default = None, dest = 'output_file', help = 'Output file name')
parser.add_option('-r', '--rotate', default = 0, dest = 'r_angle', help = 'Rotate the image to desired axis', type = int)
options, args = parser.parse_args()
deskew_obj = Deskew(options.input_file, options.display_image, options.output_file, options.r_angle)
deskew_obj.run()
default=None,
dest='display_image',
help="display the rotated image")
parser.add_option(
'-o', '--output',
default=None,
dest='output_file_path',
help='Output file name')
parser.add_option(
'-r', '--rotate',
default=0,
dest='rot_angle',
help='Rotate the image to desired axis',
type=int)
options, args = parser.parse_args()
deskew_obj = Deskew(
options.input_file_path,
options.display_image,
options.output_file_path,
options.rot_angle)
deskew_obj.run()
def deskew_image(im, pim):
cornerdetector = CornerDetector(pim)
corners = cornerdetector.detect_corners()
for i in range(0, len(corners)):
corner = corners[i]
nextcorner = corners[(i + 1) % len(corners)]
cv2.circle(im, (corner[0], corner[1]), 25, (210, 210, 210), -1)
cv2.circle(im, (corner[0], corner[1]), 27, (130, 210, 210), 4)
cv2.line(im, tuple(corner), tuple(nextcorner), (130, 210, 210), 5)
plt.imshow(im, cmap='gray')
plt.show()
deskewer = deskew.Deskewer(im, corners, 1.414)
transformedimage = deskewer.deskew()
return transformedimage