def run_create_rgb_dataset(folder): debug_mode = True # set parameters testdatasetpath = folder mypicname = "./data/volpe-2.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 RGB 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 = MyRGBImg(mypicname) mypic.binning(2) 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")) 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:.2f}, 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"))
def get_alg_image(self, index): filename, ext = splitext(self.imgs_names[index]) pathtopic = join(self.subfolders["aligned_rgb_images"], ("alg_" + filename + ".png" )) myimg = MyRGBImg() myimg.read_from_file(pathtopic) return myimg
class AvgRGB(object): # initialize method def __init__(self, path): # define main path self.path = path # folder environment self.avgpath = "" self.subfolders = {} try: self.makeavgdir() except FileNotFoundError as e: print("Folder not found") except: print("WTF") # create log file in the avg folder lg.basicConfig(filename=join(self.avgpath,'rgb_average.log'),level=lg.INFO) self.imgs = [] self.avg = MyRGBImg() self.algs = [] self.algimgs = [] def gather_pictures(self): # for now gathe all the files, next check for picture extensions p = self.path self.names = [f for f in listdir(p) if isfile(join(p, f))] for imgname in self.names: path, name, ext = get_pathname(imgname) if ext in ['.png', '.jpg']: imagepath = join(self.path, imgname) img = MyRGBImg() img.read_from_file(imagepath) self.imgs.append(img) lg.info("Image: {0} imported successfully".format(imagepath)) def average(self, aligned = True): if aligned: dataset = self.algimgs else: dataset = self.imgs s = MyRGBImg(np.zeros(dataset[0].data.shape)) s = color.rgb2lab(s.data) for i, picture in enumerate(dataset): print("Averaging image: " , i) # convert both to lab im = color.rgb2lab(picture.data) #perform operations s += im s = s / float(len(dataset)) self.avg = MyRGBImg(color.lab2rgb(s)) def load_algs(self): with open(join(self.subfolders["results"], "shifts_log.txt")) as f: lines = f.readlines() self.algs = [] for line in lines: data = line.split(' | ') data = [int(d.strip()) for d in data] self.algs.append(data) lg.info("Alignments imported successfully") def align_images(self): self.algimgs = [] for i, image in enumerate(self.imgs): algimage = deepcopy(image) print("alg:", self.algs[i][0], self.algs[i][1]) algimage.move(-self.algs[i][1], -self.algs[i][0]) self.algimgs.append(algimage) lg.info("Images aligned successfully") def save_algimgs(self): for i, algimg in enumerate(self.algimgs): filename, ext = splitext(self.names[i]) algimg.save(join(self.subfolders["aligned_rgb_images"], ("alg_" + filename + ".png" ))) lg.info("Saved aligned images successfully") def save_avg(self, name = "avg_rgb.png"): self.avg.save(join(self.subfolders["results"], name)) def makeavgdir(self): # create a folder average into the dataset path # avg # |- processed_images # |- aligned_images # |- correlation_images # |- results # |- aligned_rgb_images self.avgpath = join(self.path, "avg") if not isdir(self.avgpath): mkdir(self.avgpath) subfolders = ["aligned_rgb_images", "results", "avg_transition"] for folder in subfolders: self.subfolders[folder] = join(self.avgpath, folder) if not isdir(self.subfolders[folder]): mkdir(self.subfolders[folder])
def get_image(self, index): # read the image corresponding to the path pathtopic = join(self.path, self.imgs_names[index]) myimg = MyRGBImg() myimg.read_from_file(pathtopic) return myimg
class AvgRGB_savememory(object): # initialize method def __init__(self, path): # define main path self.path = path # folder environment self.avgpath = "" self.subfolders = {} try: self.makeavgdir() except FileNotFoundError as e: print("Folder not found") except: print("WTF") # create log file in the avg folder lg.basicConfig(filename=join(self.avgpath,'rgb_average_savemem.log'),level=lg.INFO) # instead of loading all the pictures in one array # create a path array that reads the pictures at will self.imgs_names = [] self.avg = MyRGBImg() self.algs = [] def get_image(self, index): # read the image corresponding to the path pathtopic = join(self.path, self.imgs_names[index]) myimg = MyRGBImg() myimg.read_from_file(pathtopic) return myimg def get_alg_image(self, index): filename, ext = splitext(self.imgs_names[index]) pathtopic = join(self.subfolders["aligned_rgb_images"], ("alg_" + filename + ".png" )) myimg = MyRGBImg() myimg.read_from_file(pathtopic) return myimg def save_alg_image(self, index, algimg): filename, ext = splitext(self.imgs_names[index]) algimg.limit(1.0) algimg.save(join(self.subfolders["aligned_rgb_images"], ("alg_" + filename + ".png" ))) def gather_pictures_names(self): # for now gathe all the files, next check for picture extensions p = self.path filenames = [f for f in listdir(p) if isfile(join(p, f))] for filename in filenames: path, name, ext = get_pathname(filename) if ext in ['.png', '.jpg']: self.imgs_names.append(filename) def average(self, mode = "Mode", aligned = True, debug = False, transition = True): if mode == "Mean": self.average_mean(aligned, debug, transition) if mode == "Median": self.average_median(aligned, debug, transition) if mode == "Mode": self.average_mode(aligned, debug, transition) def average_mode(self, aligned = True, debug = False, transition = True): # the mode should be the most frequent pixel in an array # let's construct a bin method for float number # if myn > nmin and myn <= nmax: binn +=1 sizedataset = len(self.imgs_names) if aligned: get_img = lambda i: self.get_alg_image(i) #self.get_alg_image(0) else: get_img = lambda i: self.get_image(i) #self.get_image(0) dimsx = get_img(0).data.shape[0] dimsy = get_img(0).data.shape[1] depth = 128 darray = np.arange(depth) # slice the operation into quadrants? dimx2 = int(dimsx / 2) dimy2 = int(dimsy / 2) quadrant = [(0, dimx2, 0, dimy2) , (dimx2 + 1, dimsx, 0, dimy2), (0, dimx2, dimy2 + 1, dimsy), (dimx2 + 1, dimsx, dimy2, dimsy)] for q in quadrant: stack = np.zeros(( int(dimsx / 2), int(dimsy / 2), 3, depth)) for i in range(sizedataset): print("---", i, "----") image = get_img(i) for x in range(q[0], q[1]): for y in range(q[2], q[3]): for c in range(3): for d in range(len(darray) - 1): if image.data[x,y,c] < 0 or image.data[x,y,c] > 1: raise ValueError("image not normalized") pv = image.data[x,y,c] * depth if pv >= darray[d] and pv < darray[d + 1]: stack[x, y, c, d] += 1 # choose the number which has the highest frequency and assign it # to the picture def average_median(self, aligned = True, debug = False, transition = True): # construct the tower of pictures # memory error. sizedataset = len(self.imgs_names) if aligned: get_img = lambda i: self.get_alg_image(i) #self.get_alg_image(0) else: get_img = lambda i: self.get_image(i) #self.get_image(0) dimsx = get_img(0).data.shape[0] dimsy = get_img(0).data.shape[1] stack = np.zeros((dimsx, dimsy, 3, sizedataset)) for i in range(sizedataset): pic = get_img(i) stack[:, :, 3, i] = pic.data print(len(stack)) def average_mean(self, aligned = True, debug = False, transition = True): sizedataset = len(self.imgs_names) if aligned: picture = self.get_alg_image(0) else: picture = self.get_image(0) s = MyRGBImg(np.zeros(picture.data.shape)) s = color.rgb2lab(s.data) for i in range(sizedataset): if debug: print("Averaging image: " , i) #load the picture if aligned: picture = self.get_alg_image(i) else: picture = self.get_image(i) # convert both to lab im = color.rgb2lab(picture.data) #perform operations s += im if transition: tr = s / float(i + 1) avg = MyRGBImg(color.lab2rgb(tr)) avg.save(join(self.subfolders["avg_transition"], "avg_tr_" + str(i) + ".png")) s = s / float(sizedataset) self.avg = MyRGBImg(color.lab2rgb(s)) # self.avg.transpose() # self.avg.rotate(90) def load_algs(self): with open(join(self.subfolders["results"], "shifts_log.txt")) as f: lines = f.readlines() self.algs = [] for line in lines: sdata = line.split(' | ') sdata = [d.strip() for d in sdata] data = [int(sdata[0]), int(sdata[1]), float(sdata[2])] self.algs.append(data) lg.info("Alignments imported successfully") def align_images(self, debug = False): self.algimgs = [] for i in range(len(self.imgs_names)): if debug: print("Aligning image:", i) print("algs:", self.algs[i]) # load picture to align algimage = self.get_image(i) algimage.squareit() algimage.rotate(self.algs[i][2]) algimage.move(-self.algs[i][0], -self.algs[i][1]) algimage.rotate(-90) # save the image self.save_alg_image(i, algimage) lg.info("Images aligned successfully") def save_avg(self, name = "avg_rgb.png"): self.avg.save(join(self.subfolders["results"], name)) def makeavgdir(self): # create a folder average into the dataset path # avg # |- processed_images # |- aligned_images # |- correlation_images # |- results # |- aligned_rgb_images self.avgpath = join(self.path, "avg") if not isdir(self.avgpath): mkdir(self.avgpath) subfolders = ["aligned_rgb_images", "results", "avg_transition"] for folder in subfolders: self.subfolders[folder] = join(self.avgpath, folder) if not isdir(self.subfolders[folder]): mkdir(self.subfolders[folder]) # for i, img in enumerate(self.imgs): # img.squareit() # self.imgs.set_image(i,img) lg.info("dataset squared") def transpose(self): for i, img in enumerate(self.imgs): img.transpose() self.imgs.set_image(i,img) lg.info("dataset transposed") def normalize(self): for i, img in enumerate(self.imgs): img.normalize() self.imgs.set_image(i,img) lg.info("dataset normalized") def binning(self, n = 1): for i, img in enumerate(self.imgs): img.binning(n) self.imgs.set_image(i,img) lg.info("dataset binned {0} times".format(n))
class AvgRGB(object): ''' The only reason this still exists is to compare if it is faster or slower than AvgRGBMem''' # ------ initialization method/s ------ def __init__(self, path, logger=""): # define main path self.path = path # folder environment self.avgpath = "" self.subfolders = {} # theres an uncatched error... try: self.makeavgdir() except FileNotFoundError as e: print("Folder not found") except: print("WTF") # create log file in the avg folder if logger == "": # create a temporary log that only prints to console self.mylog = Logger("Avgrgb") elif isinstance(logger, Logger): # inheriths the logger from somewhere else self.mylog = logger elif isfile(logger): self.mylog = Logger("Avgrgb", logger) elif logger == "auto file": self.mylog = Logger("Avgrgb", pathfile=join(self.avgpath + "myavglogger.txt")) self.imgs = [] self.avg = MyRGBImg() self.algs = [] self.algimgs = [] def gather_pictures(self): ''' The methos checks inside self.path and gather image file''' # for now gathe all the files, next check for picture extensions p = self.path self.names = [f for f in listdir(p) if isfile(join(p, f))] for imgname in self.names: path, name, ext = get_pathname(imgname) if ext in ['.png', '.jpg']: imagepath = join(self.path, imgname) img = MyRGBImg() img.read_from_file(imagepath) self.imgs.append(img) mylog.log("Image: {0} imported successfully".format(name + ext)) mylog.log("Successully imported {0} images".format(self.imgs.n)) def average(self, aligned=True): ''' averaging procedure, this function saves the newly calculated average''' if aligned: dataset = self.algimgs else: dataset = self.imgs s = MyRGBImg(np.zeros(dataset[0].data.shape)) s = color.rgb2lab(s.data) for i, picture in enumerate(dataset): print("Averaging image: ", i) # convert both to lab im = color.rgb2lab(picture.data) #perform operations s += im s = s / float(len(dataset)) self.avg = MyRGBImg(color.lab2rgb(s)) def load_algs(self): with open(join(self.subfolders["results"], "shifts_log.txt")) as f: lines = f.readlines() self.algs = [] for line in lines: data = line.split(' | ') data = [int(d.strip()) for d in data] self.algs.append(data) lg.info("Alignments imported successfully") def align_images(self): self.algimgs = [] for i, image in enumerate(self.imgs): algimage = deepcopy(image) print("alg:", self.algs[i][0], self.algs[i][1]) algimage.move(-self.algs[i][1], -self.algs[i][0]) self.algimgs.append(algimage) lg.info("Images aligned successfully") def save_algimgs(self): for i, algimg in enumerate(self.algimgs): filename, ext = splitext(self.names[i]) algimg.save( join(self.subfolders["aligned_rgb_images"], ("alg_" + filename + ".png"))) lg.info("Saved aligned images successfully") def save_avg(self, name="avg_rgb.png"): self.avg.save(join(self.subfolders["results"], name)) def makeavgdir(self): # create a folder average into the dataset path # avg # |- processed_images # |- aligned_images # |- correlation_images # |- results # |- aligned_rgb_images self.avgpath = join(self.path, "avg") if not isdir(self.avgpath): mkdir(self.avgpath) subfolders = ["aligned_rgb_images", "results", "avg_transition"] for folder in subfolders: self.subfolders[folder] = join(self.avgpath, folder) if not isdir(self.subfolders[folder]): mkdir(self.subfolders[folder])
def average_mode(self, aligned=True, debug=False, transition=True): ''' Calculates the mode of the image array. The mode should represent the most frequent pixel value in an image array. For size reasons the array is split in quadrants. ''' # define the function to get the images according to the alignment sizedataset = len(self.imgs_names) if aligned: get_img = lambda i: self.get_alg_image(i) #self.get_alg_image(0) else: get_img = lambda i: self.get_image(i) #self.get_image(0) # get the image size dimx = get_img(0).data.shape[0] dimy = get_img(0).data.shape[1] # get image half size dimx2 = int(dimx / 2) dimy2 = int(dimy / 2) # quadrants coordinates as array indices quadrant = [(0, dimx2, 0, dimy2), (dimx2, dimx, 0, dimy2), (0, dimx2, dimy2, dimy), (dimx2, dimx, dimy2, dimy)] # decide how deep should be the the measured frequency # 128 = 8 mil colors # True color 24 bbp = 256 = 16'777'260 colors depth = 128 darray = np.arange(depth) resq = [] for q in quadrant: # calculate for each image, inside a quadrant the mode # x, y, c, freq stack = np.zeros((dimx2, dimy2, 3, depth), dtype=np.uint32) for i in range(sizedataset): pic = get_img(i) # for each pixel of the image i for x, ix in zip(range(q[0], q[1]), range(0, dimx2)): for y, iy in zip(range(q[2], q[3]), range(0, dimy2)): for c in range(3): # test in which bin the pixel goes # len(darray) = depth # the sistem could work with a dictionary too # stack[ix, iy, ic][d] += value # key error -> add the vaule # it should spare memory and computation for d in range(len(darray) - 1): if pic.data[x, y, c] < 0 or pic.data[x, y, c] > 1: raise ValueError("image not normalized") pv = pic.data[x, y, c] * depth if pv >= darray[d] and pv < darray[d + 1]: stack[ix, iy, c, d] += 1 # construct the resulting quadrant and store it in the resq list resquadrant = np.zeros((dimx2, dimx2, 3)) for x in range(0, dimx2): for y in range(0, dimy2): for c in range(0, 3): # for each pixel of quadrant calculate which pixel is # the most frequent maxfreq = 0 maxvalue = 0 for i in range(depth): if stack[x, y, c, i] > maxfreq: maxfreq = stack[x, y, c, i] maxvalue = darray[i] / float(depth) resquadrant[x, y, c] = maxvalue # this are the averaged quadrants calcquadrant = MyRGBImg(resquadrant) resq.append(calcquadrant) # recompose the picture picdata = MyRGBImg(np.zeros((dimx, dimy, 3))) for i, q in enumerate(quadrant): for x, ix in zip(range(q[0], q[1]), range(quadrant[0][0], quadrant[0][1])): for y, iy in zip(range(q[2], q[3]), range(quadrant[0][2], quadrant[0][3])): for c in range(3): picdata.data[x, y, c] = resq[i].data[ix, iy, c] self.avg = picdata if debug: picdata.show_image() plt.show()
class AvgRGBMemSave(object): ''' class to manage the colored average''' # ------ initialize method ------ def __init__(self, path, logger=""): # define main path self.path = path # folder environment self.avgpath = "" self.subfolders = {} # creates the necessary subfolders try: self.makeavgdir() except FileNotFoundError as e: print("Folder not found") except: print("WTF") # create log file in the avg folder if logger == "": # create a temporary log that only prints to console self.mylog = Logger("Avgrgb") elif isinstance(logger, Logger): # inheriths the logger from somewhere else self.mylog = logger elif isfile(logger): self.mylog = Logger("Avgrgb", logger) elif logger == "auto file": self.mylog = Logger("Avgrgb", pathfile=join(self.avgpath + "myavglogger.txt")) # instead of loading all the pictures in one array # create a path array that reads the pictures at will self.imgs_names = [] self.avg = MyRGBImg() self.algs = [] # TODO: # transform the getter setters into iterators def get_image(self, index): ''' returns a MyRGBImg given the index, loads the original images''' # read the image corresponding to the path pathtopic = join(self.path, self.imgs_names[index]) myimg = MyRGBImg() myimg.read_from_file(pathtopic) return myimg def get_alg_image(self, index): ''' returns a MyRGBImg given the index, loads the aligned images''' filename, ext = splitext(self.imgs_names[index]) pathtopic = join(self.subfolders["aligned_rgb_images"], ("alg_" + filename + ".png")) myimg = MyRGBImg() myimg.read_from_file(pathtopic) return myimg def save_alg_image(self, index, algimg): ''' Saves a MyRGBImg given the index, into the alg folder''' filename, ext = splitext(self.imgs_names[index]) # do I have to normalize? if not np.all(algimg.data <= 1) or not np.all(algimg.data >= 0): algimg.limit(1.0) algimg.save( join(self.subfolders["aligned_rgb_images"], ("alg_" + filename + ".png"))) def gather_pictures_names(self): '''Checks the folder self.path for images and constructs the array self.imgs_names which is used to gather the pictures ''' p = self.path # gather all the files filenames = [f for f in listdir(p) if isfile(join(p, f))] for filename in filenames: path, name, ext = get_pathname(filename) # select only the pictures if ext in ['.png', '.jpg']: self.imgs_names.append(filename) self.mylog.log("Loaded {0} images.".format(len(self.imgs_names))) def average(self, mode="Mode", aligned=True, debug=False, transition=True): if mode == "Mean": self.average_mean(aligned, debug, transition) if mode == "Median": self.average_median(aligned, debug, transition) if mode == "Mode": self.average_mode(aligned, debug, transition) if mode == "Sum": self.average_sum(aligned, debug, transition) def average_mode(self, aligned=True, debug=False, transition=True): ''' Calculates the mode of the image array. The mode should represent the most frequent pixel value in an image array. For size reasons the array is split in quadrants. ''' # define the function to get the images according to the alignment sizedataset = len(self.imgs_names) if aligned: get_img = lambda i: self.get_alg_image(i) #self.get_alg_image(0) else: get_img = lambda i: self.get_image(i) #self.get_image(0) # get the image size dimx = get_img(0).data.shape[0] dimy = get_img(0).data.shape[1] # get image half size dimx2 = int(dimx / 2) dimy2 = int(dimy / 2) # quadrants coordinates as array indices quadrant = [(0, dimx2, 0, dimy2), (dimx2, dimx, 0, dimy2), (0, dimx2, dimy2, dimy), (dimx2, dimx, dimy2, dimy)] # decide how deep should be the the measured frequency # 128 = 8 mil colors # True color 24 bbp = 256 = 16'777'260 colors depth = 128 darray = np.arange(depth) resq = [] for q in quadrant: # calculate for each image, inside a quadrant the mode # x, y, c, freq stack = np.zeros((dimx2, dimy2, 3, depth), dtype=np.uint32) for i in range(sizedataset): pic = get_img(i) # for each pixel of the image i for x, ix in zip(range(q[0], q[1]), range(0, dimx2)): for y, iy in zip(range(q[2], q[3]), range(0, dimy2)): for c in range(3): # test in which bin the pixel goes # len(darray) = depth # the sistem could work with a dictionary too # stack[ix, iy, ic][d] += value # key error -> add the vaule # it should spare memory and computation for d in range(len(darray) - 1): if pic.data[x, y, c] < 0 or pic.data[x, y, c] > 1: raise ValueError("image not normalized") pv = pic.data[x, y, c] * depth if pv >= darray[d] and pv < darray[d + 1]: stack[ix, iy, c, d] += 1 # construct the resulting quadrant and store it in the resq list resquadrant = np.zeros((dimx2, dimx2, 3)) for x in range(0, dimx2): for y in range(0, dimy2): for c in range(0, 3): # for each pixel of quadrant calculate which pixel is # the most frequent maxfreq = 0 maxvalue = 0 for i in range(depth): if stack[x, y, c, i] > maxfreq: maxfreq = stack[x, y, c, i] maxvalue = darray[i] / float(depth) resquadrant[x, y, c] = maxvalue # this are the averaged quadrants calcquadrant = MyRGBImg(resquadrant) resq.append(calcquadrant) # recompose the picture picdata = MyRGBImg(np.zeros((dimx, dimy, 3))) for i, q in enumerate(quadrant): for x, ix in zip(range(q[0], q[1]), range(quadrant[0][0], quadrant[0][1])): for y, iy in zip(range(q[2], q[3]), range(quadrant[0][2], quadrant[0][3])): for c in range(3): picdata.data[x, y, c] = resq[i].data[ix, iy, c] self.avg = picdata if debug: picdata.show_image() plt.show() def average_median(self, aligned=True, debug=False, transition=True): ''' Calculats the mean of a serie of pictures aligned takes the pictures from the the rgb aligned folder, while if False takes the original images ''' self.mylog.log("started the median procedure") # Chose which serie to average sizedataset = len(self.imgs_names) if aligned: get_img = lambda i: self.get_alg_image(i) #self.get_alg_image(0) else: get_img = lambda i: self.get_image(i) #self.get_image(0) # get image sizes dimx = get_img(0).data.shape[0] dimy = get_img(0).data.shape[1] dimx2 = int(dimx / 2) dimy2 = int(dimy / 2) # get the quadrant coordinates quadrant = [(0, dimx2, 0, dimy2), (dimx2, dimx, 0, dimy2), (0, dimx2, dimy2, dimy), (dimx2, dimx, dimy2, dimy)] # construct the median resq = [] for q in quadrant: # for each quadrant stack = np.zeros((dimx2, dimy2, 3, sizedataset)) for i in range(sizedataset): self.mylog.log("quadrant {0} image {1}".format(q, i)) pic = get_img(i) pic.data = pic.data[q[0]:q[1], q[2]:q[3], :] stack[:, :, :, i] = pic.data # calculate median med = np.median(stack, axis=3) medpic = MyRGBImg(med) medpic.show_image() plt.show() resq.append(medpic) # recompose the picture picdata = MyRGBImg(np.zeros((dimx, dimy, 3))) for i, q in enumerate(quadrant): for x, ix in zip(range(q[0], q[1]), range(quadrant[0][0], quadrant[0][1])): for y, iy in zip(range(q[2], q[3]), range(quadrant[0][2], quadrant[0][3])): for c in range(3): picdata.data[x, y, c] = resq[i].data[ix, iy, c] # show resulting image if debug: picdata.show_image() plt.show() self.avg = picdata def average_mean(self, aligned=True, debug=False, transition=True): ''' performs the mean of the images, aligned is True will use the aligned pictures while if false will use the original picture, for the transition, each averaging step is printed out ''' self.mylog.log("started the mean averaging procedure") sizedataset = len(self.imgs_names) if aligned: picture = self.get_alg_image(0) else: picture = self.get_image(0) # initialize sum variable s = MyRGBImg(np.zeros(picture.data.shape)) #s = color.rgb2lab(s.data) for i in range(sizedataset): if debug: self.mylog.log("Averaging image: " + str(i)) #load the picture if aligned: picture = self.get_alg_image(i) else: picture = self.get_image(i) # convert both to lab #im = color.rgb2lab(picture.data) im = picture.data #perform operations s += im # if the transition is true show what happens to each picture if transition: tr = s / float(i + 1) #avg = MyRGBImg(color.lab2rgb(tr)) avg = tr avg.save( join(self.subfolders["avg_transition"], "avg_tr_" + str(i) + ".png")) # calculate the average s = s / float(sizedataset) #self.avg = MyRGBImg(color.lab2rgb(s)) self.avg = s # small trick to align the image in the correct sense if they are # squared if self.avg.data.shape[0] == self.avg.data.shape[1]: self.avg.rotate(90) self.avg.flip_V() def average_sum(self, aligned=True, debug=False, transition=True): if aligned: get_img = lambda i: self.get_alg_image(i) #self.get_alg_image(0) else: get_img = lambda i: self.get_image(i) #self.get_image(0) sizedataset = len(self.imgs_names) s = MyRGBImg(np.zeros(get_img(0).data.shape)) for i in range(sizedataset): if debug: self.mylog.log("Averaging image: " + str(i)) #load the picture picture = get_img(i) #perform operations s += picture # calculate the average self.avg = s self.avg.limit(1) def load_algs(self): ''' This function loads the alignments calculated by the avg class''' with open(join(self.subfolders["results"], "shifts_log.txt")) as f: lines = f.readlines() self.algs = [] for line in lines: sdata = line.split(' | ') sdata = [d.strip() for d in sdata] data = [int(sdata[0]), int(sdata[1]), float(sdata[2])] self.algs.append(data) def align_images(self, debug=False): ''' this function, given the shifts calculated by avg folder class and loaded with the self.load_algs method, aligns the original images ''' self.mylog.log("Align procedure started") self.algimgs = [] for i in range(len(self.imgs_names)): if debug: self.mylog.log("Aligning image:" + str(i)) # load picture to align algimage = self.get_image(i) algimage.inspect() if algimage.data.shape[0] == algimage.data.shape[1]: # operations needed to align the rgb images algimage.rotate(90) algimage.flip_V() algimage.rotate(self.algs[i][2]) algimage.move(-self.algs[i][0], -self.algs[i][1]) else: # still doesnt work... algimage.squareit() algimage.rotate(self.algs[i][2]) algimage.move(-self.algs[i][0], -self.algs[i][1]) print("- After -") algimage.inspect() # save the image self.save_alg_image(i, algimage) def save_avg(self, name="avg_rgb.png"): ''' saves the average ''' self.avg.save(join(self.subfolders["results"], name)) def makeavgdir(self): ''' create a folder average into the dataset path avg |- processed_images |- aligned_images |- correlation_images |- results |- aligned_rgb_images |- avg_transition ''' self.avgpath = join(self.path, "avg") if not isdir(self.avgpath): mkdir(self.avgpath) subfolders = ["aligned_rgb_images", "results", "avg_transition"] for folder in subfolders: self.subfolders[folder] = join(self.avgpath, folder) if not isdir(self.subfolders[folder]): mkdir(self.subfolders[folder]) def transpose(self): ''' transpose all images ''' for i, img in enumerate(self.imgs): img.transpose() self.imgs.set_image(i, img) self.mylog.log("images transposed") def normalize(self): ''' normalize all images ''' for i, img in enumerate(self.imgs): img.normalize() self.imgs.set_image(i, img) self.mylog.log("images normalized") def binning(self, n=1): ''' bins all images ''' for i, img in enumerate(self.imgs): img.binning(n) self.imgs.set_image(i, img) self.mylog.log("images binned")