title: python - Implementing Sobel operators with python without opencv
body: <p>Given a greyscale 8 bit image (2D array with values from 0 - 255 for pixel intensity), I want to implement the  Sobel operators (mask) on an image.  The Sobel function below basically loops around a given pixel,applies the following weight to the pixels:
  <a href="https://i.stack.imgur.com/1N67K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1N67K.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/Ut0Aq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ut0Aq.png" alt="enter image description here" /></a></p>
  <p>And then aplies the given formula:</p>
  <p><a href="https://i.stack.imgur.com/aBBUL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aBBUL.png" alt="enter image description here" /></a></p>
  <p>Im trying to implement the formulas from this link:
  <a href="http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm" rel="nofollow noreferrer">http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm</a></p>
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import matplotlib.image as mpimg
  import Image
  
  
  def Sobel(arr,rstart, cstart,masksize, divisor):
    sum = 0;
    x = 0
    y = 0
  
    for i in range(rstart, rstart+masksize, 1):
      x = 0
      for j in range(cstart, cstart+masksize, 1):
          if x == 0 and y == 0:
              p1 = arr[i][j]
          if x == 0 and y == 1:
              p2 = arr[i][j]
          if x == 0 and y == 2:
              p3 = arr[i][j]
          if x == 1 and y == 0:
              p4 = arr[i][j]
          if x == 1 and y == 1:
              p5 = arr[i][j]           
          if x == 1 and y == 2:
              p6 = arr[i][j]
          if x == 2 and y == 0:
              p7 = arr[i][j]
          if x == 2 and y == 1:
              p8 = arr[i][j]
          if x == 2 and y == 2:
              p9 = arr[i][j]
          x +=1
      y +=1
    return np.abs((p1 + 2*p2 + p3) - (p7 + 2*p8+p9)) + np.abs((p3 + 2*p6 + p9) - (p1 + 2*p4 +p7)) 
  
  
  def padwithzeros(vector, pad_width, iaxis, kwargs):
     vector[:pad_width[0]] = 0
     vector[-pad_width[1]:] = 0
     return vector
  
  im = Image.open('charlie.jpg')
  im.show()
  img = np.asarray(im)
  img.flags.writeable = True
  p = 1
  k = 2
  m = img.shape[0]
  n = img.shape[1]
  masksize = 3
  img = np.lib.pad(img, p, padwithzeros) #this function padds image with zeros to cater for pixels on the border.
  x = 0
  y = 0
  for row in img:
    y = 0
    for col in row:
      if not (x &lt; p or y &lt; p or y &gt; (n-k) or x &gt; (m-k)):
          img[x][y] = Sobel(img, x-p,y-p,masksize,masksize*masksize)
      y = y + 1
    x = x + 1
  
  img2 = Image.fromarray(img)
  img2.show()
  </code></pre>
  <p>Given this greyscale 8 bit image</p>
  <p><a href="https://i.stack.imgur.com/8zINU.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8zINU.gif" alt="enter image description here" /></a></p>
  <p>I get this when applying the function:</p>
  <p><a href="https://i.stack.imgur.com/MPM6y.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MPM6y.png" alt="enter image description here" /></a></p>
  <p>but should get this:</p>
  <p><a href="https://i.stack.imgur.com/ECAIK.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ECAIK.gif" alt="enter image description here" /></a></p>
  <p>I have implemented other gaussian filters with python, I'm not sure where I'm going wrong here?</p>