+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)
+