- if 0 < len(l2) < len(l1):
- # TODO: log, json doesn't match with images (labels != features)
- print("WARN", "json doesn't match with images (labels != features), Case", cas)
- continue
- pass
-
- for i, dic in enumerate(l1):
- # print('join', r, l2[i])
- ref = join(r, l2[i]) # logically, should be the json ref of i dicom image
-
- infarctus = search(ref, INFA_STR)
- # epimask = mask(ref, EPI_STR)
- # print("infarctus:", infarctus) # Testing..
- # topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic)
- w, h = topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic))
-
- # search maximums
- if wmax < w: wmax = w
- if hmax < h: hmax = h
-
- # search width minimum
- if wmin is None: wmin = w
- elif wmin > w: wmin = w
-
- # search height minimum
- if hmin is None: hmin = h
- elif hmin > h: hmin = h
-
- print('min-width, max-width:', (wmin, wmax))
- print('min-height, max-height:', (hmin, hmax))
-
- print('Ended!')
+ # if 0 < len(l2) < len(l1):
+ # # TODO: log, json doesn't match with images (labels != features)
+ # print("WARN", "json doesn't match with images (labels != features), Case", cas)
+ # continue
+ # pass
+
+ for i, dic in enumerate(l1): # l1 => ["image001", "image002"], l2 => ["37", "42"]
+ if True:
+ if "{}-{}".format(cas, dic) in BLACK_LIST_IMAGES: # if image was black listed
+ continue
+
+ epimask = ''
+ infarctus = ''
+ endomask = ''
+ # debug <<
+ # infarctus = True
+ # debug >>
+ # search the right json, for that, we open the json
+ for elt in l2:
+ # print('join', r, l2[i])
+ ref = join(r, elt) # logically, should be the json ref's "parent directory" of i dicom image
+
+ t_epimask = search(ref, EPI_STR)
+
+ parent = dirname(ref) # get the parent dir
+
+ infarctus = search(parent, INFA_STR)
+ endomask = search(parent, ENDO_STR)
+
+ if t_epimask and t_epimask.get("path"):
+ timgname = getimgname(t_epimask["path"])
+
+ if timgname == dic:
+ epimask = t_epimask
+ break
+ else:
+ print('no epicardic')
+
+
+ # print("ref left:", l1[i]) # Testing..
+ # print("ref right:", ref) # Testing..
+ # print("infarctus:", infarctus) # Testing..
+ # topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic)
+ # print('epimask', epimask)
+
+ # debug <<
+ # epimask = False
+ # debug >>
+
+ if epimask:# this condition could be if necessary
+ if infarctus and (END_INF_CASES is not None) and j > END_INF_CASES:
+ continue
+ if not infarctus and (END_NOINF_CASES is not None) and j > END_NOINF_CASES:
+ continue
+ # topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic), epimask=epimask['path'] if epimask else None)
+ # img_, dicimg_, minmax_ = topng(join(caspath, dic),
+ # '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic),
+ # epimask=epimask['path'] if epimask else None,
+ # verbose=True,
+ # )
+
+ # Possibly data augmentation purpose here
+ print("EPI_MIN_PIXELS", EPI_MIN_PIXELS)
+ topng(
+ join(caspath, dic),
+ outfile='{}/crop-mask/{}-{}'.format(join(OUT_DIR,
+ 'infarctus' if infarctus else 'noinfarctus'),
+ cas,
+ dic,
+ ),
+ epimask=epimask['path'] if epimask else None,
+ centercrop=CROP_SIZE,
+ blackmask=True,
+ epiminpixels=EPI_MIN_PIXELS
+ ) # crop and mask with black
+
+ # if endomask:
+ # topng(
+ # join(caspath, dic),
+ # outfile='{}/crop-mask-hollow/{}-{}'.format(join(OUT_DIR,
+ # 'infarctus' if infarctus else 'noinfarctus'),
+ # cas,
+ # dic,
+ # ),
+ # epimask=epimask['path'] if epimask else None,
+ # endomask=endomask['path'] if endomask else None,
+ # centercrop=CROP_SIZE,
+ # blackmask=True,
+ # ) # crop and (mask and fill hollow) with black
+
+ # topng(
+ # join(caspath, dic),
+ # outfile='{}/crop-nomask/{}-{}'.format(join(OUT_DIR,
+ # 'infarctus' if infarctus else 'noinfarctus'),
+ # cas,
+ # dic,
+ # ),
+ # epimask=epimask['path'] if epimask else None,
+ # centercrop=CROP_SIZE,
+ # ) # just crop, don't apply black mask
+
+ # w, h = BE CAREFULL, be sure w, h is needed, and that topng returns something
+ # topng(
+ # join(caspath, dic),
+ # outfile='{}/contour/{}-{}'.format(join(OUT_DIR,
+ # 'infarctus' if infarctus else 'noinfarctus'),
+ # cas,
+ # dic,
+ # ),
+ # epimask=epimask['path'] if epimask else None,
+ # redcontour=True,
+ # ) # draw a red contour for visibily purpose
+
+ # segmentation width & height <<
+ # xmin, ymin, xmax, ymax = minmax_
+ # w = xmax - xmin + 1 # +1 : even the position takes a bit
+ # h = ymax - ymin + 1
+ # segmentation width & height >>
+
+
+ # # search maximums
+ # if wmax < w: wmax = w
+ # if hmax < h: hmax = h
+
+ # # search width minimum
+ # if wmin is None: wmin = w
+ # elif wmin > w: wmin = w
+
+ # # search height minimum
+ # if hmin is None: hmin = h
+ # elif hmin > h: hmin = h
+ # except Exception as e:
+ # print("WARN", "something went wrong with this image", dic, str(e))
+ # continue
+
+
+ # print('min-width, max-width:', (wmin, wmax))
+ # print('min-height, max-height:', (hmin, hmax))
+
+ # print('Ended!')