Anisotropic Diffusion 2: The Implementation




As it is well known, deviations occur in the implementation of algorithms especially, if the algorithm was defined with reference to the continuous domain. The same is applicable in the implementation of the already defined principle for anisotropic diffusion. We would be implementing here, in python, a discrete model of anisotropic diffusion to deal with Digital Images which are ofcourse discrete.


First of all, we import the necessary libraries that we would use throughout the course of the implementation. Our implementation would most importantly be hinged on the numpy and scipy libraries in python.

 import numpy as np  
 import scipy.ndimage as sp  
 import scipy.misc as sv  
 from scipy.ndimage.filters import gaussian_filter as gaussian  


It is much easier to work with a grayscale image so we load the image as a grayscale image


 img = sp.imread("fruits.jpg", mode = "L")  




As previously defined, anisotropic diffusion is based on computing a first order gradient. There are two “normally used” types to compute the first order gradient; forward difference and backward difference. In our implementation of anisotropic diffusion, we use the forward difference gradient operator. The familiar reader would be conversant with the fact that we would obtain the gradient along the x and the y axis of the image. However, a problem manifests. In the computation of the gradient using forward difference, it returns an array that is less one along the axis of computation. Meaning, the x gradient of a 5 by 5 matrix would be 5 by 4. We would analyse and take care of this problem later. However, we zero pad the gradient to maintain the image shape of 5 by 5


 def forwardDifferenceGradient(img):  
   diffY = np.zeros_like(img)  
   diffX = np.zeros_like(img)  
   diffY[:-1, :] = np.diff(img, axis = 0)  
   diffX[:, :-1] = np.diff(img, axis = 1)  
   return diffY, diffX  

Now we define the diffusion equation, as stated in the previous post, we can have a function resembling the sigmoidal function or one stemming from the tanh function. We hence implement both schemes.


 def sigmoidFormula(gradientY, gradientX, k):  
   cGradientY = np.exp(-(gradientY/k) **2.)  
   cGradientX = np.exp(-(gradientX/k) **2.)  
   YVal = cGradientY * gradientY  
   XVal = cGradientX * gradientX  
   return YVal, XVal  

 def tanhFormula(gradientY, gradientX, k):  
   cGradientY = 1./(1. + ((gradientY/k)**2))  
   cGradientX = 1./(1. + ((gradientX/k)**2))  
   YVal = cGradientY * gradientY  
   XVal = cGradientX * gradientX  
   return YVal, XVal  

Anisotropic diffusion is an iterative process, with each iteration working on the previous image. However, to control the way the diffusion at a current iteration affects the whole image, a constant lambda. Such that, the image diffusion at a particular iteration level is multiplied by lambda and added to the image. We set lambda to 0.25


A shift in gradient would gradually shift the edge and if the number of iteration is large enough, it would completely dislocate the image, so we compensate for the shift of the gradient operator by introducing a new variable, to shift the gradient back and hence mitigate any shift in edge over time.

We choose K also to be equal to 20

 img = sp.imread("fruits.jpg", mode = "L")  
 img = img.astype("float32")  
 gauss = gaussian(img, 11)  
 shiftedY = np.zeros_like(img)  
 shiftedX = np.zeros_like(img)  
 for i in range(10):  
   dY, dX = forwardDifferenceGradient(img)  
   cY, cX = sigmoidFormula(dY, dX, 20)  
   shiftedY[:] = cY  
   shiftedX[:] = cX  
   shiftedY[1:,:] -= cY[:-1,:]  
   shiftedX[:,1:] -= cX[:,:-1]  
   img += 0.25*(shiftedY+shiftedX)  
 sv.imsave('anisotropic.jpg', img)  
 sv.imsave('gaussian.jpg', gauss)  


Anisotropic Diffusion (iteration =10, K =20)

Gaussian filtering (sigma=11)



We compare anisotropic diffusion after 10 iterations with Gaussian filtering using a window size of 11 and notice that the Gaussian filter completely blurs out the edges. With gaussian filtering, the edges are completely blurred out and information with the image is almost completely lost. Anisotropic diffusion however, maintains the edge information.

partly derived with reference to this




He is knowledgeable about a lot of things but, he knows nothing.

0 comments :