def ext_partial(spl, nth):
     if knots[nth] < 0:
         ust1 = 0.0
         ued1 = knots[nth + deg + 1]
         ust2 = knots[nth + nbr_lib]
         ued2 = 1.0
         n_dct1 = (ued1 - ust1) * n_total
         n_dct2 = (ued2 - ust2) * n_total
         n_dct1 = int(np.ceil(n_dct1))
         n_dct2 = int(np.ceil(n_dct2))
         return compute_energy_external_partial(spline=spl,
                                                img=img,
                                                ust=ust1, ued=ued1,
                                                nbr_dct=n_dct1,
                                                normalized=False) + \
             compute_energy_external_partial(spline=spl,
                                             img=img,
                                             ust=ust2, ued=ued2,
                                             nbr_dct=n_dct2,
                                             normalized=False)
     else:
         ust = knots[nth]
         ued = knots[nth + deg + 1]
         n_dct = (ued - ust) * n_total
         n_dct = int(np.ceil(n_dct))
         return compute_energy_external_partial(spline=spl,
                                                img=img,
                                                ust=ust,
                                                ued=ued,
                                                nbr_dct=n_dct,
                                                normalized=False)
 def cost(ctrl_pts, nth):
     nth = nth // 2
     knots = spline.t
     deg = spline.k
     nbr_lib = len(ctrl_pts) / 2
     n_total = 1000
     if knots[nth] < 0:
         ust1 = 0
         ued1 = knots[nth + deg + 1]
         ust2 = knots[nth + nbr_lib]
         ued2 = 1.0
         n_dct1 = (ued1 - ust1) * n_total
         n_dct2 = (ued2 - ust2) * n_total
         n_dct1 = int(np.ceil(n_dct1))
         n_dct2 = int(np.ceil(n_dct2))
         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:
             ret1 = compute_energy_internal_partial(spline=spline,
                                                    ust=ust1, ued=ued1,
                                                    nbr_dct=n_dct1) + \
                 compute_energy_internal_partial(spline=spline,
                                                 ust=ust2, ued=ued2,
                                                 nbr_dct=n_dct2)
         else:
             ret1 = compute_energy_internal_partial(spline=spline,
                                                    ust=ust1, ued=ued1,
                                                    nbr_dct=n_dct1) + \
                 compute_energy_internal_partial(spline=spline,
                                                 ust=ust2, ued=ued2,
                                                 nbr_dct=n_dct2) + gamma * \
                 compute_energy_external_partial(spline=spline, img=img,
                                                 ust=ust1, ued=ued1,
                                                 nbr_dct=n_dct1) + gamma * \
                 compute_energy_external_partial(spline=spline, img=img,
                                                 ust=ust2, ued=ued2,
                                                 nbr_dct=n_dct2)
         return ret1
     else:
         ust = knots[nth]
         ued = knots[nth + deg + 1]
         n_dct = (ued - ust) * n_total
         n_dct = int(np.ceil(n_dct))
         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:
             ret2 = compute_energy_internal_partial(spline, ust, ued, n_dct)
         else:
             ret2 = compute_energy_internal_partial(spline=spline,
                                                    ust=ust, ued=ued,
                                                    nbr_dct=n_dct) + gamma * \
                 compute_energy_external_partial(spline=spline, img=img,
                                                 ust=ust, ued=ued,
                                                 nbr_dct=n_dct)
         return ret2
Beispiel #3
0
    def ext_partial(spline, nth):
        '''
        The function is used to compute the partial external energy given the
        control point and the order of the varialbe. Firstly, we choose the
        internal interval and compute the partial energy. It helps to compute
        the simplified jacobian.

        Parameters
        ----------
        ctrl_pts : numpy.narray
            The flattened vector of control points
        nth: integer
            The order of the control point of which we want to compute the
            partial energy
        Returns
        -------
        ret1(ret2) : float
            Partial external energy corresponding the specified control point
        '''
        if knots[nth] < 0:
            ust1 = 0
            ued1 = knots[nth + deg + 1]
            ust2 = knots[nth + nbr_lib]
            ued2 = 1.0
            n_dct1 = (ued1 - ust1) * n_total
            n_dct2 = (ued2 - ust2) * n_total
            n_dct1 = int(np.ceil(n_dct1))
            n_dct2 = int(np.ceil(n_dct2))

            ret1 = gamma * \
                compute_energy_external_partial(spline=spline, img=img,
                                                ust=ust1, ued=ued1,
                                                nbr_dct=n_dct1,
                                                normalized=False) + gamma * \
                compute_energy_external_partial(spline=spline, img=img,
                                                ust=ust2, ued=ued2,
                                                nbr_dct=n_dct2,
                                                normalized=False)
            return ret1
        else:
            ust = knots[nth]
            ued = knots[nth + deg + 1]
            n_dct = (ued - ust) * n_total
            n_dct = int(np.ceil(n_dct))
            ret2 = gamma * \
                compute_energy_external_partial(spline=spline, img=img,
                                                ust=ust, ued=ued,
                                                nbr_dct=n_dct,
                                                normalized=False)
            return ret2
Beispiel #4
0
def changement_discretisation():
    # How the external energy changes with respect to the level of discretization
    n_dct = np.linspace(start=10, stop=2000, num=100, dtype=np.int16)
    energy = np.zeros(100)
    plt.rcParams["font.family"] = "serif"
    spl = spline_initialize(100)
    img, imsh = create_image(30)
    for i, data in enumerate(n_dct):
        energy[i] = compute_energy_external_partial(spline=spl, img=img,
                                                    ust=0, ued=1.0,
                                                    nbr_dct=data)
    fig = plt.figure()
    fig.canvas.set_window_title("Dicretisation")
    plt.plot(n_dct, energy)
    plt.xlabel('Nombre de point de discretisation')
    plt.ylabel('Energie')
    plt.legend()
    plt.show()
Beispiel #5
0
    def cost_partial(ctrl_pts, nth):
        '''
        The function is used to compute the jacobian given the control point
        and the order of the varialbe. Firstly, we choose the internal interval
        and compute the partial energy. It helps to compute the simplified
        jacobian

        Parameters
        ----------
        ctrl_pts : numpy.narray
            The flattened vector of control points
        nth: integer
            The order of the control point of which we want to compute the
            partial energy

        Returns
        -------
        partial_energy : float
            The partial energy corresponding to variables nth. If img=None, it
            returns the partial internal energy. If not, it returns the partial
            total (internal + external) energy.

        References
        ----------
        Les Piegl, Wayne Tiller, The NURBS book p.84

        '''
        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 knots[nth] < 0:
            ust1 = 0
            ued1 = knots[nth + deg + 1]
            ust2 = knots[nth + nbr_lib]
            ued2 = 1.0
            n_dct1 = (ued1 - ust1) * n_total
            n_dct2 = (ued2 - ust2) * n_total
            n_dct1 = int(np.ceil(n_dct1))
            n_dct2 = int(np.ceil(n_dct2))
            if img is None:
                ret1 = compute_energy_internal_partial(spline=spline,
                                                       ust=ust1, ued=ued1,
                                                       nbr_dct=n_dct1) + \
                    compute_energy_internal_partial(spline=spline,
                                                    ust=ust2, ued=ued2,
                                                    nbr_dct=n_dct2)
            else:
                ret1 = compute_energy_internal_partial(spline=spline,
                                                       ust=ust1, ued=ued1,
                                                       nbr_dct=n_dct1) + \
                    compute_energy_internal_partial(spline=spline,
                                                    ust=ust2, ued=ued2,
                                                    nbr_dct=n_dct2) + gamma * \
                    compute_energy_external_partial(spline=spline, img=img,
                                                    ust=ust1, ued=ued1,
                                                    nbr_dct=n_dct1) + gamma * \
                    compute_energy_external_partial(spline=spline, img=img,
                                                    ust=ust2, ued=ued2,
                                                    nbr_dct=n_dct2)
            return ret1
        else:
            ust = knots[nth]
            ued = knots[nth + deg + 1]
            n_dct = (ued - ust) * n_total
            n_dct = int(np.ceil(n_dct))
            if img is None:
                ret2 = compute_energy_internal_partial(spline, ust, ued, n_dct)
            else:
                ret2 = compute_energy_internal_partial(spline=spline,
                                                       ust=ust, ued=ued,
                                                       nbr_dct=n_dct) + gamma * \
                    compute_energy_external_partial(spline=spline, img=img,
                                                    ust=ust, ued=ued,
                                                    nbr_dct=n_dct)
            return ret2