def main(): if len(sys.argv) != 4: print("Wrong number of parameters. The correct usage is:\ ex6.py <image_source> <output > <max_diagonal>") else: _, image_source, output_name, max_diagonal = sys.argv if not os.path.isfile(image_source): print("no image file") else: max_diagonal = float(max_diagonal) image = HELPER.load_image(image_source) fixed_image = make_correction(image, max_diagonal) HELPER.save(fixed_image, output_name)
def otsu_loaded(image_filename): image = ex6_helper.load_image(image_filename) return otsu(image)
def downsample(image, max_diagonal_size): slant = calculate_diagonal(image) while slant > max_diagonal_size: image = downsample_by_3(image) slant = calculate_diagonal(image) if slant > max_diagonal_size: continue else: return image new_image = [[1 for i in range(9)] for j in range(9)] #print(downsample(new_image, 2**0.5)) img = ex6_helper.load_image("test20.jpg") # img2 = detect_edges(img) # result_checker = [] # for i in range(len(img)): # result_checker.append(img[i]+img2[i]) # ex6_helper.show(result_checker) dots = ex6_helper.pixels_on_line(img, 1, 17, False) new_img = [] for _ in range(len(img)): new_img.append([0] * len(img[0])) for dot in dots: new_img[dot[0]][dot[1]] = 255 ex6_helper.show(new_img) print(dots)
return new_image def make_correction(image, max_diagonal): """ Detects the dominant angle of an image and rotate it in the opposite angle :param image: image to correct :param max_diagonal: diagonal used to work on the image :return: corrected image """ new_image = threshold_filter(detect_edges( threshold_filter(downsample(image, float(max_diagonal))))) dominant_angle = get_angle(new_image) return rotate(image, -dominant_angle) image3=ex6_helper.load_image('lines001_thresh_downsampled.jpg') print(get_angle(image3)) # Getting the parameters from the console if __name__ == '__main__': if len(sys.argv) != NUMBER_OF_ARGUMENTS: print( "Wrong number of parameters. The correct usage is: " "ex6.py <image_source> <output> <max_diagonal>") else: # Saving corrected image ex6_helper.save( make_correction(ex6_helper.load_image(sys.argv[IMAGE_ARGUMENT]), sys.argv[DIAGONAL_ARGUMENT]), sys.argv[OUTPUT_ARGUMENT])
newimage = image while (diagonal(newimage) > max_diagonal_size): newimage = downsample_by_3(newimage) return newimage from math import pi ''' def get_angle(image): for angle in range( 0 , pi / 2 , 1 / (2*pi) ) : for distance in range(0 , len(image) / (2 ** 0.5) , 0.1 ): lines , newline = [] , True for pixel in pixels_on_line(image, angle, distance, top=True): if pixel == 0 : if newline : lines.append([]) newline = False lines[-1].append( pixel ) else: newline = True weight = 0 for line in lines: weight += len(line) ** 2 ''' import os if __name__ == "__main__": args = os.sys.argv show(downsample_by_3(load_image(args[1])))
old_row = new_pixel * sin_angle + new_row * cos_angle old_pixel = new_pixel * cos_angle - new_row * sin_angle old_row_centered = int(old_row + old_y) old_pixel_centered = int(old_pixel + old_x) if (-1 < old_row_centered < old_y * 2) and (-1 < old_pixel_centered < old_x * 2): rotated_image[row][pixel] = image[old_row_centered][ old_pixel_centered] return rotated_image def make_correction(image, max_diagonal): """Perform a complete correction to an image""" angle = get_angle( threshold_filter( detect_edges(threshold_filter(downsample(image, max_diagonal))))) return rotate(image, angle) if __name__ == "__main__": if len(sys.argv) == NUMBER_OF_ARGUMENTS + 1: # the if check for wrong number of arguments image_source = sys.argv[1] output = sys.argv[2] max_diagonal = int(sys.argv[3]) image = ph.load_image(image_source) new_image = make_correction(image, max_diagonal) ph.save(new_image, output) else: print(ERROR_MESSAGE)
using a smaller or equal sized image the function identifies the angle the image is rotated by, and rotates the image into a horizontal state """ new_image = downsample(image, max_diagonal) # make image smaller new_image = threshold_filter(new_image) # make image black and white new_image = detect_edges(image) # edge detection new_image = threshold_filter(new_image) # make image black and white dominant_angle = get_angle(new_image) # get dominant angle in image dominant_angle = math.radians(dominant_angle) new_image = rotate(image, -dominant_angle) # rotate image return (new_image) if __name__ == "__main__": if len(sys.argv) == NUMBER_OF_ARGUMENTS + 1: file_name = sys.argv[1] image = ex6_helper.load_image(file_name) output_name = sys.argv[2] max_diagonal = float(sys.argv[3]) image = make_correction(image, max_diagonal) ex6_helper.save(image, output_name) else: sys.exit(MSG_INVALID_NUMBER_OF_PARAMETERS)