def __data_generation(self, batch_names):
        # Initialization of the batches
        X = np.empty((self.batch_size, self.size[0], self.size[1], 3))
        # Change for using only 1 class "FIBERS" as output , last dimension to 1
        y = np.empty((self.batch_size, self.size[0], self.size[1], 1))
        for i, name in enumerate(batch_names):
            X[i, :, :, :] = load_image(self.path,
                                       name)  # Load the data for the image

            # Load the data for the classification images
            if isfile(
                    join(self.path,
                         name.replace(self.data_type,
                                      '_FIB' + self.data_type))):
                # Load the image of the annotations
                label = cv2.imread(
                    join(self.path,
                         name.replace(self.data_type,
                                      '_FIB' + self.data_type)), 0)
                # Threshold the image (while reading some annotations are not with values [0,255] only)
                _, label = cv2.threshold(label, 127, 255, 0)
            else:
                # Create a label matrix with all zeros if the image was not annotated (doesn't have fibers)
                label = np.zeros(self.size[0], self.size[1])
            y[i, :, :, 0] = 1 * (
                label == 255
            )  # Classify as 1 if the value is 255 for the "fiber" mask.
            # Change for using only 1 class "FIBERS" as output
            # y[i,:,:,1] = 1*(label==0) # Classify as 1 if the value is 0 for the "not a fiber" mask.
        return X, y
 def remove_trash(self):
     """
     This function produces a new file PRED2 that removes contours which are too small.
     :return:
     """
     for num_name, name in enumerate(self.names):
         img = load_image(self.path, name)
         contours_per_image = self.images_contours[num_name]
         mask = np.zeros(img.shape, dtype="uint8")
         for num_contour, contour in enumerate(contours_per_image):
             if self.classification[num_name][num_contour] == 0:
                 img = cv2.drawContours(img, contour, -1, 255, -1)
         cv2.imwrite(join(self.path, name), img)
    def get_contours(self, names):
        """
        A function that obtains the contours for each of the prediction images
        :return: A list with the contours for each of the images
        """
        images_contours = []
        label_contours = []
        for num_name, name in enumerate(names):
            img = load_image(self.path, name)
            img = img.astype(np.uint8)
            __, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
            __, contours_per_image, __ = cv2.findContours(
                thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
            images_contours.append(contours_per_image)

        return images_contours
 def save_numbered_fibers(self, save_path):
     """
     This function saves the prediction images. For each of the contours a number with its area will appear beside.
     :param save_path: Where should these new images should be included
     :return:
     """
     # For each image get its contours and write the area in a new image
     for num_name, name in enumerate(self.names):
         img = load_image(self.path, name)
         contours_per_image = self.images_contours[num_name]
         for num_contour, contour in enumerate(contours_per_image):
             cv2.putText(img,
                         str(self.images_areas[num_name][num_contour]),
                         (contour[0][0][0], contour[0][0][1]),
                         fontFace=cv2.FONT_HERSHEY_TRIPLEX,
                         fontScale=0.8,
                         color=4,
                         thickness=1)
         cv2.imwrite(join(save_path, name.replace('PRED', 'AREA')), img)
 def get_image_size(self):
     img = load_image(self.path, self.names[-1])
     return img.shape[0]