X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/myo-class.git/blobdiff_plain/1d3fb87f139a53b55ceb52f3dc2db49ac2b0a0ac..d983932e5a974928a10bbab7e86fdf3e162ae2ae:/topng.py diff --git a/topng.py b/topng.py index 0000a58..8722d98 100644 --- a/topng.py +++ b/topng.py @@ -6,16 +6,20 @@ import pydicom from pprint import pprint import numpy as np import pathlib +import json 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_0350/' +INPUT_DIR = '../../Data/Images_anonymous/Case_0002/' OUT_DIR = './generated/' #os.mkdir(outdir) +def roundall(*l): + return (round(e) for e in l) + def ftrim(Mat): # private func to trim the Matrix, but in one direction, vertically | horizontally # return the slice, don't affect the Matrix @@ -24,13 +28,13 @@ def ftrim(Mat): # not my fault, numpy architecture y1 = y2 = i = 0 while i < len(Mat):# search by top - if max(Mat[i]) > 0: + if Mat[i].max() > 0: y1 = i break i += 1 i = len(Mat) - 1 while i >= 0:# search by bottom - if max(Mat[i]) > 0: + if Mat[i].max() > 0: y2 = i break i -= 1 @@ -44,6 +48,80 @@ def trim(Mat): # print('horizontal:vertical', horizontal, vertical) return Mat[horizontal, vertical] +def mask(Mat, maskfile): + # return + xmin, ymin, xmax, ymax = minmax(Mat, maskfile) + + y1 = y2 = i = 0 + while i < len(Mat):# search by top + if i < ymin: + Mat[i].fill(0)# paint the row in black + else: break + i += 1 + + i = len(Mat) - 1 + while i >= 0:# search by bottom + if i > ymax: + Mat[i].fill(0)# paint the row in black + else: break + i -= 1 + # print('y1, y2', y1, y2) + # return slice(y1, y2+1)# +1 to stop at y2 + + + +def minmax(Mat, file): + """ + { + 'Image00001': [{ + 'color': '#ff0000', + 'points': [ + {'x': 94.377, 'y': 137.39}, + {'x': 100.38, 'y': 139.55}, + {'x': 103.26, 'y': 142.67}, + {'x': 105.91, 'y': 147.95}, + {'x': 105.42, 'y': 152.76}, + {'x': 100.62, 'y': 156.84}, + {'x': 95.338, 'y': 159.96}, + {'x': 89.573, 'y': 158.52}, + {'x': 84.53, 'y': 153}, + {'x': 82.848, 'y': 149.15}, + {'x': 82.368, 'y': 142.91}, + {'x': 85.01, 'y': 138.11}, + {'x': 89.813, 'y': 137.39}, + {'x': 94.377, 'y': 137.39} + ] + }] + } + return xmin, ymin, xmax, ymax + """ + with open(file) as jsonfile: + data = json.load(jsonfile) + pprint(data) + + for imgXXX in data:pass # get the value of key ~= "Image00001", cause it's dynamic + + for obj in data[imgXXX]:pass # get the object that contains the points, cause it's a list + points = obj['points'] + + + # print("print, ", data) + # print("imgXXX, ", imgXXX) + # print("points, ", points) + # print("imgXXX, ", obj['points']) + tmp = [(pt['x'], pt['y']) for pt in points ] # extract x,y. {'x': 94.377, 'y': 137.39} => (94.377, 137.39) + r = np.array(tmp) + + print(r) # log + + xmin, ymin = np.min(r, axis=0) + xmax, ymax = np.max(r, axis=0) + + print('xmax, ymax', xmax, ymax) + print('xmin, ymin', xmin, ymin) + + return roundall(xmin, ymin, xmax, ymax) + def map16(array):# can be useful in future return array * 16 @@ -67,9 +145,10 @@ def affine(Mat, ab, cd): def getdir(filepath): return '/'.join(filepath.split('/')[:-1]) + '/' -def topng(inputfile, outfile=None, overwrite=True): +def topng(inputfile, outfile=None, overwrite=True, verbose=False): """ - return (64, 64) : the width and height + (verbose) return (64, 64) : the width and height + (not verbose) return (img, dicimg) : the image and the dicimg objects """ try: dicimg = pydicom.read_file(inputfile) # read dicom image @@ -91,12 +170,15 @@ def topng(inputfile, outfile=None, overwrite=True): # affine transfo to png 16 bits, func affects img variable maxdepth = pow(2, dicimg.BitsStored) - 1 - print('dicimg.BitsStored, PNG8_MAX', dicimg.BitsStored, PNG8_MAX) # testing.. + print('dicimg.BitsStored, (img.min(), img.max())', dicimg.BitsStored, (img.min(), img.max())) # testing.. affine(img, (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) ) + mask(img, '/Users/user/Desktop/Master/Stage/Data/json/json_GT/0_Case_0002/contours/1.2.3.4.5.6/31/-85.9968/Epicardic.json') + + savepath = (outfile or inputfile) + '.png' savedir = getdir(savepath) if overwrite and not isdir( savedir ): @@ -115,7 +197,10 @@ def topng(inputfile, outfile=None, overwrite=True): cv2.imwrite(savepath, img, [cv2.IMWRITE_PNG_COMPRESSION, 0]) # write png image # test - return img.shape + if verbose: + return img, dicimg + else: + return img.shape def topngs(inputdir, outdir): """ @@ -127,5 +212,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+'Image00003', OUT_DIR + INPUT_DIR.split('/')[-2] +'-Image00003')