Exemplo n.º 1
0
def compute_class_weights(labels_dir, label_values):
    '''
    Arguments:
        labels_dir(list): Directory where the image segmentation labels are
        num_classes(int): the number of classes of pixels in all images

    Returns:
        class_weights(list): a list of class weights where each index represents
            each class label and the element is the class weight for that label.

    '''
    # TODO: need Test and Debug
    image_files = filelist(labels_dir, ifPath=True, extension='.png')
    num_classes = len(label_values)
    class_pixels = np.zeros(num_classes)
    total_pixels = 0.0

    for n in range(len(image_files)):
        image = cv2.imread(image_files[n], -1)
        for index, colour in enumerate(label_values):
            class_map = np.all(np.equal(image, colour), axis=-1)
            class_map = class_map.astype(np.float32)
            class_pixels[index] += np.sum(class_map)

        print("\rProcessing image: " + str(n) + " / " + str(len(image_files)),
              end="")
        sys.stdout.flush()

    total_pixels = float(np.sum(class_pixels))
    index_to_delete = np.argwhere(class_pixels == 0.0)
    class_pixels = np.delete(class_pixels, index_to_delete)
    class_weights = total_pixels / class_pixels
    class_weights = class_weights / np.sum(class_weights)

    return class_weights
Exemplo n.º 2
0
def data_statistics():
    ''' Count (pixel-level) mean and variance of dataset. '''
    dir_list = [
        '/home/tao/Data/RBDD/train',
    ]  # list of data folder dir.

    mean = np.array([0, 0, 0], np.float32)  # BGR
    std = np.array([0, 0, 0], np.float32)  # BGR
    total_image = 0
    # Statisify mean and std
    for folder in dir_list:
        image_paths = filelist(folder, ifPath=True)
        for path in tqdm(image_paths):
            img = cv2.imread(path, cv2.IMREAD_COLOR)  # BGR
            img = np.float32(img) / 255.0
            # images.append(np.expand_dims(img))
            mean += np.mean(np.mean(img, 0), 0)
            total_image += 1
    mean /= total_image  # get pixel-level BGR mean
    # Statisify std
    # for folder in dir_list:
    #     image_paths = filelist(folder, ifPath=True)
    #     for path in tqdm(image_paths):
    #         img = cv2.imread(path, cv2.IMREAD_COLOR)  # BGR
    #         img = np.float32(img) / 255.0
    #         X = img-mean
    #         X = X*X
    #         std += np.sum(np.sum(X, axis=0), axis=0)
    # std /= total_image
    print('mean=', mean)
    # print('std=', std)
    print('Careful: BGR!')
Exemplo n.º 3
0
def convert_image_type(image_dir, new_type, old_type=None):
    '''Covert image's type(may be specify the old type).
    Args:
        new_type: The target type of image conversion(such as: 'png', 'jpg').
    '''
    image_names = filelist(image_dir, True, old_type)
    for name in tqdm(image_names):
        img = cv2.imread(name, 1)  # 默认BGR模式读,适应Tiff的标签图
        os.remove(name)
        name = os.path.splitext(name)[0] + '.' + new_type
        cv2.imwrite(name, img, [1, 100])
Exemplo n.º 4
0
def MorphologicPostTreate(input_dir, output_dir):
    '''
    形态学后处理
    '''
    image_names = filelist(input_dir)
    kernel = np.ones([7, 7], dtype=np.uint8)
    kernel[0, 0], kernel[-1, -1], kernel[0, -1], kernel[-1, 0] = 0, 0, 0, 0
    # print(kernel)

    for i in tqdm(range(len(image_names))):
        img = cv2.imread(input_dir + "/" + image_names[i], -1)
        img = bwmorph(img, kernel_1=kernel)  # 输出[HW]label
        img = np.repeat(np.expand_dims(img, axis=2), 3, axis=2)
        cv2.imwrite(output_dir + "/" + image_names[i], img)
Exemplo n.º 5
0
def label_pretreat(label_dir, label_values):
    '''
    Pre-treat the orignal RGB label, to fix the apparent bug.
    (Actually it eliminate the wrong colors that are not in class_dict)
    By the way, unify the dtype of label imgs to png.
    '''
    l_names = filelist(label_dir, ifPath=True)
    for name in tqdm(l_names):
        label = cv2.imread(name, 1)  # read in RGB
        os.remove(name)
        name = os.path.splitext(name)[0] + '.png'
        new_label = np.zeros(label.shape, label.dtype)
        # Fix the color(stand for a class) by class-info
        for color in label_values:
            equality = np.equal(label, color)
            ind_mat = np.all(equality, axis=-1)
            new_label[ind_mat] = color  # this color list can be customized
        cv2.imwrite(name, new_label)  # new_label-BGR(according to class_dict)
Exemplo n.º 6
0
def spilt_dataset(rate=0.025, max_num=None):
    ''' Spilt images(maybe with labels)'''
    from shutil import move
    # from sklearn.model_selection import train_test_split

    # root = '/home/tao/Data/cvpr_road/'
    in_floders = [
        '/home/tao/Data/cvpr_road/512/train',
        '/home/tao/Data/cvpr_road/512/train_labels'
    ]
    out_floders = [
        '/home/tao/Data/cvpr_road/512/val',
        '/home/tao/Data/cvpr_road/512/val_labels'
    ]
    name_list = []
    for d in in_floders:
        names = filelist(d)
        name_list.append(names)
    file_num = len(name_list[0])  # 文件总数
    max_num = file_num if max_num is None else max_num

    # 按比例抽取
    index = np.random.permutation(file_num)
    index = index[:int(file_num * rate)]

    for d, names in zip(range(len(in_floders)), name_list):
        print(in_floders[d])
        cnt = 0
        for i in index:
            # 针对文件的操作
            # os.remove(d+'/'+names[i])
            move(in_floders[d] + '/' + names[i],
                 out_floders[d] + '/' + names[i])
            cnt += 1
            if cnt >= max_num:
                break  # if set max_num, prioritize the max_num

    print('Finished!')