Example #1
0
        def computeInflections(self, res):
            if len(self.points) > 1:
                fit_x = np.array([])
                fit_y = np.array([])
                for crv in self.curves:
                    fit_x = np.hstack((fit_x, crv[:, 0]))
                    fit_y = np.hstack((fit_y, crv[:, 1]))

                # convert fit_x, fit_y into the physical coordinate.
                for i, (x, y) in enumerate(zip(fit_x, fit_y)):
                    phy = util.ImageToPhysics((x, y), res)
                    fit_x[i] = phy[0]
                    fit_y[i] = phy[1]

                try:
                    smooth_x, smooth_y, _ = util.smoothCurve(fit_x, fit_y)
                    user_s = self.controlPanel.getInterpolationParams()
                    tck, u = interpolate.splprep([smooth_x, smooth_y],
                                                 k=4,
                                                 s=user_s)
                    out = interpolate.splev(u, tck)

                    # convert interpolated curve points to Image Coordinate.
                    for i, (x, y) in enumerate(zip(out[0], out[1])):
                        img = util.PhysicsToImage((x, y), res)
                        out[0][i] = img[0]
                        out[1][i] = img[1]

                    self.interpCurve = np.column_stack(
                        (out[0], out[1])).astype(np.int32)

                    # compute length
                    dxdy = res * np.diff(self.interpCurve, axis=0)
                    length = np.sum(np.linalg.norm(dxdy, 2, axis=1))

                    unew = np.linspace(0, 1, len(u), endpoint=True)
                    dx = interpolate.splev(unew, tck, der=1)
                    ddx = interpolate.splev(unew, tck, der=2)
                    k_sample = (dx[0] * ddx[1] -
                                ddx[0] * dx[1]) / np.linalg.norm(dx)**3
                    fs = interpolate.InterpolatedUnivariateSpline(unew,
                                                                  k_sample,
                                                                  k=3)
                    roots = fs.roots()

                    if (len(roots) > 0):
                        outroots = []
                        out = interpolate.splev(roots, tck)
                        for ipt in np.transpose(out):
                            ipt = util.PhysicsToImage(ipt, res)
                            outroots.append(ipt)
                        roots = outroots
                    self.inflections = roots
                    self.messagePrinted.emit(
                        "Inflection points: {} length: {:.2f} mm".format(
                            len(roots), length))
                except ValueError as e:
                    print e
                    pass
            self.repaint()
Example #2
0
 def computeInflections(self, res):
     if len(self.points) > 1:
         fit_x = np.array([])
         fit_y = np.array([])
         for crv in self.curves:
             fit_x = np.hstack((fit_x, crv[:, 0]))
             fit_y = np.hstack((fit_y, crv[:, 1]))
             
         # convert fit_x, fit_y into the physical coordinate.
         for i, (x, y) in enumerate(zip(fit_x, fit_y)):
             phy = util.ImageToPhysics((x,y), res)
             fit_x[i] = phy[0]
             fit_y[i] = phy[1]
         
         try:
             smooth_x, smooth_y, _ = util.smoothCurve(fit_x, fit_y)
             user_s = self.controlPanel.getInterpolationParams()
             tck, u = interpolate.splprep([smooth_x, smooth_y], k=4, s=user_s)
             out = interpolate.splev(u, tck)
             
             # convert interpolated curve points to Image Coordinate.
             for i, (x, y) in enumerate(zip(out[0], out[1])):
                 img = util.PhysicsToImage((x, y), res)
                 out[0][i] = img[0]
                 out[1][i] = img[1]
                 
             self.interpCurve = np.column_stack((out[0], out[1])).astype(np.int32)
             
             # compute length
             dxdy = res * np.diff(self.interpCurve, axis=0)
             length = np.sum( np.linalg.norm(dxdy, 2, axis=1) )
             
             unew = np.linspace(0, 1, len(u), endpoint=True)
             dx = interpolate.splev(unew, tck, der=1)
             ddx = interpolate.splev(unew, tck, der=2)
             k_sample = (dx[0]*ddx[1]-ddx[0]*dx[1])/np.linalg.norm(dx)**3
             fs = interpolate.InterpolatedUnivariateSpline(unew, k_sample, k=3)
             roots = fs.roots()      
 
             if (len(roots)>0):
                 outroots = []
                 out = interpolate.splev(roots, tck)
                 for ipt in np.transpose(out):
                     ipt = util.PhysicsToImage(ipt, res)
                     outroots.append(ipt)
                 roots = outroots            
             self.inflections = roots
             self.messagePrinted.emit("Inflection points: {} length: {:.2f} mm".format(len(roots), length))
         except ValueError as e:
             print e
             pass
     self.repaint()        
Example #3
0
                    outroots.append(ipt)
                self.roots = outroots
            #self.inflections = roots
            #self.messagePrinted.emit("Inflection points: {} length: {:.2f} mm".format(len(roots), length))
            return (self.imgFit_x, self.imgFit_y, self.outcrv)

        except ValueError as e:
            print e
            return None

di = DebugInfo()
#di.load(r'D:\Documents\Projects\Python\livewire\liver\1.npy')
di.load(r'/Users/stardust/Projects/Python/livewire/liver/1.npy')
(input_x, input_y) = di.CurvesToFlat()
(input_x, input_y) = di.ConvertToPhysicalCoord(input_x, input_y)
new_fit_x, new_fit_y, kappa = util.smoothCurve(input_x, input_y)
res_x, res_y = util.resampleCurve(new_fit_x, new_fit_y, 100)

inputCrv = Curve(new_fit_x, new_fit_y, 1.0)
k_crv = inputCrv.GetCurvatureCurve()

import matplotlib.pyplot as plt

u = np.linspace(0, 1, len(inputCrv.u))
out = inputCrv.GetSamples(u)
k_out = k_crv(u)
k_roots = k_crv.roots()
k_infs = np.array(k_crv(k_roots)).transpose()

inflections = inputCrv.GetSamples(k_roots)
crossidx = util.findCrossings(kappa)
Example #4
0
                self.roots = outroots
            #self.inflections = roots
            #self.messagePrinted.emit("Inflection points: {} length: {:.2f} mm".format(len(roots), length))
            return (self.imgFit_x, self.imgFit_y, self.outcrv)

        except ValueError as e:
            print e
            return None


di = DebugInfo()
#di.load(r'D:\Documents\Projects\Python\livewire\liver\1.npy')
di.load(r'/Users/stardust/Projects/Python/livewire/liver/1.npy')
(input_x, input_y) = di.CurvesToFlat()
(input_x, input_y) = di.ConvertToPhysicalCoord(input_x, input_y)
new_fit_x, new_fit_y, kappa = util.smoothCurve(input_x, input_y)
res_x, res_y = util.resampleCurve(new_fit_x, new_fit_y, 100)

inputCrv = Curve(new_fit_x, new_fit_y, 1.0)
k_crv = inputCrv.GetCurvatureCurve()

import matplotlib.pyplot as plt

u = np.linspace(0, 1, len(inputCrv.u))
out = inputCrv.GetSamples(u)
k_out = k_crv(u)
k_roots = k_crv.roots()
k_infs = np.array(k_crv(k_roots)).transpose()

inflections = inputCrv.GetSamples(k_roots)
crossidx = util.findCrossings(kappa)