def get_gradient_vector_flow(self, potential): """ calculate the gradient vector flow from the given potential. This function is not very well tested, yet """ # use Eq. 12 from Paper `Gradient Vector Flow: A New External Force for Snakes` u, v = self.get_buffers([0, 1], potential.shape) fx = cv2.Sobel(potential, cv2.CV_64F, 1, 0, ksize=5) fy = cv2.Sobel(potential, cv2.CV_64F, 0, 1, ksize=5) fxy = fx**2 + fy**2 # print fx.max(), fy.max(), fxy.max() # debug.show_image(potential, fx, fy, fxy, u, v) mu = 10 def dudt(u): return mu*cv2.Laplacian(u, cv2.CV_64F) - (u - fx)*fxy def dvdt(v): return mu*cv2.Laplacian(v, cv2.CV_64F) - (v - fy)*fxy N = 10000 #< maximum number of steps that the integrator is allowed dt = 1e-4 #< time step for n in xrange(N): rhs = dudt(u) residual = np.abs(rhs).sum() if n % 1000 == 0: print n*dt, '%e' % residual u += dt*rhs for n in xrange(N): rhs = dvdt(v) # residual = np.abs(rhs).sum() # if n % 100 == 0: # print n*dt, '%e' % residual v += dt*rhs debug.show_image(potential, (u, v)) return (u, v)
def show_debug_image(self): """ shows the debug image on screen """ self.add_debug_output() debug.show_image(self.image)