+from timeit import timeit
+import numpy as np
+
+
+INPUT_DIR = '../../Data/Images_anonymous/Case_0002/'
+OUT_DIR = './generated/'
+
+
+np.set_printoptions(edgeitems=272, linewidth=500)# hey numpy, take advantage of my large screen
+
+def check(p1, p2, Mat):
+ """
+ Not by @res
+ Uses the line defined by p1 and p2 to check array of
+ input indices against interpolated value
+
+ Returns boolean array, with True inside and False outside of shape
+ """
+ idxs = np.indices(Mat.shape) # Create 3D array of indices
+
+ try:
+ p1 = p1.astype(float)
+ p2 = p2.astype(float)
+
+ # Calculate max column idx for each row idx based on interpolated line between two points
+ max_col_idx = (idxs[0] - p1[0]) / (p2[0] - p1[0]) * (p2[1] - p1[1]) + p1[1]
+ sign = np.sign(p2[0] - p1[0])
+ return idxs[1] * sign <= max_col_idx * sign
+ except RuntimeWarning as e:
+ print("p1, p2")
+ print(p1, p2)
+ return True
+
+def drawcontours(Mat, vertices):
+ """
+ Not by @res
+
+ Mat.shape : (20,20) or something like..
+ vertices : np.array([
+ [5,12],
+ [8,18],
+ [13,14],
+ [11,6],
+ [4,6],
+ ])
+
+ return Mat : the resulting matrix
+
+ Creates np.array with dimensions defined by shape
+ Fills polygon defined by vertices with ones, all other values zero"""
+
+ fill = np.ones(Mat.shape) # Initialize boolean array defining shape fill
+
+ # Create check array for each edge segment, combine into fill array
+ for k in range(vertices.shape[0]): # .shape[0] is the number of vertices, like len(vertices)
+ fill = np.all([fill, check(vertices[k-1], vertices[k], Mat)], axis=0)
+
+ # Set all values outside polygon to zero
+ # print("fill")
+ # print(fill)
+ Mat[np.invert(fill)] = 0
+
+ return Mat
+