def cost(ctrl): ctrl = np.array(ctrl) ctrl = np.reshape(ctrl, ((len(ctrl) / 2), 2)) spline.set_ctrl_pts(ctrl, only_free=True) if img is None: ret = compute_energy_internal_total(spline=spline, nbr_dct=1000) else: ret = compute_energy_internal_total(spline=spline, nbr_dct=1000) + \ gamma * compute_energy_external_total(spline=spline, img=img, nbr_dct=1000) return ret
def cost(ctrl_pts): ''' Compute the total energy given the new control points. It acts as the cost function of the optimisation. Parameters ---------- ctrl_pts : numpy.ndarray The flattened vector of control points Returns ------- ret: float The total energy corresponding to the given control points. ''' ctrl_pts = np.array(ctrl_pts) ctrl_pts = np.reshape(ctrl_pts, ((len(ctrl_pts) / 2), 2)) spline.set_ctrl_pts(ctrl_pts, only_free=True) ret = compute_energy_internal_total(spline=spline, ba_ratio=ba_ratio, nbr_dct=nbr) + \ gamma * compute_energy_external_total(spline=spline, img=img, nbr_dct=nbr, normalized=True) return ret
def cost(ctrl): ctrl = np.array(ctrl) ctrl = np.reshape(ctrl, ((len(ctrl) / 2), 2)) spline.set_ctrl_pts(ctrl, only_free=True) return compute_energy_external_total(spline=spline, img=img, nbr_dct=10000, normalized=True) + \ compute_energy_internal_total(spline=spline, nbr_dct=10000)
def cost(ctrl_pts): ''' Compute the total energy given the new control points. It is the cost function of the optimisation. It also helps to compute the traditional jacobian. Parameters ---------- ctrl_pts : numpy.ndarray The flattened vector of control points Returns ------- ret: float The total energy corresponding to the given control points.If img=None, it returns the partial internal energy. If not, it returns the partial total (internal + external) energy. ''' ctrl_pts = np.array(ctrl_pts) ctrl_pts = np.reshape(ctrl_pts, ((len(ctrl_pts) / 2), 2)) spline.set_ctrl_pts(ctrl_pts, only_free=True) if img is None: ret = compute_energy_internal_total(spline=spline, ba_ratio=ba_ratio, nbr_dct=nbr) else: ret = compute_energy_internal_total(spline=spline, ba_ratio=ba_ratio, nbr_dct=nbr) + \ gamma * compute_energy_external_total(spline=spline, img=img, nbr_dct=nbr, normalized=True) return ret
def changement_rayon_total(): # How the total energy changes with respect to the radius given that we # know the optimal radius n_dct = 1000 img, _ = create_image(30) R = np.linspace(20, 40, 1000) e_ext = np.zeros(1000) e_int = np.zeros(1000) e_total = np.zeros(1000) dict = {} gamma = 50 for i, data in enumerate(R): spl = spline_initialize(data) e_int[i] = compute_energy_internal_total(spline=spl, ba_ratio=400, nbr_dct=n_dct) e_ext[i] = compute_energy_external_total(spl, img, n_dct) e_total[i] = e_int[i] + gamma * e_ext[i] dict[data] = e_total[i] print min(dict.iteritems(), key=operator.itemgetter(1))[0] fig = plt.figure() fig.canvas.set_window_title("Rayon") ax = fig.add_subplot(111) ax1 = fig.add_subplot(311) ax2 = fig.add_subplot(312) ax3 = fig.add_subplot(313) ax1.title.set_text('Le changement avec gamma = ' + str(gamma)) ax1.plot(R, e_int, 'r', label='Energie interne') ax1.legend(fontsize='large') ax2.plot(R, e_ext, 'g', label='Energie externe') ax2.legend(fontsize='large') ax3.plot(R, e_total, 'b', label='Energie totale') ax3.legend(fontsize='large') ax.spines['top'].set_color('none') ax.spines['bottom'].set_color('none') ax.spines['left'].set_color('none') ax.spines['right'].set_color('none') ax.tick_params(labelcolor='w', top='off', bottom='off', left='off', right='off') ax.set_xlabel('Rayon') ax.set_ylabel("Energie") plt.show()
def compute_internal(): st = timeit.default_timer() for i in range(50): compute_energy_internal_total(initialize(50)) ed = timeit.default_timer() print ed - st