Пример #1
0
    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
Пример #2
0
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"))
Пример #3
0
    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()