def load_centers(self, path):  #Deprecated
        '''
        loads centers from mat files in given path
        '''

        self.log.start('Load Centers', 3, update_num=1)
        if os.path.isfile(self.centers_path):
            self.centers = read_dict_from_matfile(self.centers_path)

            self.log.end()
        else:
            self.centers = {}

            sift_centers = read_arr_from_matfile(
                path + "/Dictionaries/sift_centers.mat", 'dic')
            self.centers['sift_centers'] = sift_centers

            self.log.update()
            mr_resp_centers = read_arr_from_matfile(
                path + "/Dictionaries/mr_resp_centers.mat", 'dic')
            self.centers['mr_resp_centers'] = mr_resp_centers

            self.log.update()
            gist_centers = read_arr_from_matfile(
                path + "/Dictionaries/gist_centers.mat", 'dic')
            self.centers['gist_centers'] = gist_centers

            self.log.update()

            self.log.start("Writing Centers File to HDD", 1, 1)
            if self.save_mode:
                write_dict_to_matfile(self.centers, self.centers_path)

            self.log.update()
    def load_images(self, path):
        '''
        loads images from given path into an array list
        '''
        self.log.start("Load Images", 1, 1)
        path_list = path_to_subfolder_pathlist(path, filter=".jpg")
        self.image_path_list = path_list

        self.image_list = np.array([
            os.path.splitext(basename(filepath))[0] for filepath in path_list
        ])

        if not self.seq_reading:
            if os.path.isfile(self.image_array_list_path):
                self.image_array_list = read_arr_from_matfile(
                    self.image_array_list_path,
                    os.path.splitext(basename(self.image_array_list_path))[0])

            else:
                self.image_array_list = np.array(
                    [np.array(Image.open(filepath)) for filepath in path_list])
                write_arr_to_matfile(
                    self.image_array_list, self.image_array_list_path,
                    os.path.splitext(basename(self.image_array_list_path))[0])

        self.log.update()
    def label_sp(self, i, seg_folder, path_list):
        '''
        labels superpixel image from path_list[i] uniquely
        '''

        self.sp_array = read_arr_from_matfile(path_list[i], "S")
        self.sp_labeled = np.zeros(self.sp_array.shape) - 1
        self.label_ffw()
    def load_super_pixel(self, path, seg_method):
        '''
        loads super pixels from given path or calculates them if not created yet
        @param path: Path to superpixels
        @param seg_method: segmantation method and parameters to calculate superpixels
        '''

        #calculate superpixels which are not created
        sp_calculator = SPCalculator(self)
        sp_calculator.calculate_sp(seg_method, path)

        #load superpixels if seq_reading is set to False
        self.log.start("Load SuperPixels", 1, 1)
        path_list = path_to_subfolder_pathlist(path, filter=".mat")

        self.sp_array_path_list = path_list
        if not self.seq_reading:
            if os.path.isfile(self.sp_array_list_path):
                self.sp_array_list = read_arr_from_matfile(
                    self.sp_array_list_path,
                    os.path.splitext(basename(self.sp_array_list_path))[0])

            else:
                for i in range(len(path_list)):
                    try:
                        sp = read_arr_from_matfile(path_list[i], 'superPixels')
                    except:
                        print "Failure in File {0}".format(i)
                        print self.image_list[i]
                        raise

                self.sp_array_list = np.array([
                    read_arr_from_matfile(filepath, 'superPixels')
                    for filepath in path_list
                ])

                if self.save_mode:
                    write_arr_to_matfile(
                        self.sp_array_list, self.sp_array_list_path,
                        os.path.splitext(basename(self.sp_array_list_path))[0])

        self.log.update()
    def saliency(self, seg_folder, saliency_folder, k):
        '''
        clusters and relabels saliency maps of images
        @param seg_folder: superpixel folder
        @param saliency_folder: folder to salience-images
        '''

        self.log.start("Recomputing Saliency SuperPixels",
                       len(self.image_array_list), 1)
        self.log.verbose = True

        path_list = path_to_subfolder_pathlist(saliency_folder, filter=".mat")

        for i in range(len(path_list)):

            self.sp_array = read_arr_from_matfile(path_list[i], "S")
            self.sp_labeled = np.zeros(self.sp_array.shape) - 1

            pixel = np.reshape(self.sp_array, (self.sp_array.shape[0]*\
                                               self.sp_array.shape[1]))

            #clustering
            centroids, _ = kmeans(pixel, 60)
            # quantization
            qnt, _ = vq(pixel, centroids)

            centers_idx = np.reshape(
                qnt, (self.sp_array.shape[0], self.sp_array.shape[1]))
            self.sp_array = centroids[centers_idx]

            self.label_ffw()

            write_arr_to_matfile(self.sp_labeled,
                            seg_folder + "/" + \
                            os.path.splitext(basename(path_list[i]))[0] +".mat",
                            "superPixels")
            self.log.update()
    def load_segment_descriptors(self, path, seg_suffix):  #Deprecated
        '''
        loads Segment Descriptors
        '''


        self.segment_descriptor_dict_path = self.save_path + \
            "segment_descriptor_dict_"+seg_suffix + ".mat"
        if os.path.isfile(self.segment_descriptor_dict_path):
            self.log.start("Load Segment Descriptors", 1, 1)
            self.segment_descriptor_dict = read_dict_from_matfile(
                self.segment_descriptor_dict_path)
            self.log.update()

        else:
            self.segment_descriptor_dict = {}

            self.calculate_segment_features(path)

            self.log.start("Load Segment Descriptors", len(self.feature_list),
                           1)

            for feature in self.feature_list:
                image_features = np.array([
                    read_arr_from_matfile(filepath, 'desc')
                    for filepath in path_to_subfolder_pathlist(
                        path + '/' + feature, filter=".mat")
                ])
                self.segment_descriptor_dict[feature] = image_features
                self.log.update()

            self.log.start("Writing Segment Descriptor File to HDD", 1, 1)
            if self.save_mode:
                write_dict_to_matfile(self.segment_descriptor_dict,
                                      self.segment_descriptor_dict_path)
            self.log.update()
    def label_images(self, label_path, sp_label_path):  #Deprecated
        '''
        label superpixels according to their image labels
        '''

        self.log.start("Load Image Labels", 1, 1)
        path_list = path_to_subfolder_pathlist(label_path, filter=".mat")

        if os.path.isfile(self.image_label_array_list_path):
            self.image_label_array_list = read_arr_from_matfile(
                self.image_label_array_list_path,
                os.path.splitext(basename(
                    self.image_label_array_list_path))[0])

        else:
            self.image_label_array_list = np.array([
                read_arr_from_matfile(filepath, 'S') for filepath in path_list
            ])

            write_arr_to_matfile(
                self.image_label_array_list, self.image_label_array_list_path,
                os.path.splitext(basename(
                    self.image_label_array_list_path))[0])

        self.log.update()

        self.log.start("Labeling SuperPixels", len(self.image_array_list), 1)

        if os.path.isfile(sp_label_path + "/segIndex.mat"):
            self.segment_label_index = read_dict_from_matfile(sp_label_path +
                                                              "/segIndex.mat")
            self.log.end()
        else:
            self.segment_label_index = {}
            self.segment_label_index['label'] = np.array([])
            self.segment_label_index['sp'] = np.array([])
            self.segment_label_index['image'] = np.array([])
            self.segment_label_index['spSize'] = np.array([])

            self.label_thread_lock.acquire()
            for i in range(len(self.image_label_array_list)):

                #print "Active Threads: {0}".format(8-self.max_thread)
                self.max_thread.acquire()
                try:
                    t = threading.Thread(target=self.label_thread,
                                         args=(
                                             sp_label_path,
                                             i,
                                         ))
                    t.start()
                except:
                    pass

            print "Acquire label_thread_lock"
            self.label_thread_lock.acquire()
            self.log.start("Writing Segment Label File",
                           len(self.image_label_array_list), 1)
            try:
                for i in range(len(self.image_label_array_list)):
                    self.segment_label_index['label'] = \
                        np.concatenate((self.segment_label_index['label'],
                                self.label_thread_index[i]['label']))
                    self.segment_label_index['sp'] = \
                        np.concatenate((self.segment_label_index['sp'],
                                self.label_thread_index[i]['sp']))
                    self.segment_label_index['image'] = \
                        np.concatenate((self.segment_label_index['image'],
                                np.ones(self.label_thread_index[i]['sp'].shape)*i))
                    self.segment_label_index['spSize'] = \
                        np.concatenate((self.segment_label_index['spSize'],
                                self.label_thread_index[i]['spSize']))
                    self.log.update()
            finally:
                try:
                    self.label_thread_lock.release()
                except:
                    pass

            write_dict_to_matfile(self.segment_label_index,
                                  sp_label_path + "/segIndex.mat")
    def load_global_descriptors(self, path, subfolder):  #Deprecated
        '''
        loads global descriptors from mat files in given path
        '''

        self.log.start('Load Global Descriptors', 7, update_num=1)
        if os.path.isfile(self.global_descriptor_dict_path):
            self.global_descriptor_dict = read_dict_from_matfile(
                self.global_descriptor_dict_path)

            self.log.end()
        else:
            self.global_descriptor_dict = {}

            print "Loading Color Histogramm"
            co_hist = np.array([
                read_arr_from_file(filepath, 'coHist')
                for filepath in path_to_subfolder_pathlist(
                    path + '/' + 'coHist', filter=".mat")
            ])
            self.log.update("Loading colorGist")
            color_gist = np.array([
                read_arr_from_file(filepath, 'colorGist')
                for filepath in path_to_subfolder_pathlist(
                    path + '/' + 'colorGist', filter=".txt")
            ])
            self.log.update("Loading Spatial Pyramid")
            spatial_pyramid_h = np.array([
                read_arr_from_matfile(filepath, 'H')
                for filepath in path_to_subfolder_pathlist(
                    path + '/' + 'SpatialPyrDenseScaled',
                    filter="hist_200.mat")
            ])
            self.log.update()
            spatial_pyramid_pyr = np.array([
                read_arr_from_matfile(filepath, 'pyramid')
                for filepath in path_to_subfolder_pathlist(
                    path + '/' + 'SpatialPyrDenseScaled',
                    filter="pyramid_200.mat")
            ])
            self.log.update()
            spatial_pyramid_texton = np.array([
                read_dict_from_matfile(filepath)
                for filepath in path_to_subfolder_pathlist(
                    path + '/' + 'SpatialPyrDenseScaled', filter="ind_200.mat")
            ])

            self.log.update("Loading mr filter")
            mr_filter = np.array([
                read_arr_from_file(filepath, 'texton')
                for filepath in path_to_subfolder_pathlist(
                    path + '/' + 'Textons/mr_filter', filter=".mat")
            ])
            self.log.update("Loading Sift Textons")
            sift_textons = np.array([
                read_arr_from_file(filepath, 'texton')
                for filepath in path_to_subfolder_pathlist(
                    path + '/' + 'Textons/sift_textons', filter=".mat")
            ])

            self.global_descriptor_dict['coHist'] = co_hist
            self.global_descriptor_dict['colorGist'] = color_gist
            self.global_descriptor_dict['H'] = spatial_pyramid_h
            self.global_descriptor_dict['pyramid'] = spatial_pyramid_pyr
            self.global_descriptor_dict['texton_ind'] = spatial_pyramid_texton

            self.global_descriptor_dict['mr_filter'] = mr_filter
            self.global_descriptor_dict['sift_textons'] = sift_textons

            self.log.update()

            self.log.start("Writing Global Descriptor File to HDD", 1, 1)

            if self.save_mode:
                write_dict_to_matfile(self.global_descriptor_dict,
                                      self.global_descriptor_dict_path)

            self.log.update()
    def _gt_thread(self, i, seg_folder, path_list):
        '''
        Ground Truth Thread
        @param i: index to current image
        @param seg_folder: superpixel folder
        @param path_list: path to images 
        '''

        if not os.path.isfile(seg_folder + "/" + \
                        os.path.splitext(basename(path_list[i]))[0] +".mat"):

            segments = read_arr_from_matfile(path_list[i], "S")

            segments_new = segments.copy()
            labels = np.unique(segments)
            label_new = 1
            for l in labels:
                segment = (segments == l) * 1
                segment_arg = np.argwhere(segments == l)

                mask = np.array([[1, 1, 1, 1, 1], [1, 0, 1, 0, 1],
                                 [1, 1, 1, 1, 1], [1, 0, 1, 0, 1],
                                 [1, 1, 1, 1, 1]])

                cluster = {}

                points = range(len(segment_arg))
                c = 0

                for p in range(len(segment_arg)):
                    q = queue()
                    if p in points:
                        points.remove(p)
                        cluster[c] = [p]
                        q.put(p)

                        mod_arr = segments_new == -5
                        while (not q.empty()):

                            i = q.get()

                            seg = segment_arg[i]
                            row_up = max(0, seg[0] - 1)
                            row_down = min(segment.shape[0], seg[0] + 2)
                            col_left = max(0, seg[1] - 1)
                            col_right = min(segment.shape[1], seg[1] + 2)

                            if seg[0] - 1 < 0:
                                mask_up = 0
                                mask_down = 2
                            elif seg[0] + 1 >= segment.shape[0]:
                                mask_up = 3
                                mask_down = 5
                            else:
                                mask_up = 1
                                mask_down = 4

                            if seg[1] - 1 < 0:
                                mask_left = 0
                                mask_right = 2
                            elif seg[1] + 1 >= segment.shape[1]:
                                mask_left = 3
                                mask_right = 5
                            else:
                                mask_left = 1
                                mask_right = 4


                            segment[row_up:row_down, col_left:col_right] += \
                                mask[mask_up:mask_down,mask_left:mask_right]

                            [(cluster[c].append(j), q.put(j), points.remove(j))
                             for j in points if segment[segment_arg[j][0],
                                                        segment_arg[j][1]] > 1]

                            segments_new[segment > 1] = label_new
                            segment[row_up:row_down, col_left:col_right] -= \
                                mask[mask_up:mask_down,mask_left:mask_right]

                        label_new += 1

                        c += 1

                print l
            write_arr_to_matfile(segments_new,
                            seg_folder + "/" + \
                            os.path.splitext(basename(path_list[i]))[0] +".mat",
                            "superPixels")