def true_positif(image,mask,threshold,threshold_merge = 0.172,method = 'crop',slices = 100): '''Input: an image, the corresponding mask, a method, a threshold and the number of slices. Ouput : the number of voxels that are in our bounding box and in the perfect box.''' if(method == 'crop'): [left,right,top,bottom] = get_coordinates(image,threshold,slices) elif(method == 'merge'): image = splitandmerge(image,threshold_merge,method='Exponent') [left,right,top,bottom] = get_coordinates(image,threshold,slices) else: raise Exception('Unknown method') [p_left,p_right,p_top,p_bottom] = perfect_box_coord(mask) tp_top = 0 tp_bottom = 0 tp_left = 0 tp_right = 0 total_area = (bottom-top)*(left-right) if(p_left>left and p_left<right): tp_left = p_left elif(p_left<left and left<p_right): tp_left = left if(p_right<right and p_right>left): tp_right = p_right elif(p_right>right and right>p_left): tp_right = right if(p_top>top and p_top<bottom): tp_top = p_top elif(p_top<top and top<p_bottom): tp_top = top if(p_bottom<bottom and p_bottom>top): tp_bottom = p_bottom elif(p_bottom>bottom and bottom>p_top): tp_bottom = bottom return (tp_right-tp_left)*(tp_bottom-tp_top)
def percentage_cropped(image,threshold,threshold_merge = 0.172,method = 'crop',slices = 100): '''Input: an image, a threshold, a method and a number of slices. Ouput: the percentage of the image that has been cropped.''' if(method == 'crop'): [left,right,top,bottom] = get_coordinates(image,threshold,slices) elif(method == 'merge'): image = splitandmerge(image,threshold_merge,method='Exponent') [left,right,top,bottom] = get_coordinates(image,threshold,slices) else: raise Exception('Unknown method') (rows,cols) = (image.shape[0],image.shape[1]) (box_rows,box_cols) = (bottom-top,right-left) return (1-((box_rows*box_cols)/(rows*cols)))*100
def true_negatif(image,mask,threshold,threshold_merge = 0.172,method = 'crop',slices = 100): '''Input: an image, the corresponding mask, a method, a threshold and the number of slices. Ouput : the number of voxels that are in our bounding box but shouldnt.''' tp = true_positif(image,mask,threshold,threshold_merge = threshold_merge,method = method,slices =slices) [left,right,top,bottom] = get_coordinates(image,threshold,slices) total_bb = (right-left)*(bottom-top) return total_bb-tp
def false_positif(image,mask,threshold,threshold_merge = 0.172,method = 'crop',slices = 100): '''Input: an image, the corresponding mask, a method, a threshold and the number of slices. Ouput : the number of voxels that are not in our bounding box and shouldnt be.''' tp = true_positif(image,mask,threshold,threshold_merge = threshold_merge,method = method,slices =slices) total = total_voxels(image) [left,right,top,bottom] = get_coordinates(image,threshold,slices) total_bb = (right-left)*(bottom-top) [p_left,p_right,p_top,p_bottom] = perfect_box_coord(mask) total_pb = (p_right-p_left)*(p_bottom-p_top) return total-total_bb-total_pb+tp
def percentage_outside_box_crop(image,mask,threshold,threshold_merge = 0.172,method = 'crop',slices = 100): '''Input: an image, the corresponding mask, a method, a threshold and the number of slices. Ouput: percentage of the organs that our outside the box.''' if(method == 'crop'): [left,right,top,bottom] = get_coordinates(image,threshold,slices) elif(method == 'merge'): image = splitandmerge(image,threshold_merge,method='Exponent') [left,right,top,bottom] = get_coordinates(image,threshold,slices) else: raise Exception('Unknown method') [p_left,p_right,p_top,p_bottom] = perfect_box_coord(mask) h_top = 0 h_bottom = 0 w_left = 0 w_right = 0 total_area = (bottom-top)*(left-right) if(p_left<left): w_left = left-p_left if(p_right>right): w_right = p_right-right if(p_top<top): h_top = top-p_top if(p_bottom>bottom): h_bottom = p_bottom-bottom h = h_top+h_bottom w = w_left+w_right if(h == 0 and w == 0): return 0 elif(h == 0): h = bottom-top elif(w == 0): w = left-right return ((h*w)/total_area)*100
bm2[:, :, s, :], col1, col2, x1, x0, y0, y1, width, height, color) line = (s // 16) * 192 col = (s % 16) * 192 im[line:(line + 192), col:(col + 192), :] = output im = ((im - np.min(im)) * 255 / (np.max(im) - np.min(im))).astype(np.uint8) frame1 = plt.gca() frame1.axes.get_xaxis().set_visible(False) frame1.axes.get_yaxis().set_visible(False) plt.imshow(im) print(info) plt.show() if filename != None: plt.imsave(filename, im, dpi=1000) plt.close() image = np.load('data/merged_image.npy') # MERGE mask = np.load('data/mask.npy') mask = mask[:, :, :, :-1] pred = np.zeros((192, 192, 160, 2)) col1 = np.array([[255, 0, 0], [0, 128, 0], [0, 0, 255]]) col2 = np.array([[255, 0, 255], [0, 255, 0], [0, 255, 255]]) [y0, y1, x0, x1] = get_coordinates(image, get_threshold(), 100) info = 'patient 0' filename = None blue = np.array([0, 0, 255]) show_slices_gen(image, mask, pred, col1, col2, info, filename, y0, y1, x0, x1, blue)
col = (s%16)*cols im[line:(line+rows),col:(col+cols),:] = output im = ((im - np.min(im)) * 255 / (np.max(im) - np.min(im))).astype(np.uint8) frame1 = plt.gca() frame1.axes.get_xaxis().set_visible(False) frame1.axes.get_yaxis().set_visible(False) plt.imshow(im) print(info) plt.show() if filename != None: plt.imsave(filename, im, dpi=1000) plt.close() image = np.load('../show_images/data/CHA-CBCT-2-image.npy') [y0,y1,x0,x1] = get_coordinates(image,0.1,100) info = 'patient 0' filename = None (rows,cols) = np.shape(image[:,:,0]) red = np.array([255,0,0]) show_slices_gen(image,rows,cols, info, filename,y0,y1,x0,x1,red)