Skip to content

Instantly share code, notes, and snippets.

@leonidk
Last active March 24, 2024 03:23
Show Gist options
  • Save leonidk/8798fdbf38db120b8536d25ea2f8c3b4 to your computer and use it in GitHub Desktop.
Save leonidk/8798fdbf38db120b8536d25ea2f8c3b4 to your computer and use it in GitHub Desktop.
difference of gaussians example in python
from skimage import data, feature, color, filter, img_as_float
from matplotlib import pyplot as plt
original_image = img_as_float(data.chelsea())
img = color.rgb2gray(original_image)
k = 1.6
plt.subplot(2,3,1)
plt.imshow(original_image)
plt.title('Original Image')
for idx,sigma in enumerate([4.0,8.0,16.0,32.0]):
s1 = filter.gaussian_filter(img,k*sigma)
s2 = filter.gaussian_filter(img,sigma)
# multiply by sigma to get scale invariance
dog = s1 - s2
plt.subplot(2,3,idx+2)
print dog.min(),dog.max()
plt.imshow(dog,cmap='RdBu')
plt.title('DoG with sigma=' + str(sigma) + ', k=' + str(k))
ax = plt.subplot(2,3,6)
blobs_dog = [(x[0],x[1],x[2]) for x in feature.blob_dog(img, min_sigma=4, max_sigma=32,threshold=0.5,overlap=1.0)]
# skimage has a bug in my version where only maxima were returned by the above
blobs_dog += [(x[0],x[1],x[2]) for x in feature.blob_dog(-img, min_sigma=4, max_sigma=32,threshold=0.5,overlap=1.0)]
#remove duplicates
blobs_dog = set(blobs_dog)
img_blobs = color.gray2rgb(img)
for blob in blobs_dog:
y, x, r = blob
c = plt.Circle((x, y), r, color='red', linewidth=2, fill=False)
ax.add_patch(c)
plt.imshow(img_blobs)
plt.title('Detected DoG Maxima')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment