[853718]: / bm_dataset / generate_regist_pairs.py

Download this file

124 lines (98 with data), 4.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
Script for generating registration pairs in two schemas
Sample run::
python generate_regist_pairs.py \
-i "../output/synth_dataset/*.jpg" \
-l "../output/synth_dataset/*.csv" \
-csv ../output/cover.csv --mode each2all
python bm_dataset/generate_regist_pairs.py \
-i "$HOME/Medical-data/dataset_CIMA/lung-lesion_1/scale-100pc/*.png" \
-l "$HOME/Medical-data/dataset_CIMA/lung-lesion_1/scale-100pc/*.csv" \
-csv $HOME/Medical-data/dataset_CIMA/dataset_CIMA_100pc.csv --mode each2all
Copyright (C) 2016-2019 Jiri Borovec <jiri.borovec@fel.cvut.cz>
"""
import argparse
import glob
import logging
import os
import sys
import pandas as pd
sys.path += [os.path.abspath('.'), os.path.abspath('..')] # Add path to root
from birl.benchmark import ImRegBenchmark
from birl.utilities.data_io import image_sizes
from birl.utilities.experiments import parse_arg_params
# list of combination options
OPTIONS_COMBINE = ('first2all', 'each2all')
def arg_parse_params():
""" parse the input parameters
:return dict: parameters
"""
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--path_pattern_images', type=str, help='path to the input image', required=True)
parser.add_argument('-l', '--path_pattern_landmarks', type=str, help='path to the input landmarks', required=True)
parser.add_argument('-csv', '--path_csv', type=str, required=True, help='path to coordinate csv file')
parser.add_argument(
'--mode',
type=str,
required=False,
help='type of combination of registration pairs',
default=OPTIONS_COMBINE[0],
choices=OPTIONS_COMBINE
)
args = parse_arg_params(parser, upper_dirs=['path_csv'])
return args
def generate_pairs(path_pattern_imgs, path_pattern_lnds, mode):
""" generate the registration pairs as reference and moving images
:param str path_pattern_imgs: path to the images and image name pattern
:param str path_pattern_lnds: path to the landmarks and its name pattern
:param str mode: one of OPTIONS_COMBINE
:return: DF
"""
list_imgs = sorted(glob.glob(path_pattern_imgs))
list_lnds = sorted(glob.glob(path_pattern_lnds))
if len(list_imgs) != len(list_lnds):
raise RuntimeError(
'the list of loaded images (%i) and landmarks (%i) is different length' % (len(list_imgs), len(list_lnds))
)
if len(list_imgs) < 2:
raise RuntimeError('the minimum is 2 elements')
logging.info('combining list %i files with "%s"', len(list_imgs), mode)
pairs = [(0, i) for i in range(1, len(list_imgs))]
if mode == 'each2all':
pairs += [(i, j) for i in range(1, len(list_imgs)) for j in range(i + 1, len(list_imgs))]
reg_pairs = []
for i, j in pairs:
rec = dict(zip(ImRegBenchmark.COVER_COLUMNS, (list_imgs[i], list_imgs[j], list_lnds[i], list_lnds[j])))
img_size, img_diag = image_sizes(rec[ImRegBenchmark.COL_IMAGE_REF])
rec.update({
ImRegBenchmark.COL_IMAGE_SIZE: img_size,
ImRegBenchmark.COL_IMAGE_DIAGONAL: img_diag,
})
reg_pairs.append(rec)
df_overview = pd.DataFrame(reg_pairs)
return df_overview
def main(path_pattern_images, path_pattern_landmarks, path_csv, mode='all2all'):
""" main entry point
:param str path_pattern_images: path to images
:param str path_pattern_landmarks: path to landmarks
:param str path_csv: path output cover table, add new rows if it exists
:param str mode: option first2all or all2all
"""
# if the cover file exist continue in it, otherwise create new
if os.path.isfile(path_csv):
logging.info('loading existing csv file: %s', path_csv)
df_overview = pd.read_csv(path_csv, index_col=0)
else:
logging.info('creating new cover file')
df_overview = pd.DataFrame()
df_ = generate_pairs(path_pattern_images, path_pattern_landmarks, mode)
df_overview = pd.concat((df_overview, df_), axis=0) # , sort=True
df_overview = df_overview[list(ImRegBenchmark.COVER_COLUMNS_EXT)].reset_index(drop=True)
logging.info('saving csv file with %i records \n %s', len(df_overview), path_csv)
df_overview.to_csv(path_csv)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
arg_params = arg_parse_params()
logging.info('running...')
main(**arg_params)
logging.info('DONE')