Advertisement
nlw

Perona Malik diffusion wrapper

nlw
Apr 2nd, 2011
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. #!/usr/bin/python
  2. ####################################################################
  3. ## This program executes a Perona Malik anisotropic diffusion to
  4. ## smooth images preserving edges. (I've heard this version is not
  5. ## actually anisotropic, but it's definitely non-linear!)
  6. ##
  7. ## The actual diffusion iteration algorithm is at
  8. ##   http://pastebin.com/Mi21mud3
  9. ##
  10. ## Coded by: nwerneck@gmail.com
  11. ####################################################################
  12.  
  13.  
  14.  
  15. import scipy
  16. import scipy.ndimage
  17.  
  18. from scipy.linalg import eig
  19. from scipy.misc import imsave as myimsave
  20.  
  21. import numpy as np
  22. from numpy  import *
  23. import Image
  24. import sys
  25.  
  26.  
  27.  
  28. from nld_aux import *
  29.  
  30. if __name__=='__main__':
  31.     from pylab import *
  32.  
  33.     ion()
  34.  
  35.     rc('image', interpolation='nearest')
  36.  
  37.     ion()
  38.  
  39.     im = Image.open (sys.argv[1])
  40.     frame = np.array(im, dtype=np.float32)
  41.     img = np.array(im, dtype=np.float32)
  42.     img = img[:,:,:3] #remove alpha channel
  43.  
  44.     ## Parameters for the diffusion
  45.     alpha=0.05
  46.     glim=1./5
  47.  
  48.  
  49.     ## Initialize the arrays used in the algorithm.
  50.     gradx   = np.zeros((img.shape[0]  ,img.shape[1]-1,3), dtype=np.float32)
  51.     grady   = np.zeros((img.shape[0]-1,img.shape[1]  ,3), dtype=np.float32)
  52.     cgradx  = np.zeros((img.shape[0]  ,img.shape[1]-1), dtype=np.float32)
  53.     cgrady  = np.zeros((img.shape[0]-1,img.shape[1]  ), dtype=np.float32)
  54.  
  55.     # PErform 2400 update iterations
  56.     for k in range(2400):
  57.         print k
  58.         nldiffusion(img, gradx, grady, cgradx, cgrady, alpha, glim )
  59.  
  60.         # Save one image every 10 iterations to generate 10 seconds video.
  61.         if not k%10:
  62.             myimsave('aa%04d.png'%(k/10), img)
  63.  
  64.     figure(1)
  65.     imshow(frame/256.)
  66.     figure(2)
  67.     imshow(img/256.)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement