Skip to content

Instantly share code, notes, and snippets.

@enzoftware
Last active October 5, 2021 19:03
Show Gist options
  • Save enzoftware/3a36fe7a6b801156a12de2b2d8cebc7a to your computer and use it in GitHub Desktop.
Save enzoftware/3a36fe7a6b801156a12de2b2d8cebc7a to your computer and use it in GitHub Desktop.
Sobel filter in Python for edge detection 🐍
from PIL import Image
import math
path = "peru.jpeg" # Your image path
img = Image.open(path)
newimg = Image.new("RGB", (width, height), "white")
for x in range(1, width-1): # ignore the edge pixels for simplicity (1 to width-1)
for y in range(1, height-1): # ignore edge pixels for simplicity (1 to height-1)
# initialise Gx to 0 and Gy to 0 for every pixel
Gx = 0
Gy = 0
# top left pixel
p = img.getpixel((x-1, y-1))
r = p[0]
g = p[1]
b = p[2]
# intensity ranges from 0 to 765 (255 * 3)
intensity = r + g + b
# accumulate the value into Gx, and Gy
Gx += -intensity
Gy += -intensity
# remaining left column
p = img.getpixel((x-1, y))
r = p[0]
g = p[1]
b = p[2]
Gx += -2 * (r + g + b)
p = img.getpixel((x-1, y+1))
r = p[0]
g = p[1]
b = p[2]
Gx += -(r + g + b)
Gy += (r + g + b)
# middle pixels
p = img.getpixel((x, y-1))
r = p[0]
g = p[1]
b = p[2]
Gy += -2 * (r + g + b)
p = img.getpixel((x, y+1))
r = p[0]
g = p[1]
b = p[2]
Gy += 2 * (r + g + b)
# right column
p = img.getpixel((x+1, y-1))
r = p[0]
g = p[1]
b = p[2]
Gx += (r + g + b)
Gy += -(r + g + b)
p = img.getpixel((x+1, y))
r = p[0]
g = p[1]
b = p[2]
Gx += 2 * (r + g + b)
p = img.getpixel((x+1, y+1))
r = p[0]
g = p[1]
b = p[2]
Gx += (r + g + b)
Gy += (r + g + b)
# calculate the length of the gradient (Pythagorean theorem)
length = math.sqrt((Gx * Gx) + (Gy * Gy))
# normalise the length of gradient to the range 0 to 255
length = length / 4328 * 255
length = int(length)
# draw the length in the edge image
#newpixel = img.putpixel((length,length,length))
newimg.putpixel((x,y),(length,length,length))
@danilo-bc
Copy link

Hi, I'm looking to do something similar to this and your gist happened to pop up in google search. The only line I don't understand is line 85. Where does 4238 come from? Something to do with image size? Instead of length, I'd call it either magnitude or module (as far as I understand).

Also, is this the recommended way to operate in RGB images? I've seen a lot of material on Greyscale, but none yet on any color space.

Thanks for sharing.

@rafaeldjsm
Copy link

@danilo-bc this is because to normalize with the max value of G = (sum(GxGx) + sum(GyGy))**0.5 is where C = [765,765,765], G = array([4327.49350086])

ar1 = np.matrix([[-1,0,1],[-2,0,2],[-1,0,1]])

ar2 = np.matrix([[1,2,1],[0,0,0],[-1,-2,-1]])

Gx = np.array(ar1C)
Gy = np.array(ar2
C)
G = (sum(GxGx) + sum(GyGy))**0.5

@danilo-bc
Copy link

Thanks @rafaeldjsm, after these years I got a better understanding of the issue, but it's great that you've come to solve that mystery :)

That said, the original author could append your explanation to the gist, or at least have the value be precalculated with your steps.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment