示例#1
0
class AvgFolderMem(object):

    # Initialization functions
    def __init__(self, path):
        # define the main path
        self.path = path

        # folder environment variable
        self.names = []
        self.avgpath = ""
        self.subfolders = {}

        # create the folder environment
        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, 'example.log'),
                       level=lg.INFO)

        # pictures
        self.init_imgs = ImageArray(self.path)
        self.imgs = NpyImageArray(
            (self.subfolders["processed_images"], "proc_imgs.npy",
             len(self.init_imgs.paths)))

        # initialize variables
        self.template = MyImage()
        self.templateft = None
        self.angles_list = []

        self.templaterotsft = None

        folder = self.subfolders["aligned_images"]
        bname = "algimage.npy"
        q = self.imgs.n
        self.algimgs = NpyImageArray((folder, bname, q))

        folder = self.subfolders["correlation_images"]
        bname = "correlation.npy"
        q = self.imgs.n
        self.corrs = NpyImageArray((folder, bname, q))

        self.shifts = []

        self.avg = MyImage()

    def refine_angles(self, min_angle, max_angle):
        # builds a angles tree given the min max angle
        pass

    def makeavgdir(self):
        # create a folder average into the dataset path
        # avg
        #  |- processed_images
        #  |- aligned_images
        #  |- correlation_images
        #  |- results
        #  |- template_rot
        self.avgpath = join(self.path, "avg")
        if not isdir(self.avgpath):
            mkdir(self.avgpath)

        subfolders = [
            "processed_images", "aligned_images", "correlation_images",
            "results", "template_rot"
        ]
        for folder in subfolders:
            self.subfolders[folder] = join(self.avgpath, folder)
            if not isdir(self.subfolders[folder]):
                mkdir(self.subfolders[folder])

    def gather_pictures(self):
        for i, image in enumerate(self.init_imgs):
            self.imgs.set_image(i, image)

    # image operations
    def c2gscale(self):
        for i, img in enumerate(self.imgs):
            img.convert2grayscale()
            self.imgs.set_image(i, img)
        lg.info("dataset converted to grayscale")

    def squareit(self):
        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))

    # template handling
    def generate_template(self, option, rot_precision=None):
        if type(option) is str:
            if option == "UseFirstImage":
                self.template = self.imgs.get_image(0)
                self.templateft = ImgFFT(self.template)
                self.templateft.ft()
            elif option == "Average":
                self.average(False)
                self.template = self.avg
                self.templateft = ImgFFT(self.template)
                self.templateft.ft()
            else:
                raise TemplateTypeError(option)
        elif type(option) == MyImage:
            self.template = option
            self.templateft = ImgFFT(self.template)
            self.templateft.ft()
        else:
            raise TemplateTypeError(type(option))
        lg.info("template created: {0}".format(option))

        if type(rot_precision) == tuple:

            print("Creating rotation references")

            # rot_precision format = (from, to, precision)
            frm = rot_precision[0]
            to = rot_precision[1]
            prec = rot_precision[2]
            self.angles_list = np.arange(frm, to, prec)

            print("From", frm, "to", to, "precision", prec)
            print("Total:", len(self.angles_list), "angles")
            self.templaterotsft = NpyFTArray(
                (self.subfolders["template_rot"], "template_rot_ft.npy",
                 len(self.angles_list)))
            for i, angle in enumerate(self.angles_list):
                print("creating angle: ", angle)
                rot = deepcopy(self.template)
                rot.rotate(angle)

                rotft = ImgFFT(rot)
                rotft.ft()

                self.templaterotsft.set_image(i, rotft)

    def align_images(self, debug=False):
        c = 0

        anglestree = AnglesTree(-1, 1, 0.1, self.template)

        for image in self.imgs:

            angle = anglestree.analyze_image(image)

            #            # generate the fourier transform of the image
            #            imgft = ImgFFT(image)
            #            imgft.ft()
            #
            #            # calculate the rotations
            #            smax = 0
            #            idxmax = 0
            #            for idx, temp in enumerate(self.templaterotsft):
            #                corr = temp.correlate(imgft)
            #                s, dx, dy = corr.find_peak(1)
            #
            #                if s > smax:
            #                    smax = s
            #                    idxmax = idx
            #
            #            angle = float(self.angles_list[idxmax])

            print("angle found", angle)

            rotalgimage = deepcopy(image)
            rotalgimage.rotate(-angle)

            # calculate the shifts
            rotalgimageft = ImgFFT(rotalgimage)
            rotalgimageft.ft()

            corr = rotalgimageft.correlate(self.templateft)
            self.corrs.set_image(c, corr)

            dx, dy = corr.find_translation(1)
            self.shifts.append((dx, dy, angle))

            print("shifts:", dx, dy)

            rotalgimage.move(dx, dy)

            self.algimgs.set_image(c, rotalgimage)

            if debug:
                print("Correlated image:", c)

            lg.info("correlated image n: " + str(c))

            c += 1

    def average(self, aligned=True, debug=False):
        if aligned:
            dataset = self.algimgs
        else:
            dataset = self.imgs
        s = MyImage(np.zeros(dataset[0].data.shape))
        for i, picture in enumerate(dataset):
            if debug:
                print("Averaging picture:", i)
            s += picture

        self.avg = s / dataset.n

    # I/O methods
    def get_template_path(self):
        return join(self.avgpath, "template.png")

    def get_avg_path(self):
        return join(self.subfolders["results"], "avg.png")

    def save_template(self):
        self.template.save(join(self.avgpath, "template.png"))

    def load_template(self, filepathname):
        self.template.read_from_file(filepathname)

    def save_avg(self):
        self.avg.save(self.get_avg_path())

    def save_shifts(self):
        with open(join(self.subfolders["results"], "shifts_log.txt"),
                  "w") as f:
            for shift in self.shifts:
                f.write("{0[0]:d} | {0[1]:d} | {0[2]:.3f}\n".format(shift))
        lg.info("Shifts saved")

    def load_shifts(self):
        with open(join(self.subfolders["results"], "shifts_log.txt")) as f:
            lines = f.readlines()

        self.shifts = []
        for line in lines:
            sdata = line.split(' | ')
            sdata = [d.strip() for d in sdata]
            data = [int(sdata[0]), int(sdata[1]), float(sdata[2])]
            print(data)
            self.shifts.append(data)
示例#2
0
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"))
示例#3
0
class AvgFolder(object):

    # Initialization functions
    def __init__(self, path):
        # define the main path
        self.path = path

        # folder environment variable
        self.names = []
        self.avgpath = ""
        self.subfolders = {}

        # create the folder environment
        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, 'example.log'),
                       level=lg.INFO)

        # initialize variables
        self.imgs = []
        self.template = MyImage()
        self.templateft = None
        self.angles_list = []
        self.templaterotsft = []

        self.algimgs = []
        self.corrs = []
        self.shifts = []

        self.avg = MyImage()

    def gather_pictures(self):
        # for now gather 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 ['.jpg', '.png']:
                imagepath = join(self.path, imgname)
                img = MyImage()
                img.read_from_file(imagepath)
                self.imgs.append(img)
                lg.info("Image: {0} imported successfully".format(imagepath))

    def makeavgdir(self):
        # create a folder average into the dataset path
        # avg
        #  |- processed_images
        #  |- aligned_images
        #  |- correlation_images
        #  |- results
        self.avgpath = join(self.path, "avg")
        if not isdir(self.avgpath):
            mkdir(self.avgpath)

        subfolders = [
            "processed_images", "aligned_images", "correlation_images",
            "results"
        ]
        for folder in subfolders:
            self.subfolders[folder] = join(self.avgpath, folder)
            if not isdir(self.subfolders[folder]):
                mkdir(self.subfolders[folder])

    # image operations
    def c2gscale(self):
        for img in self.imgs:
            img.convert2grayscale()
        lg.info("dataset converted to grayscale")

    def squareit(self):
        for img in self.imgs:
            img.squareit()
        lg.info("dataset squared")

    def transpose(self):
        for img in self.imgs:
            img.transpose()
        lg.info("dataset transposed")

    def normalize(self):
        for img in self.imgs:
            img.normalize()
        lg.info("dataset normalized")

    def binning(self, n=1):
        for img in self.imgs:
            img.binning(n)
        lg.info("dataset binned {0} times".format(n))

    # template handling
    def generate_template(self, option, rot_precision=None):
        if type(option) is str:
            if option == "UseFirstImage":
                self.template = self.imgs[0]
                self.templateft = ImgFFT(self.template)
                self.templateft.ft()
            elif option == "Average":
                self.average(False)
                self.template = self.avg
                self.templateft = ImgFFT(self.template)
                self.templateft.ft()
            else:
                raise TemplateTypeError(option)
        elif type(option) == MyImage:
            self.template = option
            self.templateft = ImgFFT(self.template)
            self.templateft.ft()
        else:
            raise TemplateTypeError(type(option))
        lg.info("template created: {0}".format(option))

        if type(rot_precision) == tuple:

            print("Creating rotation references")

            # rot_precision format = (from, to, precision)
            frm = rot_precision[0]
            to = rot_precision[1]
            prec = rot_precision[2]
            self.angles_list = np.arange(frm, to, prec)

            print("From", frm, "to", to, "precision", prec)
            print("Total:", len(self.angles_list), "angles")

            for angle in self.angles_list:
                print("creating angle: ", angle)
                rot = deepcopy(self.template)
                rot.rotate(angle)

                rotft = ImgFFT(rot)
                rotft.ft()

                self.templaterotsft.append(rotft)

    def align_images(self, debug=False):
        c = 0
        for image in self.imgs:

            # generate the fourier transform of the image
            imgft = ImgFFT(image)
            imgft.ft()

            # calculate the rotations
            smax = 0
            idxmax = 0
            for idx, temp in enumerate(self.templaterotsft):
                corr = temp.correlate(imgft)
                s, dx, dy = corr.find_peak(1)

                if s > smax:
                    smax = s
                    idxmax = idx

            print("angle found", self.angles_list[idxmax])

            rotalgimage = deepcopy(image)
            rotalgimage.rotate(-self.angles_list[idxmax])

            # calculate the shifts
            rotalgimageft = ImgFFT(rotalgimage)
            rotalgimageft.ft()

            corr = rotalgimageft.correlate(self.templateft)
            self.corrs.append(corr)

            dx, dy = corr.find_translation(1)
            self.shifts.append((dx, dy))

            print("shifts:", dx, dy)

            rotalgimage.move(dx, dy)
            self.algimgs.append(rotalgimage)

            if debug:
                print("Correlated image:", c)
            lg.info("correlated image n: " + str(c))

            c += 1

    def average(self, aligned=True):
        if aligned:
            dataset = self.algimgs
        else:
            dataset = self.imgs
        s = MyImage(np.zeros(dataset[0].data.shape))
        for picture in dataset:
            s += picture

        self.avg = s / len(dataset)

    # I/O methods

    def save_template(self):
        self.template.save(join(self.avgpath, "template.png"))

    def load_template(self, filepathname):
        self.template.read_from_file(filepathname)

    def save_imgs(self):
        for i, img in enumerate(self.imgs):
            filename, ext = splitext(self.names[i])
            img.save(
                join(self.subfolders["processed_images"],
                     "proc_" + filename + ".png"))
        lg.info("processed dataset saved, images {0}".format(i))

    def laod_imgs(self):
        lg.info("Loading processed images")
        p = self.subfolders["processed_images"]
        names = [f for f in listdir(p) if isfile(join(p, f))]

        self.imgs = []
        for name in names:
            img = MyImage()
            img.read_from_file(name)
            self.imgs.append(img)
            lg.info("Image: {0} imported successfully".format(name))

    def save_algimgs(self):
        for i, algimg in enumerate(self.algimgs):
            filename, ext = splitext(self.names[i])
            algimg.save(
                join(self.subfolders["aligned_images"],
                     ("alg_" + filename + ".png")))
        lg.info("aligned dataset saved, images {0}".format(i))

    def laod_algimgs(self):
        lg.info("Loading aligned images")
        p = self.subfolders["aligned_images"]
        names = [f for f in listdir(p) if isfile(join(p, f))]

        self.imgs = []
        for name in names:
            img = MyImage()
            img.read_from_file(name)
            self.imgs.append(img)
            lg.info("Image: {0} imported successfully".format(name))

    def save_corrs(self):
        for i, corr in enumerate(self.corrs):
            filename, ext = splitext(self.names[i])
            corr.save(
                join(self.subfolders["correlation_images"],
                     ("corr_" + filename + ".png")))
        lg.info("correlations dataset saved, images {0}".format(i))

    def laod_corrs(self):
        lg.info("Loading correlation images")
        p = self.subfolders["correlation_images"]
        names = [f for f in listdir(p) if isfile(join(p, f))]

        self.imgs = []
        for name in names:
            img = MyImage()
            img.read_from_file(name)
            self.imgs.append(img)
            lg.info("Image: {0} imported successfully".format(name))

    def save_avg(self):
        self.avg.save(join(self.subfolders["results"], "avg.png"))

    def save_shifts(self):
        with open(join(self.subfolders["results"], "shifts_log.txt"),
                  "w") as f:
            for shift in self.shifts:
                f.write("{0[0]:d} | {0[1]:d}\n".format(shift))
        lg.info("Shifts saved")

    def load_shifts(self):
        with open(join(self.subfolders["results"], "shifts_log.txt")) as f:
            lines = f.readlines()

        self.shifts = []
        for line in lines:
            data = line.split(' - ')
            data = [int(d.strip()) for d in data]
            print(data)
            self.shifts.append(data)