Skip to content

Instantly share code, notes, and snippets.

@blackball
Created September 26, 2017 12:40
Show Gist options
  • Save blackball/31031eb9b3d2710f956c3eb27814cd5b to your computer and use it in GitHub Desktop.
Save blackball/31031eb9b3d2710f956c3eb27814cd5b to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import cv2
import numpy as np
def GetNeighbors(r, c, A, B, C):
# 1 2 3
# 4 5 6
# 7 8 9
n = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 0), (0, 1), (1, -1), (1, 0), (1, 1)]
neighbors = []
for p in n:
neighbors.append(A[r + p[0], c + p[1]])
neighbors.append(C[r + p[0], c + p[1]])
if not (p[0] == 0 and p[1] == 0):
neighbors.append(B[r + p[0], c + p[1]])
return neighbors
def IsExtrema(me, neighbors):
return (me > neighbors).all() or (me < neighbors).all()
def FindExtremas(A, B, C):
rows, cols = B.shape[:2]
ret = []
for i in xrange(1, rows-1):
for j in xrange(1, cols-1):
val = B[i, j]
neighbors = GetNeighbors(i, j, A, B, C)
if IsExtrema(val, neighbors):
ret.append((i, j))
return ret
def DoG(I, bigK, smallK):
blurBig = cv2.GaussianBlur(img,(bigK,bigK),0)
blurSamll = cv2.GaussianBlur(img,(smallK,smallK),0)
return blurBig - blurSamll
def main():
I = np.ones((400, 400), dtype = np.float32) * 255
I[20:80, 20:80] = 0 # draw a black rectangle
I[200:280, 200:280] = 0 # draw a black rectangle
cv2.circle(I, (130, 130), 40, (0,0,0), -1)
# create blured image with kernel: 3,5,7,9
L3 = cv2.GaussianBlur(I,(3,3),0)
L5 = cv2.GaussianBlur(I,(5,5),0)
L7 = cv2.GaussianBlur(I,(7,7),0)
L9 = cv2.GaussianBlur(I,(9,9),0)
# construct DoG
L35 = L3 - L5
L57 = L5 - L7
L79 = L7 - L9
extremas = FindExtremas(L35, L57, L79)
bgr = cv2.cvtColor(I, cv2.COLOR_GRAY2BGR)
for p in extremas:
cv2.circle(bgr, (p[1], p[0]), 2, (0,0,255), 2)
cv2.imshow("bgr", bgr)
cv2.waitKey(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment