This commit is contained in:
0x01FE 2024-09-06 14:50:56 -05:00
parent 4ae720bd70
commit f0565b8e95

138
sobel.py
View File

@ -1,30 +1,28 @@
import scipy.ndimage as nd
import imageio.v3 as iio
# import scipy.ndimage as nd
# import imageio.v3 as iio
import numpy as np
import math
from PIL import Image, ImageFilter
IMAGE_PATH = 'engine.png'
IMAGE_PATH = 'sample-images/engine.PNG'
BLUR_STRENGTH_1 = 1
BLUR_STRENGTH_2 = 3
DOG_THRESHOLD = 8
sobel_x_kernel = ImageFilter.Kernel((3, 3),
(
1, 0, -1,
2, 0, -2,
1, 0, -1
))
sobel_y_kernel = ImageFilter.Kernel((3, 3),
(
1, 2, 1,
0, 0, 0,
-1, -2, -1
))
CHARS = [
' ',
'.',
'_',
'+',
'=',
'A',
'K',
'B',
'&',
'#'
]
def main():
@ -35,13 +33,29 @@ def main():
blur_1 = image.filter(ImageFilter.GaussianBlur(BLUR_STRENGTH_1))
blur_2 = image.filter(ImageFilter.GaussianBlur(BLUR_STRENGTH_2))
print(image.size)
dog: Image = difference_of_gaussians(blur_1, blur_2)
sb = sobel(image)
print(dog.size)
sb.save('sobel-pil.png')
# sb = sobel(image)
sb = sobel(dog)
# sb.save('sobel-pil.png')
sb, gradient = sobel(dog)
w, h = sb.size
print(w * h)
print(sb.size)
print(len(gradient))
exit()
for y in range(0, h):
for x in range(0, w):
gradient_v = gradient[y * (w - 1) + x]
print(match_gradient(gradient_v))
sb.save('sobel-dog.png')
@ -101,22 +115,23 @@ def difference_of_gaussians(blur_1: Image, blur_2: Image) -> Image:
# Taken from
# https://enzoftware.github.io/posts/image-filter-python
def sobel(img: Image) -> Image:
def sobel(img: Image) -> tuple[Image.Image, list]:
if img.mode == 'L':
return sobel_L(img)
width, height = img.size
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)
gradient = []
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]
r, g, b = img.getpixel((x - 1, y - 1))
# intensity ranges from 0 to 765 (255 * 3)
intensity = r + g + b
@ -126,56 +141,35 @@ def sobel(img: Image) -> Image:
Gy += -intensity
# remaining left column
p = img.getpixel((x-1, y))
r = p[0]
g = p[1]
b = p[2]
r, g, b = img.getpixel((x-1, y))
Gx += -2 * (r + g + b)
p = img.getpixel((x-1, y+1))
r = p[0]
g = p[1]
b = p[2]
r, g, b = img.getpixel((x-1, y+1))
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]
r, g, b = img.getpixel((x, y-1))
Gy += -2 * (r + g + b)
p = img.getpixel((x, y+1))
r = p[0]
g = p[1]
b = p[2]
r, g, b = img.getpixel((x, y+1))
Gy += 2 * (r + g + b)
# right column
p = img.getpixel((x+1, y-1))
r = p[0]
g = p[1]
b = p[2]
r, g, b = img.getpixel((x+1, y-1))
Gx += (r + g + b)
Gy += -(r + g + b)
p = img.getpixel((x+1, y))
r = p[0]
g = p[1]
b = p[2]
r, g, b = img.getpixel((x+1, y))
Gx += 2 * (r + g + b)
p = img.getpixel((x+1, y+1))
r = p[0]
g = p[1]
b = p[2]
r, g, b = img.getpixel((x+1, y+1))
Gx += (r + g + b)
Gy += (r + g + b)
@ -187,12 +181,44 @@ def sobel(img: Image) -> Image:
length = length / 4328 * 255
length = int(length)
gradient_v = math.atan2(Gy, Gx)
# print(gradient_v)
# draw the length in the edge image
#newpixel = img.putpixel((length,length,length))
newimg.putpixel((x,y),(length,length,length))
gradient.append(gradient_v)
return (newimg, gradient)
def sobel_L(img: Image) -> tuple[Image.Image, list]:
pass
# TODO @0x01FE : refactor plz increment by 30 & 60
def match_gradient(n: float) -> str:
n = abs(n)
if n < math.radians(30):
return '-'
elif n < math.radians(60):
return '/'
elif n < math.radians(120):
return '|'
elif n < math.radians(150):
return '\\'
elif n < math.radians(210):
return '-'
elif n < math.radians(240):
return '/'
elif n < math.radians(300):
return '|'
elif n < math.radians(330):
return '\\'
else:
return '-'
return newimg
if __name__ == '__main__':
main()