X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/myo-class.git/blobdiff_plain/c67ab60de95008178e26817946990382751d91d8..1d3fb87f139a53b55ceb52f3dc2db49ac2b0a0ac:/topng.py?ds=sidebyside diff --git a/topng.py b/topng.py index 9651446..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 @@ -40,15 +68,19 @@ def getdir(filepath): return '/'.join(filepath.split('/')[:-1]) + '/' def topng(inputfile, outfile=None, overwrite=True): + """ + return (64, 64) : the width and height + """ try: dicimg = pydicom.read_file(inputfile) # read dicom image 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 << - print('img', img) + # return img.shape # $$ COMMENT OR REMOVE THIS LINE + # print('img', img) # print('img', type(img)) # print('min', img.min(), 'max', img.max()) # dicimg.convert_pixel_data() # same as using dicimg.pixel_array @@ -58,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) ) @@ -82,6 +114,8 @@ def topng(inputfile, outfile=None, overwrite=True): cv2.imwrite(savepath, img, [cv2.IMWRITE_PNG_COMPRESSION, 0]) # write png image + # test + return img.shape def topngs(inputdir, outdir): """ @@ -93,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')