From: Bernardo TOD Date: Thu, 23 May 2019 22:57:29 +0000 (+0200) Subject: trim image, remove bad black border X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/myo-class.git/commitdiff_plain/1d3fb87f139a53b55ceb52f3dc2db49ac2b0a0ac trim image, remove bad black border --- diff --git a/topng.py b/topng.py index c9b72d9..0000a58 100644 --- a/topng.py +++ b/topng.py @@ -12,10 +12,38 @@ from decimal import Decimal as d, ROUND_HALF_UP as rhu PNG16_MAX = pow(2, 16) - 1 # here if thinks the heaviest weight bit is for transparency or something not in use with dicom imgs PNG8_MAX = pow(2, 8+1) - 1 # heaviest weight bit is 8 => 2**8, but dont forget the others: the reason of +1 -INPUT_DIR = '../../Data/Images_anonymous/Case_0449/' +INPUT_DIR = '../../Data/Images_anonymous/Case_0350/' OUT_DIR = './generated/' #os.mkdir(outdir) +def ftrim(Mat): + # private func to trim the Matrix, but in one direction, vertically | horizontally + # return the slice, don't affect the Matrix + # y | : for column, x -> : for rows + # v + # not my fault, numpy architecture + y1 = y2 = i = 0 + while i < len(Mat):# search by top + if max(Mat[i]) > 0: + y1 = i + break + i += 1 + i = len(Mat) - 1 + while i >= 0:# search by bottom + if max(Mat[i]) > 0: + y2 = i + break + i -= 1 + # print('y1, y2', y1, y2) + return slice(y1, y2+1)# +1 to stop at y2 + +def trim(Mat): + # most use who make implicit call of ftrim twice + horizontal = ftrim(Mat) + vertical = ftrim(Mat.T) + # print('horizontal:vertical', horizontal, vertical) + return Mat[horizontal, vertical] + def map16(array):# can be useful in future return array * 16 @@ -48,11 +76,11 @@ def topng(inputfile, outfile=None, overwrite=True): except pydicom.errors.InvalidDicomError as e: # @TODO: log, i can't read this file return - img = dicimg.pixel_array# get image array (12bits) + img = trim(dicimg.pixel_array)# get image array (12bits) # test << # return img.shape # $$ COMMENT OR REMOVE THIS LINE - print('img', img) + # print('img', img) # print('img', type(img)) # print('min', img.min(), 'max', img.max()) # dicimg.convert_pixel_data() # same as using dicimg.pixel_array @@ -62,10 +90,10 @@ def topng(inputfile, outfile=None, overwrite=True): # test >> # affine transfo to png 16 bits, func affects img variable - maxdepth = img.max() - # print('maxdepth, PNG8_MAX', maxdepth, PNG8_MAX) # testing.. + maxdepth = pow(2, dicimg.BitsStored) - 1 + print('dicimg.BitsStored, PNG8_MAX', dicimg.BitsStored, PNG8_MAX) # testing.. affine(img, - (img.min(), maxdepth), + (0, maxdepth), # img.min() replace 0 may be, but not sure it would be good choice (0, PNG16_MAX if maxdepth > PNG8_MAX else PNG8_MAX) ) @@ -99,5 +127,5 @@ def topngs(inputdir, outdir): topng( inputdir + f, join(outdir, f) ) if __name__ == '__main__': - # topngs( INPUT_DIR, join(OUT_DIR, INPUT_DIR.split('/')[-2]) ) - topng(INPUT_DIR+'Image00001', OUT_DIR + INPUT_DIR.split('/')[-2] +'-Image00001') + topngs( INPUT_DIR, join(OUT_DIR, INPUT_DIR.split('/')[-2]) ) + # topng(INPUT_DIR+'Image00001', OUT_DIR + INPUT_DIR.split('/')[-2] +'-Image00001') diff --git a/totraindir.py b/totraindir.py index 0fead80..8d90c14 100644 --- a/totraindir.py +++ b/totraindir.py @@ -9,7 +9,7 @@ from regularjson import search, RT_PATH, JSON_GTS, INFA_STR # constants GLOB_DIR = '../../Data/Images_anonymous/' -OUT_DIR = './train/' +OUT_DIR = './generated/train/' INDICE_CASE = 'Case' START = None # to manage the number of Patient Case to use in training + validation, if START == None => 0, if END == None => last index, it will use all in GLOB_DIR