title: Convolving sobel operator in x direction in frequency domain
body: <p>I implemented the code <a href="https://stackoverflow.com/a/54977551/7328782">given by Cris Luengo for convolution in frequency in domain</a>, however I'm not getting the intended gradient image in x direction.</p>
  
  <p>Image without flipping the kernel in x and y direction:</p>
  
  <p><img src="https://i.stack.imgur.com/JjIjB.png" alt="Image with normal kernel"></p>
  
  <p>Image after flipping the kernel:</p>
  
  <p><img src="https://i.stack.imgur.com/1K2Aq.png" alt="enter image description here"></p>
  
  <p>If you notice, the second image is same as given by <code>ImageKernel</code> filter from the pillow library. Also, one thing to notice is I don't have to flip the kernel if I apply Sobel kernel in y direction, I get the exactly intended image.</p>
  
  <p>This is my code:</p>
  
  <pre><code>import numpy as np
  from scipy import misc
  from scipy import fftpack
  import matplotlib.pyplot as plt
  from PIL import Image,ImageDraw,ImageOps,ImageFilter
  from pylab import figure, title, imshow, hist, grid,show
  
  im1=Image.open("astronaut.png").convert('L') 
  # im1=ImageOps.grayscale(im1)
  img=np.array(im1)
  
  # kernel = np.ones((3,3)) / 9
  # kernel=np.array([[0,-1,0],[-1,4,-1],[0,-1,0]])
  kernel=np.array([[-1,0,1],[-2,0,2],[-1,0,1]])
  kernel=np.rot90(kernel,2)
  print(kernel)
  
  sz = (img.shape[0] - kernel.shape[0], img.shape[1] - kernel.shape[1])  # total 
  amount of padding
  kernel = np.pad(kernel, (((sz[0]+1)//2, sz[0]//2), ((sz[1]+1)//2, sz[1]//2)), 
  'constant')
  
  kernel = fftpack.ifftshift(kernel)
  
  filtered = np.real(fftpack.ifft2(fftpack.fft2(img) * 
  fftpack.fft2(kernel)))+np.imag(fftpack.ifft2(fftpack.fft2(img) * 
  fftpack.fft2(kernel)))
  filtered=np.maximum(0,np.minimum(filtered,255))
  im2=Image.open("astronaut.png").convert('L')
  
  u=im2.filter(ImageFilter.Kernel((3,3), [-1,0,1,-2,0,2,-1,0,1], 
  scale=1, offset=0))
  
  fig2=figure()
  
  ax1 = fig2.add_subplot(221)  
  ax2 = fig2.add_subplot(222)
  ax3 = fig2.add_subplot(223)
  
  ax1.title.set_text('Original Image')
  ax2.title.set_text('After convolving in freq domain')
  ax3.title.set_text('imagefilter conv')
  ax1.imshow(img,cmap='gray')
  ax2.imshow(filtered,cmap='gray')
  ax3.imshow(np.array(u),cmap='gray')
  
  show()
  </code></pre>