1 from timeit import timeit
5 INPUT_DIR = '../../Data/Images_anonymous/Case_0002/'
6 OUT_DIR = './generated/'
9 np.set_printoptions(edgeitems=272, linewidth=500)# hey numpy, take advantage of my large screen
11 def check(p1, p2, Mat):
14 Uses the line defined by p1 and p2 to check array of
15 input indices against interpolated value
17 Returns boolean array, with True inside and False outside of shape
19 idxs = np.indices(Mat.shape) # Create 3D array of indices
25 # Calculate max column idx for each row idx based on interpolated line between two points
26 max_col_idx = (idxs[0] - p1[0]) / (p2[0] - p1[0]) * (p2[1] - p1[1]) + p1[1]
27 sign = np.sign(p2[0] - p1[0])
28 return idxs[1] * sign <= max_col_idx * sign
29 except RuntimeWarning as e:
34 def drawcontours(Mat, vertices):
38 Mat.shape : (20,20) or something like..
47 return Mat : the resulting matrix
49 Creates np.array with dimensions defined by shape
50 Fills polygon defined by vertices with ones, all other values zero"""
52 fill = np.ones(Mat.shape) # Initialize boolean array defining shape fill
54 # Create check array for each edge segment, combine into fill array
55 for k in range(vertices.shape[0]): # .shape[0] is the number of vertices, like len(vertices)
56 fill = np.all([fill, check(vertices[k-1], vertices[k], Mat)], axis=0)
58 # Set all values outside polygon to zero
61 Mat[np.invert(fill)] = 0
66 return '/'.join(filepath.split('/')[:-1]) + '/'
69 if __name__ == '__main__':
70 # vertices = np.array(list(reversed([
97 from topng import topng
98 Mat = topng(INPUT_DIR+'Image00003', OUT_DIR + INPUT_DIR.split('/')[-2] +'-Image00003', epimask="/Users/user/Desktop/Master/Stage/Data/json/json_GT/0_Case_0002/contours/1.2.3.4.5.6/31/-85.9968/Epicardic.json")
100 # Mat = np.ones((20, 25)).astype(np.int32)
103 # print( timeit(lambda:create_polygon([20,20], vertices), number=100000) )
104 polygon_array = drawcontours(Mat, vertices)
105 print(polygon_array.shape)
106 # print(polygon_array.astype(np.int32))