Пример #1
0
def show_images_and_masks(imagesdir,
                          masksdir,
                          suffix='_mask',
                          show=False,
                          save=False):
    all_images = get_subfiles(imagesdir)
    for imagefilename in all_images:
        filename = imagefilename.split('.')[0]
        maskfilename = str(filename) + suffix + '.png'

        img = cv2.imread(imagesdir + '/' + imagefilename)
        mask = cv2.imread(masksdir + '/' + maskfilename)

        if os.getenv('COLORMAP_FLAG').upper() == 'TRUE':
            mask_modified = apply_color_map(mask)
        else:
            mask_modified = get_binary_image(mask)

        combined_img = np.concatenate((img, mask_modified), axis=1)

        if show:
            cv2.imshow("annotations - Filename: {}".format(filename),
                       combined_img)
            cv2.waitKey()
            cv2.destroyAllWindows()

        if save:
            cv2.imwrite('results/combined/{}'.format(str(filename) + '.png'),
                        combined_img)
Пример #2
0
def save_images_and_masks(imagesdir, masksdir, suffix='_predict', show=False,
                          save=False):
    all_mask_images = get_subfiles(masksdir)
    counter = 1
    for maskfilename in all_mask_images:
        image_id = maskfilename.split('.')[0].replace(suffix, '')
        imagefilename = image_id + '.jpg'

        img = cv2.imread(imagesdir + '/' + imagefilename)
        mask = cv2.imread(masksdir + '/' + maskfilename)

        if os.getenv('COLORMAP_FLAG').upper() == 'TRUE':
            mask_modified = apply_color_map(mask)
        else:
            mask_modified = get_binary_image(mask)

        combined_img = np.concatenate((img, mask_modified), axis=1)

        if show:
            cv2.imshow("annotations - Filename: {}".format(image_id), combined_img)
            cv2.waitKey()
            cv2.destroyAllWindows()

        if save:
            print('\ncounter: {}'.format(counter))
            print('results/combined/{}'.format(str(image_id) + '.png'))
            cv2.imwrite(os.path.join(os.getenv('COMBINED_IMAGES_PATH'), (str(image_id) + '.png')), combined_img)
            counter += 1
Пример #3
0
def test_generator(test_path, target_size=(256, 256), flag_multi_class=False, as_gray=True):
    test_images_filename = get_subfiles(test_path)
    for i in range(len(test_images_filename)):
        imgrgb = io.imread(os.path.join(test_path, test_images_filename[i]))
        if as_gray:
            img = rgb2gray(imgrgb)
        img = trans.resize(img, target_size)
        img = np.reshape(img, img.shape+(1,)) if (not flag_multi_class) else img
        img = np.reshape(img, (1,)+img.shape)
        yield img
Пример #4
0
def train_valid_test_split(master_data_path,
                           train_path,
                           valid_path,
                           test_path,
                           percent_valid=0.2,
                           percent_test=0.2):
    # distribute files from master to train, valid and test
    all_data_filenames = get_subfiles(os.path.join(master_data_path, 'images'))
    valid_filenames = random.sample(
        all_data_filenames,
        int((percent_valid / 100.0) * len(all_data_filenames)))
    test_filenames = random.sample(
        valid_filenames, int((percent_test / 100.0) * len(valid_filenames)))
    train_filenames = [
        x for x in all_data_filenames if x not in valid_filenames
    ]
    valid_filenames = [x for x in valid_filenames if x not in test_filenames]

    # create directories
    create_directory(train_path)
    create_directory(os.path.join(train_path, 'images'))
    create_directory(os.path.join(train_path, 'masks'))
    create_directory(test_path)
    create_directory(os.path.join(test_path, 'png_images'))
    create_directory(os.path.join(test_path, 'tiff_images'))
    create_directory(valid_path)
    create_directory(os.path.join(valid_path, 'images'))
    create_directory(os.path.join(valid_path, 'masks'))

    # copy train files
    for file in train_filenames:
        copyfile(os.path.join(os.path.join(master_data_path, 'images'), file),
                 os.path.join(os.path.join(train_path, 'images'), file))
        mask_filename = file.split('.')[0] + '_mask.png'
        copyfile(
            os.path.join(os.path.join(master_data_path, 'masks'),
                         mask_filename),
            os.path.join(os.path.join(train_path, 'masks'), mask_filename))
    print('\nTrain files copied successfully ...')

    # copy validation files
    for file in valid_filenames:
        copyfile(os.path.join(os.path.join(master_data_path, 'images'), file),
                 os.path.join(os.path.join(valid_path, 'images'), file))
        mask_filename = file.split('.')[0] + '_mask.png'
        copyfile(
            os.path.join(os.path.join(master_data_path, 'masks'),
                         mask_filename),
            os.path.join(os.path.join(valid_path, 'masks'), mask_filename))
    print('\nValidation files copied successfully ...')

    # copy test files
    for file in test_filenames:
        copyfile(os.path.join(os.path.join(master_data_path, 'images'), file),
                 os.path.join(os.path.join(test_path, 'png_images'), file))
        copyfile(
            os.path.join(cropped_tiff_images_path,
                         file.split('.')[0] + '.tif'),
            os.path.join(os.path.join(test_path, 'tiff_images'),
                         file.split('.')[0] + '.tif'))
    print('\nTest files copied successfully ...')