def savegif(self, path, gifpath, size): image = MyImage(path) image.convert2grayscale() image.squareit() # calculate ft for resizing imft = ImgFFT(image.data) imft.ft() im = imft.resize_image(size[0], size[1]) imrgb = MyRGBImg(np.zeros((size[0], size[1], 3))) for c in range(3): imrgb.set_channel(im, c) # save resized image imsave(gifpath, imrgb.data, format="gif")
def run_average_gray(folder): # options debug_mode = True # chose path to image sequence folder datasetpath = folder memsave = True # True | False preprocop = [("convert to grayscale", ), ("square it", ), ("binning", 0), ("transpose", ), ("normalize", )] custom_template = False # True | False template_image_path = folder + "template_folder/template.png" # path to image auto_template_type = "UseFirstImage" # "Use First Image" | "Average" save_template = True # True | False align_images = True # True | False align_mode = "fixed" # "fixed | tree" // fixed still fastest option align_space = (-1, 1, 0.1) # (min angle, max angle, precision) # logger mylog = Logger("Averaging Gray", datasetpath + "main_logfile.txt", debug_mode=debug_mode) mylog.log("Debug mode: " + "ON" if debug_mode == True else "OFF") mylog.log("For the folder:") mylog.log(datasetpath) mylog.log("Averaging type: grey") mylog.log("Memory saving mode: " + str(memsave)) mylog.log( "------------------------------\nLoading dataset\n------------------------------" ) if memsave: avg = AvgFolderMem(datasetpath) else: avg = AvgFolder(datasetpath) avg.gather_pictures() # build the informatiosn mylog.log("number of pictures:" + str(avg.init_imgs.n)) image = avg.init_imgs.get_image(0) mylog.log("Size of images: " + str(image.data.shape[0]) + "x" + str(image.data.shape[1])) mylog.log("--- Start preporcessing ---", True) nametofunc = {} nametofunc[preprocop[0]] = lambda: avg.c2gscale() nametofunc[preprocop[1]] = lambda: avg.squareit() nametofunc[preprocop[2]] = lambda n: avg.binning(n) nametofunc[preprocop[3]] = lambda: avg.transpose() nametofunc[preprocop[4]] = lambda: avg.normalize() for name in preprocop: if len(name) == 1: nametofunc[name]() mylog.log("Process: " + name[0]) if len(name) == 2: nametofunc[name](name[1]) mylog.log("Process: " + name[0] + "Arguments: " + str(name[1])) mylog.log("Processing took: ", True) mylog.log( "------------------------------\nGenerating template\n------------------------------" ) if custom_template: custom_t = MyImage(template_image_path) custom_t.convert2grayscale() mylog.log("Template loaded from: " + template_image_path) mylog.log("Template image is: {0}x{1}".format(custom_t.get_sizex(), custom_t.get_sizey())) avg.generate_template(custom_t) else: avg.generate_template(auto_template_type) mylog.log("Template generated: " + auto_template_type) mylog.log("Template generated", True) if save_template: avg.save_template() if debug_mode: avg.template.show_image() plt.show() avg.template.inspect() if align_images: mylog.log( "------------------------------\nAlignment\n------------------------------" ) mylog.log("Choosen Aligment: " + align_mode) alignnames = ["min angle: ", " |max angle: ", " |precision: "] mylog.log("".join(a + str(d) for a, d in zip(alignnames, align_space))) avg.align_images(align_mode, align_space, debug_mode) avg.save_shifts() mylog.log("Alignment done", True) if avg.anglestree != None: mylog.log("Numnber of template generated: " + str(len(avg.anglestree.angles_nodes))) mylog.log("Normally would be: " + str( len(np.arange(align_space[0], align_space[1], align_space[2]))) ) else: mylog.log("Numnber of template generated: " + str(avg.templaterotsft.n)) mylog.log("Shifts saved") mylog.log( "------------------------------\nAverage\n------------------------------" ) avg.average(debug=debug_mode) avg.save_avg() mylog.log("Average Complete", True) if debug_mode: avg.avg.show_image() plt.show() avg.avg.inspect() mylog.log("End procedure", True)
def run_create_test_dataset(folder): debug_mode = True # set parameters testdatasetpath = folder mypicname = "./data/Lenna.png" n_pictures = 25 min_a = -10 max_a = 10 min_xs = -25 max_xs = 25 min_ys = -25 max_ys = 25 flip_angle = True mylog = Logger("Create gray dataset", testdatasetpath + "main_logfile.txt", debug_mode=debug_mode) mylog.log("Creating dataset in:\n" + testdatasetpath) mylog.log("Using the picture: " + mypicname) mylog.log("Creating dataset with {0} pictures".format(n_pictures)) mylog.log("With rotations from {0} to {1} degree".format(min_a, max_a)) mylog.log("With shift in x: from {0} to {1} and y: from {2} to {3}".format( min_xs, max_xs, min_ys, max_ys)) mylog.log( "The dataset will be generated by {0} randomly flipping rotations and translations" .format("" if flip_angle == True else "not")) if not isdir(testdatasetpath): mkdir(testdatasetpath) mylog.log("Created test dataset path") # create a test dataset: mypic = MyImage(mypicname) mypic.squareit() mypic.convert2grayscale() mypic.binning(2) mypic.normalize() mylog.log("Processing done") template_folder = join(testdatasetpath, "template_folder") if not isdir(template_folder): mkdir(template_folder) mypic.save(join(template_folder, "template.png")) mylog.log("Saved the original image in the template folder") if debug_mode: mypic.show_image() plt.show() mylog.log( "------------------------------\nCreating dataset\n------------------------------" ) np.random.seed(10) logpathdir = join(testdatasetpath, "tlog") if not isdir(logpathdir): mkdir(logpathdir) with open(join(logpathdir, "mytransformations.log"), 'w') as f: angles = np.random.uniform(min_a, max_a, n_pictures) for i in range(n_pictures): image = deepcopy(mypic) if flip_angle: anglefirst = False if np.random.randint(0, 2) == 0 else True else: anglefirst = True angle = angles[i] dx = np.random.randint(min_xs, max_xs) dy = np.random.randint(min_ys, max_ys) f.write("{0} {1} {2} {3}\n".format(anglefirst, dx, dy, angle)) mylog.log( "Pictrue with: rot first {0}, angle: {1}, shift x: {2}, y: {3} created" .format(anglefirst, angle, dx, dy)) if anglefirst: image.rotate(angle) image.move(dx, dy) else: image.move(dx, dy) image.rotate(angle) if debug_mode: image.show_image() plt.show() image.save(join(testdatasetpath, "pic_" + str(i) + ".png"))
# load sample image imagepath = "C:/Users/Mauro/Desktop/Vita Online/Programming/Picture cross corr/Lenna.png" imagepath = "C:/Users/Mauro/Desktop/Vita Online/Programming/Picture cross corr/silentcam/dataset24/avg/correlation_images/corr_1497777846958.png" imagepath = "../../../Lenna.png" # # convert range test # x = np.arange(0, 3, 0.1) # print(x) # # for i in x: # print(i, map_range(i, 0, 1, 0, 2*np.pi)) im = MyImage() im.read_from_file(imagepath) im.convert2grayscale() print("Original image") im.show_image() plt.show() ft = ImgFFT(im) ft.ft() imres = ft.resize_image(256, 256) imres.show_image() plt.show() # print("Power Spectrum") # ps = ft.power_spectrum() # ps.show_image()