def mean_shift_selection( votes, n_points=5000, bandwidth=10, n=10, cutoff=None, debug=False, debug_output="./" ): points = np.argwhere(votes) probas = votes[points[:,0], points[:,1], points[:,2]] points, probas = random_pick(points,probas,n_points) ms = MeanShift(bandwidth=bandwidth) ms.fit(points) weights = np.zeros( ms.cluster_centers_.shape[0], dtype='float' ) for i,c in enumerate(ms.cluster_centers_): weights[i] = np.sum(probas[ms.labels_==i]) detection = ms.cluster_centers_[np.argsort(weights)[::-1]] #points = points[np.argsort(weights)[::-1]] #ms.labels_ = ms.labels_[np.argsort(weights)[::-1]] weights = np.sort(weights)[::-1] weights /= weights.max() if debug: print weights.tolist() res = irtk.zeros(votes.get_header(),dtype='int') points = np.array(detection,dtype='int') if points.shape[0] > 30: points = points[:30] print np.arange(1,int(points.shape[0]+1))[::-1] res[points[:,0], points[:,1], points[:,2]] = np.arange(1,int(points.shape[0]+1))#[::-1] irtk.imwrite(debug_output+"/clusters.nii.gz",res) irtk.imwrite(debug_output+"/clusters_centers.nii.gz",irtk.landmarks_to_spheres(res,r=5)) if cutoff is not None: selected = np.sum( weights >= cutoff ) detection = detection[:selected] weights = weights[:selected] else: if len(detection) > n: detection = detection[:n] weights = weights[:n] return detection, weights
shutil.rmtree(args.output2) os.renames(tmp_output+"/"+str(best_candidate), args.output2) if os.path.exists(tmp_output): shutil.rmtree(tmp_output) else: p = np.unravel_index(np.argmax(votes_heart), votes_heart.shape ) score_candidate( p-args.padding, args.brain_center, output=args.output2, verbose=True ) landmark = irtk.zeros(hough_header,dtype='uint8') landmark[p[0],p[1],p[2]] = 1 landmark = irtk.landmarks_to_spheres(landmark, r=10) irtk.imwrite(args.output + "/detected_heart.nii.gz",landmark) # for each landmark, run predict2, keep best result if args.rw: img_filename = args.output2 + "/img.nii.gz" seg_filename = args.output2 + "/final_seg.nii.gz" rw_filename = args.output2 + "/rw.nii.gz" cmd = [ "python", os.path.dirname(__file__)+"/segment_random_walker.py", "--img", img_filename, "--seg", seg_filename,
# p_heart = np.unravel_index( np.argmax(votes_heart), img.shape ) landmarks = irtk.zeros(hough_header,dtype='uint8') landmarks[p_left_lung[0],p_left_lung[1],p_left_lung[2]] = 1 landmarks[p_right_lung[0],p_right_lung[1],p_right_lung[2]] = 2 landmarks[p_heart[0],p_heart[1],p_heart[2]] = 3 if ( 0 <= p_liver[0] < landmarks.shape[0] and 0 <= p_liver[1] < landmarks.shape[1] and 0 <= p_liver[2] < landmarks.shape[2] ): landmarks[p_liver[0],p_liver[1],p_liver[2]] = 4 if ( 0 <= p_brain[0] < landmarks.shape[0] and 0 <= p_brain[1] < landmarks.shape[1] and 0 <= p_brain[2] < landmarks.shape[2] ): landmarks[p_brain[0],p_brain[1],p_brain[2]] = 5 landmarks = irtk.landmarks_to_spheres(landmarks, r=10) irtk.imwrite(args.output + "/landmarks.nii.gz",landmarks) irtk.imwrite(args.output + "/votes_blurred_left_lung.nii.gz", irtk.Image(votes_left_lung,hough_header)) irtk.imwrite(args.output + "/votes_blurred_right_lung.nii.gz", irtk.Image(votes_right_lung,hough_header)) irtk.imwrite(args.output + "/votes_blurred_heart.nii.gz", irtk.Image(votes_heart,hough_header)) irtk.imwrite(args.output + "/votes_blurred_liver.nii.gz", irtk.Image(votes_liver,hough_header)) if args.back_proj: back_proj_left_lung = hough_votes_backprojection(np.ascontiguousarray(offsets_left_lung.astype('int32')), votes_left_lung, padding=args.padding)