def show_chip_distinctiveness_plot(chip, kpts, dstncvs, fnum=1, pnum=None): import plottool as pt pt.figure(fnum, pnum=pnum) ax = pt.gca() divider = pt.ensure_divider(ax) #ax1 = divider.append_axes("left", size="50%", pad=0) ax1 = ax ax2 = divider.append_axes("bottom", size="100%", pad=0.05) #f, (ax1, ax2) = pt.plt.subplots(1, 2, sharex=True) cmapstr = 'rainbow' # 'hot' color_list = pt.df2.plt.get_cmap(cmapstr)(ut.norm_zero_one(dstncvs)) sortx = dstncvs.argsort() #pt.df2.plt.plot(qfx2_dstncvs[sortx], c=color_list[sortx]) pt.plt.sca(ax1) pt.colorline(np.arange(len(sortx)), dstncvs[sortx], cmap=pt.plt.get_cmap(cmapstr)) pt.gca().set_xlim(0, len(sortx)) pt.dark_background() pt.plt.sca(ax2) pt.imshow(chip, darken=.2) # MATPLOTLIB BUG CANNOT SHOW DIFFERENT ALPHA FOR POINTS AND KEYPOINTS AT ONCE #pt.draw_kpts2(kpts, pts_color=color_list, ell_color=color_list, ell_alpha=.1, ell=True, pts=True) #pt.draw_kpts2(kpts, color_list=color_list, pts_alpha=1.0, pts_size=1.5, # ell=True, ell_alpha=.1, pts=False) ell = ut.get_argflag('--ell') pt.draw_kpts2(kpts, color_list=color_list, pts_alpha=1.0, pts_size=1.5, ell=ell, ell_alpha=.3, pts=not ell) pt.plt.sca(ax)
def show_hist_submaxima(hist_, edges=None, centers=None, maxima_thresh=.8, pnum=(1, 1, 1)): r""" For C++ to show data Args: hist_ (?): edges (None): centers (None): CommandLine: python -m vtool.histogram --test-show_hist_submaxima --show python -m pyhesaff._pyhesaff --test-test_rot_invar --show python -m vtool.histogram --test-show_hist_submaxima --dpath figures --save ~/latex/crall-candidacy-2015/figures/show_hist_submaxima.jpg Example: >>> # DISABLE_DOCTEST >>> import plottool as pt >>> from vtool.histogram import * # NOQA >>> # build test data >>> hist_ = np.array(list(map(float, ut.get_argval('--hist', type_=list, default=[1, 4, 2, 5, 3, 3])))) >>> edges = np.array(list(map(float, ut.get_argval('--edges', type_=list, default=[0, 1, 2, 3, 4, 5, 6])))) >>> maxima_thresh = ut.get_argval('--maxima_thresh', type_=float, default=.8) >>> centers = None >>> # execute function >>> show_hist_submaxima(hist_, edges, centers, maxima_thresh) >>> pt.show_if_requested() """ #print(repr(hist_)) #print(repr(hist_.shape)) #print(repr(edges)) #print(repr(edges.shape)) #ut.embed() import plottool as pt #ut.embed() if centers is None: centers = hist_edges_to_centers(edges) bin_colors = pt.get_orientation_color(centers) pt.figure(fnum=pt.next_fnum(), pnum=pnum) POLAR = False if POLAR: pt.df2.plt.subplot(*pnum, polar=True, axisbg='#000000') pt.draw_hist_subbin_maxima(hist_, centers, bin_colors=bin_colors, maxima_thresh=maxima_thresh) #pt.gca().set_rmax(hist_.max() * 1.1) #pt.gca().invert_yaxis() #pt.gca().invert_xaxis() pt.dark_background() #if ut.get_argflag('--legend'): # pt.figure(fnum=pt.next_fnum()) # centers_ = np.append(centers, centers[0]) # r = np.ones(centers_.shape) * .2 # ax = pt.df2.plt.subplot(111, polar=True) # pt.plots.colorline(centers_, r, cmap=pt.df2.plt.get_cmap('hsv'), linewidth=10) # #ax.plot(centers_, r, 'm', color=bin_colors, linewidth=100) # ax.set_rmax(.2) # #ax.grid(True) # #ax.set_title("Angle Colors", va='bottom') title = ut.get_argval('--title', default='') import plottool as pt pt.set_figtitle(title)
def show_graph(infr, graph=None, use_image=False, update_attrs=True, with_colorbar=False, pnum=(1, 1, 1), zoomable=True, pickable=False, **kwargs): r""" Args: infr (?): graph (None): (default = None) use_image (bool): (default = False) update_attrs (bool): (default = True) with_colorbar (bool): (default = False) pnum (tuple): plot number(default = (1, 1, 1)) zoomable (bool): (default = True) pickable (bool): (de = False) **kwargs: verbose, with_labels, fnum, layout, ax, pos, img_dict, title, layoutkw, framewidth, modify_ax, as_directed, hacknoedge, hacknode, node_labels, arrow_width, fontsize, fontweight, fontname, fontfamilty, fontproperties CommandLine: python -m ibeis.algo.graph.mixin_viz GraphVisualization.show_graph --show Example: >>> # ENABLE_DOCTEST >>> from ibeis.algo.graph.mixin_viz import * # NOQA >>> from ibeis.algo.graph import demo >>> import plottool as pt >>> infr = demo.demodata_infr(ccs=ut.estarmap( >>> range, [(1, 6), (6, 10), (10, 13), (13, 15), (15, 16), >>> (17, 20)])) >>> pnum_ = pt.make_pnum_nextgen(nRows=1, nCols=3) >>> infr.show_graph(show_cand=True, simple_labels=True, pickable=True, fnum=1, pnum=pnum_()) >>> infr.add_feedback((1, 5), INCMP) >>> infr.add_feedback((14, 18), INCMP) >>> infr.refresh_candidate_edges() >>> infr.show_graph(show_cand=True, simple_labels=True, pickable=True, fnum=1, pnum=pnum_()) >>> infr.add_feedback((17, 18), NEGTV) # add inconsistency >>> infr.apply_nondynamic_update() >>> infr.show_graph(show_cand=True, simple_labels=True, pickable=True, fnum=1, pnum=pnum_()) >>> ut.show_if_requested() """ import plottool as pt if graph is None: graph = infr.graph # kwargs['fontsize'] = kwargs.get('fontsize', 8) with warnings.catch_warnings(): warnings.simplefilter("ignore") # default_update_kw = ut.get_func_kwargs(infr.update_visual_attrs) # update_kw = ut.update_existing(default_update_kw, kwargs) # infr.update_visual_attrs(**update_kw) if update_attrs: infr.update_visual_attrs(graph=graph, **kwargs) verbose = kwargs.pop('verbose', infr.verbose) pt.show_nx(graph, layout='custom', as_directed=False, modify_ax=False, use_image=use_image, pnum=pnum, verbose=verbose, **kwargs) if zoomable: pt.zoom_factory() pt.pan_factory(pt.gca()) # if with_colorbar: # # Draw a colorbar # _normal_ticks = np.linspace(0, 1, num=11) # _normal_scores = np.linspace(0, 1, num=500) # _normal_colors = infr.get_colored_weights(_normal_scores) # cb = pt.colorbar(_normal_scores, _normal_colors, lbl='weights', # ticklabels=_normal_ticks) # # point to threshold location # thresh = None # if thresh is not None: # xy = (1, thresh) # xytext = (2.5, .3 if thresh < .5 else .7) # cb.ax.annotate('threshold', xy=xy, xytext=xytext, # arrowprops=dict( # alpha=.5, fc="0.6", # connectionstyle="angle3,angleA=90,angleB=0"),) # infr.graph if graph.graph.get('dark_background', None): pt.dark_background(force=True) if pickable: fig = pt.gcf() fig.canvas.mpl_connect('pick_event', ut.partial(on_pick, infr=infr))
def show_coverage_grid(num_rows, num_cols, subbin_xy_arr, neighbor_bin_centers, neighbor_bin_weights, neighbor_bin_indices, fnum=None, pnum=None): """ visualizes the voting scheme on the grid. (not a mask, and no max) """ import plottool as pt import vtool as vt import matplotlib as mpl if fnum is None: fnum = pt.next_fnum() fig = pt.figure(fnum, pnum=pnum) ax = fig.gca() x_edge_indices = np.arange(num_cols) y_edge_indices = np.arange(num_rows) x_center_indices = vt.hist_edges_to_centers(x_edge_indices) y_center_indices = vt.hist_edges_to_centers(y_edge_indices) x_center_grid, y_center_grid = np.meshgrid(x_center_indices, y_center_indices) ax.set_xticks(x_edge_indices) ax.set_yticks(y_edge_indices) # Plot keypoint loc ax.scatter(subbin_xy_arr[0], subbin_xy_arr[1], marker='o') # Plot Weighted Lines to Subbin pt_colors = pt.distinct_colors(len(subbin_xy_arr.T)) segment_list = [] color_list = [] for subbin_centers, subbin_weights in zip(neighbor_bin_centers, neighbor_bin_weights): for pt_xys, center_xys, weight, color in zip(subbin_xy_arr.T, subbin_centers, subbin_weights, pt_colors): # Adjsut weight to alpha for easier visualization alpha = weight INCRESE_ALPHA_VISIBILITY = True if INCRESE_ALPHA_VISIBILITY: min_viz_alpha = .05 alpha = alpha * (1.0 - min_viz_alpha) + min_viz_alpha alpha **= 1.0 #pt.plots.colorline( segment = np.vstack((pt_xys, center_xys)) segment_list.append(segment) # Alpha becomes part of the colors color_list.append(list(color) + [alpha]) # DO NOT USE PLOT VERY SLOW #ax.plot(*segment.T, color=color, alpha=alpha, lw=3) ax = pt.gca() # Plot all segments in single line collection for speed # solid | dashed | dashdot | dotted lc = mpl.collections.LineCollection(segment_list, colors=color_list, linewidth=3, linestyles='solid') ax.add_collection(lc) # Plot Grid Center num_cells = num_cols * num_rows grid_alpha = min(.4, max(1 - (num_cells / 500), .1)) grid_color = [.6, .6, .6, grid_alpha] #print(grid_color) # Plot grid cetner ax.scatter(x_center_grid, y_center_grid, marker='.', color=grid_color, s=grid_alpha) ax.set_xlim(0, num_cols - 1) ax.set_ylim(0, num_rows - 1) #----- pt.dark_background() ax.grid(True, color=[.3, .3, .3]) ax.set_xticklabels([]) ax.set_yticklabels([])
def hackshow_names(ibs, aid_list, fnum=None): r""" Args: ibs (IBEISController): ibeis controller object aid_list (list): CommandLine: python -m ibeis.other.dbinfo --exec-hackshow_names --show python -m ibeis.other.dbinfo --exec-hackshow_names --show --db PZ_Master1 Example: >>> # DISABLE_DOCTEST >>> from ibeis.other.dbinfo import * # NOQA >>> import ibeis >>> ibs = ibeis.opendb(defaultdb='PZ_MTEST') >>> aid_list = ibs.get_valid_aids() >>> result = hackshow_names(ibs, aid_list) >>> print(result) >>> ut.show_if_requested() """ import plottool as pt import vtool as vt grouped_aids, nid_list = ibs.group_annots_by_name(aid_list) grouped_aids = [aids for aids in grouped_aids if len(aids) > 1] unixtimes_list = ibs.unflat_map(ibs.get_annot_image_unixtimes_asfloat, grouped_aids) yaws_list = ibs.unflat_map(ibs.get_annot_yaws, grouped_aids) #markers_list = [[(1, 2, yaw * 360 / (np.pi * 2)) for yaw in yaws] for yaws in yaws_list] unixtime_list = ut.flatten(unixtimes_list) timemax = np.nanmax(unixtime_list) timemin = np.nanmin(unixtime_list) timerange = timemax - timemin unixtimes_list = [((unixtimes[:] - timemin) / timerange) for unixtimes in unixtimes_list] for unixtimes in unixtimes_list: num_nan = sum(np.isnan(unixtimes)) unixtimes[np.isnan(unixtimes)] = np.linspace(-1, -.5, num_nan) #ydata_list = [np.arange(len(aids)) for aids in grouped_aids] sortx_list = vt.argsort_groups(unixtimes_list, reverse=False) #markers_list = ut.list_ziptake(markers_list, sortx_list) yaws_list = ut.list_ziptake(yaws_list, sortx_list) ydatas_list = vt.ziptake(unixtimes_list, sortx_list) #ydatas_list = sortx_list #ydatas_list = vt.argsort_groups(unixtimes_list, reverse=False) # Sort by num members #ydatas_list = ut.take(ydatas_list, np.argsort(list(map(len, ydatas_list)))) xdatas_list = [np.zeros(len(ydatas)) + count for count, ydatas in enumerate(ydatas_list)] #markers = ut.flatten(markers_list) #yaws = np.array(ut.flatten(yaws_list)) y_data = np.array(ut.flatten(ydatas_list)) x_data = np.array(ut.flatten(xdatas_list)) fnum = pt.ensure_fnum(fnum) pt.figure(fnum=fnum) ax = pt.gca() #unique_yaws, groupxs = vt.group_indices(yaws) ax.scatter(x_data, y_data, color=[1, 0, 0], s=1, marker='.') #pt.draw_stems(x_data, y_data, marker=markers, setlims=True, linestyle='') pt.dark_background() ax = pt.gca() ax.set_xlim(min(x_data) - .1, max(x_data) + .1) ax.set_ylim(min(y_data) - .1, max(y_data) + .1)
def in_depth_ellipse(kp): """ Makes sure that I understand how the ellipse is created form a keypoint representation. Walks through the steps I took in coming to an understanding. CommandLine: python -m pyhesaff.tests.test_ellipse --test-in_depth_ellipse --show --num-samples=12 Example: >>> # SCRIPT >>> from pyhesaff.tests.test_ellipse import * # NOQA >>> import pyhesaff.tests.pyhestest as pyhestest >>> test_data = pyhestest.load_test_data(short=True) >>> kpts = test_data['kpts'] >>> kp = kpts[0] >>> #kp = np.array([0, 0, 10, 10, 10, 0]) >>> test_locals = in_depth_ellipse(kp) >>> ut.quit_if_noshow() >>> ut.show_if_requested() """ import plottool as pt #nSamples = 12 nSamples = ut.get_argval('--num-samples', type_=int, default=12) kp = np.array(kp, dtype=np.float64) #----------------------- # SETUP #----------------------- np.set_printoptions(precision=3) #pt.reset() pt.figure(9003, docla=True, doclf=True) ax = pt.gca() ax.invert_yaxis() def _plotpts(data, px, color=pt.BLUE, label='', marker='.', **kwargs): #pt.figure(9003, docla=True, pnum=(1, 1, px)) pt.plot2(data.T[0], data.T[1], marker, '', color=color, label=label, **kwargs) #pt.update() def _plotarrow(x, y, dx, dy, color=pt.BLUE, label=''): ax = pt.gca() arrowargs = dict(head_width=.5, length_includes_head=True, label=label) arrow = mpl.patches.FancyArrow(x, y, dx, dy, **arrowargs) arrow.set_edgecolor(color) arrow.set_facecolor(color) ax.add_patch(arrow) #pt.update() #----------------------- # INPUT #----------------------- print('kp = %s' % ut.repr2(kp, precision=3)) print('--------------------------------') print('Let V = Perdoch.A') print('Let Z = Perdoch.E') print('Let invV = Perdoch.invA') print('--------------------------------') print('Input from Perdoch\'s detector: ') # We are given the keypoint in invA format if len(kp) == 5: (ix, iy, iv11, iv21, iv22) = kp iv12 = 0 elif len(kp) == 6: (ix, iy, iv11, iv21, iv22, ori) = kp iv12 = 0 invV = np.array([[iv11, iv12, ix], [iv21, iv22, iy], [0, 0, 1]]) V = np.linalg.inv(invV) Z = (V.T).dot(V) import vtool as vt V_2x2 = V[0:2, 0:2] Z_2x2 = Z[0:2, 0:2] V_2x2_ = vt.decompose_Z_to_V_2x2(Z_2x2) assert np.all(np.isclose(V_2x2, V_2x2_)) #C = np.linalg.cholesky(Z) #np.isclose(C.dot(C.T), Z) #Z print('invV is a transform from points on a unit-circle to the ellipse') ut.horiz_print('invV = ', invV) print('--------------------------------') print('V is a transformation from points on the ellipse to a unit circle') ut.horiz_print('V = ', V) print('--------------------------------') print('An ellipse is a special case of a conic. For any ellipse:') print( 'Points on the ellipse satisfy (x_ - x_0).T.dot(Z).dot(x_ - x_0) = 1') print('where Z = (V.T).dot(V)') ut.horiz_print('Z = ', Z) # Define points on a unit circle theta_list = np.linspace(0, TAU, nSamples) cicrle_pts = np.array([(np.cos(t_), np.sin(t_), 1) for t_ in theta_list]) # Transform those points to the ellipse using invV ellipse_pts1 = invV.dot(cicrle_pts.T).T #Lets check our assertion: (x_ - x_0).T.dot(Z).dot(x_ - x_0) == 1 x_0 = np.array([ix, iy, 1]) checks = [(x_ - x_0).T.dot(Z).dot(x_ - x_0) for x_ in ellipse_pts1] try: # HELP: The phase is off here. in 3x3 version I'm not sure why #assert all([almost_eq(1, check) for check in checks1]) is_almost_eq_pos1 = [ut.almost_eq(1, check) for check in checks] is_almost_eq_neg1 = [ut.almost_eq(-1, check) for check in checks] assert all(is_almost_eq_pos1) except AssertionError as ex: print('circle pts = %r ' % cicrle_pts) print(ex) print(checks) print([ut.almost_eq(-1, check, 1E-9) for check in checks]) raise else: #assert all([abs(1 - check) < 1E-11 for check in checks2]) print('... all of our plotted points satisfy this') #======================= # THE CONIC SECTION #======================= # All of this was from the Perdoch paper, now lets move into conic sections # We will use the notation from wikipedia # References: # http://en.wikipedia.org/wiki/Conic_section # http://en.wikipedia.org/wiki/Matrix_representation_of_conic_sections #----------------------- # MATRIX REPRESENTATION #----------------------- # The matrix representation of a conic is: #(A, B2, B2_, C) = Z.flatten() #(D, E, F) = (0, 0, 1) (A, B2, D2, B2_, C, E2, D2_, E2_, F) = Z.flatten() B = B2 * 2 D = D2 * 2 E = E2 * 2 assert B2 == B2_, 'matrix should by symmetric' assert D2 == D2_, 'matrix should by symmetric' assert E2 == E2_, 'matrix should by symmetric' print('--------------------------------') print('Now, using wikipedia\' matrix representation of a conic.') con = np.array(((' A', 'B / 2', 'D / 2'), ('B / 2', ' C', 'E / 2'), ('D / 2', 'E / 2', ' F'))) ut.horiz_print('A matrix A_Q = ', con) # A_Q is our conic section (aka ellipse matrix) A_Q = np.array(((A, B / 2, D / 2), (B / 2, C, E / 2), (D / 2, E / 2, F))) ut.horiz_print('A_Q = ', A_Q) #----------------------- # DEGENERATE CONICS # References: # http://individual.utoronto.ca/somody/quiz.html print('----------------------------------') print('As long as det(A_Q) != it is not degenerate.') print('If the conic is not degenerate, we can use the 2x2 minor: A_33') print('det(A_Q) = %s' % str(np.linalg.det(A_Q))) assert np.linalg.det(A_Q) != 0, 'degenerate conic' A_33 = np.array(((A, B / 2), (B / 2, C))) ut.horiz_print('A_33 = ', A_33) #----------------------- # CONIC CLASSIFICATION #----------------------- print('----------------------------------') print('The determinant of the minor classifies the type of conic it is') print('(det == 0): parabola, (det < 0): hyperbola, (det > 0): ellipse') print('det(A_33) = %s' % str(np.linalg.det(A_33))) assert np.linalg.det(A_33) > 0, 'conic is not an ellipse' print('... this is indeed an ellipse') #----------------------- # CONIC CENTER #----------------------- print('----------------------------------') print('the centers of the ellipse are obtained by: ') print('x_center = (B * E - (2 * C * D)) / (4 * A * C - B ** 2)') print('y_center = (D * B - (2 * A * E)) / (4 * A * C - B ** 2)') # Centers are obtained by solving for where the gradient of the quadratic # becomes 0. Without going through the derivation the calculation is... # These should be 0, 0 if we are at the origin, or our original x, y # coordinate specified by the keypoints. I'm doing the calculation just for # shits and giggles x_center = (B * E - (2 * C * D)) / (4 * A * C - B**2) y_center = (D * B - (2 * A * E)) / (4 * A * C - B**2) ut.horiz_print('x_center = ', x_center) ut.horiz_print('y_center = ', y_center) #----------------------- # MAJOR AND MINOR AXES #----------------------- # Now we are going to determine the major and minor axis # of this beast. It just the center augmented by the eigenvecs print('----------------------------------') # Plot ellipse axis # !HELP! I DO NOT KNOW WHY I HAVE TO DIVIDE, SQUARE ROOT, AND NEGATE!!! (evals, evecs) = np.linalg.eig(A_33) l1, l2 = evals # The major and minor axis lengths b = 1 / np.sqrt(l1) a = 1 / np.sqrt(l2) v1, v2 = evecs # Find the transformation to align the axis nminor = v1 nmajor = v2 dx1, dy1 = (v1 * b) dx2, dy2 = (v2 * a) minor = np.array([dx1, -dy1]) major = np.array([dx2, -dy2]) x_axis = np.array([[1], [0]]) cosang = (x_axis.T.dot(nmajor)).T # Rotation angle theta = np.arccos(cosang) print('a = ' + str(a)) print('b = ' + str(b)) print('theta = ' + str(theta[0] / TAU) + ' * 2pi') # The warped eigenvects should have the same magintude # As the axis lengths assert ut.almost_eq(a, major.dot(ltool.rotation_mat2x2(theta))[0]) assert ut.almost_eq(b, minor.dot(ltool.rotation_mat2x2(theta))[1]) try: # HACK if len(theta) == 1: theta = theta[0] except Exception: pass #----------------------- # ECCENTRICITY #----------------------- print('----------------------------------') print('The eccentricity is determined by:') print('') print(' (2 * np.sqrt((A - C) ** 2 + B ** 2)) ') print('ecc = -----------------------------------------------') print(' (nu * (A + C) + np.sqrt((A - C) ** 2 + B ** 2))') print('') print('(nu is always 1 for ellipses)') nu = 1 ecc_numer = (2 * np.sqrt((A - C)**2 + B**2)) ecc_denom = (nu * (A + C) + np.sqrt((A - C)**2 + B**2)) ecc = np.sqrt(ecc_numer / ecc_denom) print('ecc = ' + str(ecc)) # Eccentricity is a little easier in axis aligned coordinates # Make sure they aggree ecc2 = np.sqrt(1 - (b**2) / (a**2)) assert ut.almost_eq(ecc, ecc2) #----------------------- # APPROXIMATE UNIFORM SAMPLING #----------------------- # We are given the keypoint in invA format print('----------------------------------') print('Approximate uniform points an inscribed polygon bondary') #def next_xy(x, y, d): # # References: # # http://gamedev.stackexchange.com/questions/1692/what-is-a-simple-algorithm-for-calculating-evenly-distributed-points-on-an-ellip # num = (b ** 2) * (x ** 2) # den = ((a ** 2) * ((a ** 2) - (x ** 2))) # dxdenom = np.sqrt(1 + (num / den)) # deltax = d / dxdenom # x_ = x + deltax # y_ = b * np.sqrt(1 - (x_ ** 2) / (a ** 2)) # return x_, y_ def xy_fn(t): return np.array((a * np.cos(t), b * np.sin(t))).T #nSamples = 16 #(ix, iy, iv11, iv21, iv22), iv12 = kp, 0 #invV = np.array([[iv11, iv12, ix], # [iv21, iv22, iy], # [ 0, 0, 1]]) #theta_list = np.linspace(0, TAU, nSamples) #cicrle_pts = np.array([(np.cos(t_), np.sin(t_), 1) for t_ in theta_list]) uneven_points = invV.dot(cicrle_pts.T).T[:, 0:2] #uneven_points2 = xy_fn(theta_list) def circular_distance(arr): dist_most_ = ((arr[0:-1] - arr[1:])**2).sum(1) dist_end_ = ((arr[-1] - arr[0])**2).sum() return np.sqrt(np.hstack((dist_most_, dist_end_))) # Calculate the distance from each point on the ellipse to the next dists = circular_distance(uneven_points) total_dist = dists.sum() # Get an even step size multiplier = 1 step_size = total_dist / (nSamples * multiplier) # Walk along edge num_steps_list = [] offset_list = [] dist_walked = 0 total_dist = step_size for count in range(len(dists)): segment_len = dists[count] # Find where your starting location is offset_list.append(total_dist - dist_walked) # How far can you possibly go? total_dist += segment_len # How many steps can you take? num_steps = int((total_dist - dist_walked) // step_size) num_steps_list.append(num_steps) # Log how much further youve gotten dist_walked += (num_steps * step_size) #print('step_size = %r' % step_size) #print(np.vstack((num_steps_list, dists, offset_list)).T) # store the percent location at each line segment where # the cut will be made cut_list = [] for num, dist, offset in zip(num_steps_list, dists, offset_list): if num == 0: cut_list.append([]) continue offset1 = (step_size - offset) / dist offset2 = ((num * step_size) - offset) / dist cut_locs = (np.linspace(offset1, offset2, num, endpoint=True)) cut_list.append(cut_locs) #print(cut_locs) # Cut the segments into new better segments approx_pts = [] nPts = len(uneven_points) for count, cut_locs in enumerate(cut_list): for loc in cut_locs: pt1 = uneven_points[count] pt2 = uneven_points[(count + 1) % nPts] # Linearly interpolate between points new_loc = ((1 - loc) * pt1) + ((loc) * pt2) approx_pts.append(new_loc) approx_pts = np.array(approx_pts) # Warp approx_pts to the unit circle print('----------------------------------') print('For each aproximate point, find the closet point on the ellipse') #new_unit = V.dot(approx_pts.T).T ones_ = np.ones(len(approx_pts)) new_hlocs = np.vstack((approx_pts.T, ones_)) new_unit = V.dot(new_hlocs).T # normalize new_unit new_mag = np.sqrt((new_unit**2).sum(1)) new_unorm_unit = new_unit / np.vstack([new_mag] * 3).T new_norm_unit = new_unorm_unit / np.vstack([new_unorm_unit[:, 2]] * 3).T # Get angle (might not be necessary) x_axis = np.array([1, 0, 0]) arccos_list = x_axis.dot(new_norm_unit.T) uniform_theta_list = np.arccos(arccos_list) # Maybe this? uniform_theta_list = np.arctan2(new_norm_unit[:, 1], new_norm_unit[:, 0]) # unevn_cicrle_pts = np.array([(np.cos(t_), np.sin(t_), 1) for t_ in uniform_theta_list]) # This is the output. Approximately uniform points sampled along an ellipse uniform_ell_pts = invV.dot(unevn_cicrle_pts.T).T #uniform_ell_pts = invV.dot(new_norm_unit.T).T _plotpts(approx_pts, 0, pt.YELLOW, label='approx points', marker='o-') _plotpts(uniform_ell_pts, 0, pt.RED, label='uniform points', marker='o-') # Desired number of points #ecc = np.sqrt(1 - (b ** 2) / (a ** 2)) # Total arclength #total_arclen = ellipeinc(TAU, ecc) #firstquad_arclen = total_arclen / 4 # Desired arclength between points #d = firstquad_arclen / nSamples # Initial point #x, y = xy_fn(.001) #uniform_points = [] #for count in range(nSamples): # if np.isnan(x_) or np.isnan(y_): # print('nan on count=%r' % count) # break # uniform_points.append((x_, y_)) # The angle between the major axis and our x axis is: #----------------------- # DRAWING #----------------------- print('----------------------------------') # Draw the keypoint using the tried and true pt # Other things should subsiquently align #pt.draw_kpts2(np.array([kp]), ell_linewidth=4, # ell_color=pt.DEEP_PINK, ell_alpha=1, arrow=True, rect=True) # Plot ellipse points _plotpts(ellipse_pts1, 0, pt.LIGHT_BLUE, label='invV.dot(cicrle_pts.T).T', marker='o-') _plotarrow(x_center, y_center, dx1, -dy1, color=pt.GRAY, label='minor axis') _plotarrow(x_center, y_center, dx2, -dy2, color=pt.GRAY, label='major axis') # Rotate the ellipse so it is axis aligned and plot that rot = ltool.rotation_around_mat3x3(theta, ix, iy) ellipse_pts3 = rot.dot(ellipse_pts1.T).T #!_plotpts(ellipse_pts3, 0, pt.GREEN, label='axis aligned points') # Plot ellipse orientation ortho_basis = np.eye(3)[:, 0:2] orient_axis = invV.dot(ortho_basis) print(orient_axis) _dx1, _dx2, _dy1, _dy2, _1, _2 = orient_axis.flatten() #!_plotarrow(x_center, y_center, _dx1, _dy1, color=pt.BLUE, label='ellipse rotation') #!_plotarrow(x_center, y_center, _dx2, _dy2, color=pt.BLUE) #pt.plt.gca().set_xlim(400, 600) #pt.plt.gca().set_ylim(300, 500) xmin, ymin = ellipse_pts1.min(0)[0:2] - 1 xmax, ymax = ellipse_pts1.max(0)[0:2] + 1 pt.plt.gca().set_xlim(xmin, xmax) pt.plt.gca().set_ylim(ymin, ymax) pt.legend() pt.dark_background(doubleit=3) pt.gca().invert_yaxis() # Hack in another view # It seems like the even points are not actually that even. # there must be a bug pt.figure(fnum=9003 + 1, docla=True, doclf=True, pnum=(1, 3, 1)) _plotpts(ellipse_pts1, 0, pt.LIGHT_BLUE, label='invV.dot(cicrle_pts.T).T', marker='o-', title='even') pt.plt.gca().set_xlim(xmin, xmax) pt.plt.gca().set_ylim(ymin, ymax) pt.dark_background(doubleit=3) pt.gca().invert_yaxis() pt.figure(fnum=9003 + 1, pnum=(1, 3, 2)) _plotpts(approx_pts, 0, pt.YELLOW, label='approx points', marker='o-', title='approx') pt.plt.gca().set_xlim(xmin, xmax) pt.plt.gca().set_ylim(ymin, ymax) pt.dark_background(doubleit=3) pt.gca().invert_yaxis() pt.figure(fnum=9003 + 1, pnum=(1, 3, 3)) _plotpts(uniform_ell_pts, 0, pt.RED, label='uniform points', marker='o-', title='uniform') pt.plt.gca().set_xlim(xmin, xmax) pt.plt.gca().set_ylim(ymin, ymax) pt.dark_background(doubleit=3) pt.gca().invert_yaxis() return locals()