def calibration_combination(data): min_magno = np.array([float("inf"), float("inf"), float("inf")]) max_magno = -np.array([float("inf"), float("inf"), float("inf")]) for vec in data: min_magno = np.minimum(min_magno, vec) max_magno = np.maximum(max_magno, vec) magno_sf = 2 / (max_magno - min_magno) magno_bias = (max_magno + min_magno) / 2 calibrated_initial = (data - magno_bias) * magno_sf data2 = data_regularize(calibrated_initial) center, (a, b, c), evecs, v = ellipsoid_fit(data2) D = np.array([[1 / a, 0., 0.], [0., 1 / b, 0.], [0., 0., 1 / c]]) transform = np.dot(np.diag(magno_sf),evecs.dot(D).dot(evecs.T)) offset = center.T + magno_bias calibrated = transform.dot((data - offset).T).T plot_scatter(calibrated, "Combination Calibration", 100) eval = eval_calibrated(calibrated) print("Combination Calibration (Transform - {}, Offset - {} Eval - {})".format( np.array_str(transform), np.array_str(offset), str(eval)))
def _monte_carlo_identity(): '''Generate the default params that don't affect readings by fitting to a unit sphere''' # create points randomly distributed on the unit sphere npoints = 25000 vec = np.random.randn(3, npoints) vec /= np.linalg.norm(vec, axis=0) # use those to fit regularized = data_regularize(vec.T, divs=8) return ellipsoid_fit(regularized)
def calibration_ellipsoid(file='magcalib.txt'): data = read_mag_file(file) data = data_regularize(data) center, evecs, radii = ellipsoid_fit(data) a, b, c = radii #r = (a * b * c) ** (1. / 3.) D = np.array([[1/a, 0., 0.], [0., 1/b, 0.], [0., 0., 1/c]]) transformation = evecs@[email protected] return center.T,transformation
def calibration_ellipsoid(data): data2 = data_regularize(data) plot_scatter(data2, "Regularized Data", 1) center, (a, b, c), evecs, v = ellipsoid_fit(data2) D = np.array([[1 / a, 0., 0.], [0., 1 / b, 0.], [0., 0., 1 / c]]) transform = evecs.dot(D).dot(evecs.T) calibrated = transform.dot((data - center.T).T).T plot_scatter(calibrated, "Calibrated ADIS16405 Magnetometer Data", 100) eval = eval_calibrated(calibrated) print("Ellipsoid Calibration (Transform - {}, Offset - {} Eval - {})".format( np.array_str(transform), np.array_str(center.T), str(eval))) return transform
# 右腎臓のみを抽出 removed_seg = tb.remove_left_kidney(seg) # contour_pts = tb.get_full_pts(seg) # kmeans = KMeans(n_clusters=2,random_state=173).fit(contour_pts) # pred = np.array(kmeans.labels_) # selected_clu = int(kmeans.cluster_centers_[0,2] < kmeans.cluster_centers_[1,2]) # not_selected = contour_pts[pred != selected_clu,:] # removed_seg = seg.copy() # for i in range(not_selected.shape[0]): # removed_seg[not_selected[i,0],not_selected[i,1],not_selected[i,2]] = 0 contour_pts = tb.get_contour_pts(removed_seg) center, evecs, radius = ef.ellipsoid_fit(contour_pts) # if 1.5 * np.sum(removed_seg) < 4/3*math.pi*radius[0]*radius[1]*radius[2]: # continue if vis: img_mask = tb.get_image_mask_points(seg, contour_pts) color_img = tb.draw_segmentation(seg, img_mask, mark_val=255) tb.show_ct_image(color_img) surf = tb.make_nearest_surf(center, radius, evecs, contour_pts, vis=vis,
import numpy as np from ellipsoid_fit import ellipsoid_fit as ellipsoid_fit, data_regularize if __name__=='__main__': data = np.loadtxt("mag_out.txt") data2 = data_regularize(data) center, radii, evecs, v = ellipsoid_fit(data2) a,b,c = radii r = (a*b*c)**(1./3.) D = np.array([[r/a,0.,0.],[0.,r/b,0.],[0.,0.,r/c]]) TR = evecs.dot(D).dot(evecs.T) print print 'center: ',center print 'transformation:' print TR np.savetxt('magcal_ellipsoid.txt', np.vstack((center.T, TR)))
import numpy as np from ellipsoid_fit import ellipsoid_fit as ellipsoid_fit, data_regularize if __name__ == '__main__': data = np.loadtxt("mag_out.txt") data2 = data_regularize(data) center, radii, evecs, v = ellipsoid_fit(data2) a, b, c = radii r = (a * b * c)**(1. / 3.) D = np.array([[r / a, 0., 0.], [0., r / b, 0.], [0., 0., r / c]]) TR = evecs.dot(D).dot(evecs.T) print('') print('center: ', center) print('transformation:') print(TR) np.savetxt('magcal_ellipsoid.txt', np.vstack((center.T, TR)))
clip_binary[i, :, :] = thresh < clip[i, :, :] points = np.zeros((np.sum(clip_binary), 3)) count = 0 for i in range(clip.shape[0]): for j in range(clip.shape[1]): for k in range(clip.shape[2]): if clip_binary[i, j, k]: points[count, :] = np.array([ i, j, k ]) + bounding_box[:, 0] + 0.0001 * np.random.rand(3) count += 1 center, rotation, radius = ef.ellipsoid_fit(points) # center = np.array(center) + bounding_box[:,0] print(center) print(rotation) print(radius) np.save( '/Users/shomakitamiya/Documents/python/snake3D/data/SliceLabel/all/' + str(case) + '/bb_center' + human + '.npy', center) np.save( '/Users/shomakitamiya/Documents/python/snake3D/data/SliceLabel/all/' + str(case) + '/bb_rotation' + human + '.npy', rotation) np.save( '/Users/shomakitamiya/Documents/python/snake3D/data/SliceLabel/all/' + str(case) + '/bb_radius' + human + '.npy', radius)
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from ellipsoid_fit import ellipsoid_fit, ellipsoid_plot, data_regularize if __name__=='__main__': #data = np.loadtxt("magcal_ellipsoid.txt") #data = np.loadtxt("../acceldata.txt") data = np.loadtxt("../magneticdata.txt") data2 = data_regularize(data, divs=8) center, evecs, radii = ellipsoid_fit(data2) data_centered = data - center.T data_centered_regularized = data2 - center.T a, b, c = radii r = (a * b * c) ** (1. / 3.) D = np.array([[r/a, 0., 0.], [0., r/b, 0.], [0., 0., r/c]]) #http://www.cs.brandeis.edu/~cs155/Lecture_07_6.pdf #affine transformation from ellipsoid to sphere (translation excluded) TR = evecs.dot(D).dot(evecs.T) data_on_sphere = TR.dot(data_centered_regularized.T).T fig = plt.figure() ax = fig.add_subplot(111, projection='3d') #hack for equal axes ax.set_aspect('equal') for direction in (-1, 1):
import numpy as np from ellipsoid_fit import ellipsoid_fit as ellipsoid_fit, data_regularize if __name__ == '__main__': data = np.loadtxt("mag_out.txt") # data2 = data_regularize(data) center, evecs, radii, v = ellipsoid_fit(data) a, b, c = radii r = (a * b * c)**(1. / 3.) D = np.array([[r / a, 0., 0.], [0., r / b, 0.], [0., 0., r / c]]) transformation = evecs.dot(D).dot(evecs.T) print('') print('center: ', center) print('radii: ', radii) print('evecs: ', evecs) print('transformation:') print(transformation) print('Coefficients:') print(v) np.savetxt('magcal_ellipsoid.txt', np.vstack((center.T, transformation)))
def calibrate(self, raw_mag): #not sure what regularizing does but the ellipsoid_fit example uses it... regularized = data_regularize(raw_mag, divs=8) center, radii, evecs, v = ellipsoid_fit(regularized) self.params = center, radii, evecs, v
contour_pts = np.array(contour_pts) pred = KMeans(n_clusters=2, random_state=173).fit_predict(contour_pts) # fig = plt.figure() # ax = fig.add_subplot(111, projection='3d') # for i in range(2): # ax.scatter(contour_pts[pred==i,0], contour_pts[pred==i,1], contour_pts[pred==i,2]) # plt.show() c_no = 0 # center, radius = tb.sphere_fit(contour_pts[pred==c_no,0],contour_pts[pred==c_no,1],contour_pts[pred==c_no,2]) # radius -= 5 center, evecs, radii = ef.ellipsoid_fit(contour_pts[pred == c_no, :]) print(center) surf = tb.make_ellipsoid_axis_surf(center, radii, evecs) color_img = tb.draw_contour(seg, surf) tb.show_ct_image(color_img) rect_size = np.array([20, 6, 6]) surf = tb.cul_contour(seg, surf, rect_size, div=20, N_limit=5, c=0.95, ctrlpts_size=8,
radius[1] * math.cos(v) * math.sin(u), radius[2] * math.sin(v) ] for u in np.linspace(0, 2 * math.pi, num=psize) for v in np.linspace(-math.pi / 2 + 0.01, math.pi / 2 - 0.01, num=psize)]) for i in range(len(points)): points[i] = np.dot(points[i], rotation) points += center fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # ef.ellipsoid_plot(center, radius, rotation, ax=ax, plot_axes=True, cage_color='g') ax.scatter(points[:, 0], points[:, 1], points[:, 2]) plt.show() print(points.shape) center, evecs, radii = ef.ellipsoid_fit(points) print(center) print(evecs) print(radii) points = np.array( [[ radii[0] * math.cos(u) * math.cos(v), radii[1] * math.cos(v) * math.sin(u), radii[2] * math.sin(v) ] for u in np.linspace(0, 2 * math.pi, num=psize) for v in np.linspace(-math.pi / 2 + 0.01, math.pi / 2 - 0.01, num=psize)]) for i in range(len(points)): points[i] = np.dot(points[i], evecs) points += center