X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/myo-class.git/blobdiff_plain/c67ab60de95008178e26817946990382751d91d8..eeb9b42c3779bbb098c07e2dd91d40ecf6e63372:/helpers.py diff --git a/helpers.py b/helpers.py index 9784900..808a4a5 100644 --- a/helpers.py +++ b/helpers.py @@ -1,2 +1,106 @@ +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 + def getdir(filepath): - return '/'.join(filepath.split('/')[:-1]) + '/' + return '/'.join(filepath.split('/')[:-1]) + '/' + + +if __name__ == '__main__': + # vertices = np.array(list(reversed([ + # (5,12), + # (8,18), + # (13,14), + # (11,6), + # ]) )) + + vertices = np.array([ + [ 97, 129], + [103, 134], + [108, 139], + [109, 146], + [109, 155], + [105, 161], + [ 99, 165], + [ 93, 166], + [ 83, 164], + [ 78, 161], + [ 73, 153], + [ 72, 147], + [ 72, 140], + [ 75, 133], + [ 79, 129], + [ 85, 127], + [ 93, 127], + ]) + + from topng import topng + 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") + # print(Mat) + # Mat = np.ones((20, 25)).astype(np.int32) + # Mat[:,:] = 14 + + # print( timeit(lambda:create_polygon([20,20], vertices), number=100000) ) + polygon_array = drawcontours(Mat, vertices) + print(polygon_array.shape) + # print(polygon_array.astype(np.int32))