def align_images(self, angle_space_mode, angle_space, debug=False):
        ''' The function takes a serie of images and aligns it against the
        template, outputs the resulting images into the aligned_images folder
        input:
          angle_space_mode = ("fixed"|"tree")
          angle_space = tuple (min angle, max angle, precision)
        '''
        c = 0
        if angle_space_mode == "fixed":
            self.generate_rotref(angle_space)
        elif angle_space_mode == "tree":
            self.anglestree = AnglesTree(angle_space, self.template)

        for image in self.imgs:

            if angle_space_mode == "fixed":
                # 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])
            elif angle_space_mode == "tree":
                angle = self.anglestree.analyze_image(image)

            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)

            c += 1
    def calculate_bandpass(self, inradius, insmooth, outradius, outsmooth):
        ''' This method calculates the filter and saves the corresponding images
        the power spectrum (self.psimage) and the result of the filter
        (self.iftimage) in the temp folder
        '''

        #transfrom the image
        self.ftimage = ImgFFT(self.inimage.image)
        self.ftimage.ft()

        # create bandpass mask
        mask = Mask(self.inimage.image.data.shape)
        mask.bandpass(inradius, insmooth, outradius, outsmooth)
        self.ftimage.apply_mask(mask)

        # represent the masked ps
        self.ftimage.power_spectrum()
        self.psimage = ImagePath(self.name + "_ps", self.ftimage.ps,
                                 self.bufpath)
        self.savegif(self.psimage, (500, 500))

        # calculate inverse transform
        self.ftimage.ift()
        self.iftimage = ImagePath(self.name + "ift", self.ftimage.imgifft,
                                  self.bufpath)
        self.savegif(self.iftimage, (500, 500))
    def analyze_image(self, image):
        print("------------ Analysis image ------------")

        langle = self.langle
        rangle = self.rangle

        # reset cc values
        for angle in self.angles_nodes:
            angle.ccvalue = None

        imageft = ImgFFT(image.data)
        imageft.ft()

        #aprec = langle.angle + self.prec - rangle.angle

        while langle.angle + self.prec < rangle.angle:
            print("---------", langle.angle + self.prec, rangle.angle,
                  "-----------")
            # test the three angles
            print("Analyzing:", langle.angle, rangle.angle)

            hangle = (langle.angle + rangle.angle) / 2.
            halfangle = None
            print("halfangle", hangle)
            for anglenode in self.angles_nodes:
                if hangle == anglenode.angle:
                    print("Halfangle already existing!")
                    halfangle = anglenode

            if halfangle is None:
                print("Creating halfangle")
                halfangle = AngleNode((langle.angle + rangle.angle) / 2.,
                                      self.angles, self.template)
                self.angles_nodes.append(halfangle)

            if langle.ccvalue is None:
                cc1 = langle.template.correlate(imageft)
                b1, x, y = cc1.find_peak()
                langle.ccvalue = b1

            if halfangle.ccvalue is None:
                cc2 = halfangle.template.correlate(imageft)
                b2, x, y = cc2.find_peak()
                halfangle.ccvalue = b2

            #cc3 = rangle.template.correlte(imageft)

            print("ccs:", langle.ccvalue, halfangle.ccvalue)  #, cc3)

            if langle.ccvalue >= halfangle.ccvalue:
                rangle = halfangle
            else:
                langle = halfangle

            print("Angles: ", langle.angle, rangle.angle)

            #aprec = np.sqrt(langle.angle**2 + rangle.angle**2)

        return (langle.angle + rangle.angle) / 2.
示例#4
0
    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)
示例#5
0
    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 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))
    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))

        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)
示例#8
0
    def init_template(self):
        print("creating angle: ", self.angle)
        rot = deepcopy(self.inittemplate)

        rot.rotate(self.angle)

        rotft = ImgFFT(rot)
        rotft.ft()
        self.template = rotft

        self.angles.append(self.angle)
示例#9
0
    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 savegif(self, imagepath, size):
        ''' Given a class imagepath and the size of the images, saves into the
        temp folder the associated image
        '''

        # calculate ft for resizing
        imft = ImgFFT(imagepath.image.data)
        imft.ft()
        im = imft.resize_image(size[0], size[1])

        # save resized image
        imsave(imagepath.gifname, im.data, format="gif")
示例#11
0
    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")
示例#12
0
    def generate_rotref(self, angle_space):
        print("Creating rotation references")

        # rot_precision format = (from, to, precision)
        frm = angle_space[0]
        to = angle_space[1]
        prec = angle_space[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)
示例#13
0
 def get_image(self, i):
     with open(self.paths[i], 'rb') as f:
         imgfft = np.load(f)
         ft = ImgFFT(MyImage())
         ft.imgfft = imgfft
     return ft