]> AND Private Git Repository - myo-class.git/blobdiff - topng.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
stable, day after meeting
[myo-class.git] / topng.py
index fde569ba6509bdf19efb0a28c282d4ec4b8b1dfb..672fd6d9a4dd66bfc33018d1c28981ad94b13d11 100644 (file)
--- a/topng.py
+++ b/topng.py
@@ -27,6 +27,7 @@ INPUT_DIR = '../../Data/Images_anonymous/Case_0002/'
 OUT_DIR = './generated/'
 
 CROP_SIZE = (45, 45) # (width, height)
+EPI_MIN_PIXELS = 30 * 30 # nombre pixels minimal pour considérer un epicardic, else on ignore, 30 * 30, vu que c'est carré
 RED_COLOR = 100
 
 def roundall(*l):
@@ -108,6 +109,43 @@ def getxy(file):
                return np.array(tmp, dtype=np.int32)
                # return tmp
 
+def getimgname(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 'Image00001' or return ''
+       """
+       imgXXX = None
+       # print("file", file)
+       with open(file) as jsonfile:
+               data = json.load(jsonfile)
+               if len(data) > 1:
+                       print("more than one image in same json")
+               # print("data")
+               # pprint(data)
+               for imgXXX in data: pass  # get the value of key ~= "Image00001", cause it's dynamic
+       return imgXXX
+
+
 def minmax(file):
        r = getxy(file)
        if r is not None:
@@ -285,7 +323,7 @@ def readpng(inputfile):# just a specific func to preview a "shape = (X,Y,3)" ima
                for i, row in enumerate(tab):
                        print(row[0]*65536 + row[0]*256 + row[0], end=" " if i % image.shape[0] != 0 else "\n")
 
-def topng(inputfile, outfile=None, overwrite=True, verbose=False, epimask='', endomask='', centercrop=None, blackmask=False, square=False, redcontour=False):
+def topng(inputfile, outfile=None, overwrite=True, verbose=False, epimask='', endomask='', centercrop=None, blackmask=False, square=False, redcontour=False, epiminpixels=-1):
        """
                (verbose) return (64, 64) : the width and height
                (not verbose) return (img, dicimg) : the image and the dicimg objects
@@ -348,9 +386,18 @@ def topng(inputfile, outfile=None, overwrite=True, verbose=False, epimask='', en
                                if endomask:
                                        img = hollowmask(img, epimask, endomask)
                                else:
+                                       # ignore small epicardic --<<
+                                       xmin, ymin, xmax, ymax = minmax(epimask)
+                                       pixels = (xmax - xmin) * (ymax - ymin)
+                                       if pixels <= epiminpixels:
+                                               print( "small epicardic ({}), ignored!".format(inputfile.split('/')[-1]) )
+                                               return
+                                       # ignore small epicardic -->>
+
                                        img = mask(img, epimask)
 
                if centercrop:
+
                        img = crop(img, epimask, centercrop)
 
 
@@ -376,6 +423,7 @@ def topng(inputfile, outfile=None, overwrite=True, verbose=False, epimask='', en
        # np.savetxt(savepath + '.npy', img)
        # test >>
 
+
        if np.count_nonzero(img) > 0: # matrix not full of zero
                cv2.imwrite(savepath, img, [cv2.IMWRITE_PNG_COMPRESSION, 0]) # write png image