This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def hysteresis(img, weak, strong=255): | |
M, N = img.shape | |
for i in range(1, M-1): | |
for j in range(1, N-1): | |
if (img[i,j] == weak): | |
try: | |
if ((img[i+1, j-1] == strong) or (img[i+1, j] == strong) or (img[i+1, j+1] == strong) | |
or (img[i, j-1] == strong) or (img[i, j+1] == strong) | |
or (img[i-1, j-1] == strong) or (img[i-1, j] == strong) or (img[i-1, j+1] == strong)): | |
img[i, j] = strong |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def threshold(img, lowThresholdRatio=0.05, highThresholdRatio=0.09): | |
highThreshold = img.max() * highThresholdRatio; | |
lowThreshold = highThreshold * lowThresholdRatio; | |
M, N = img.shape | |
res = np.zeros((M,N), dtype=np.int32) | |
weak = np.int32(25) | |
strong = np.int32(255) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
def gaussian_kernel(size, sigma=1): | |
size = int(size) // 2 | |
x, y = np.mgrid[-size:size+1, -size:size+1] | |
normal = 1 / (2.0 * np.pi * sigma**2) | |
g = np.exp(-((x**2 + y**2) / (2.0*sigma**2))) * normal | |
return g |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy import ndimage | |
def sobel_filters(img): | |
Kx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], np.float32) | |
Ky = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]], np.float32) | |
Ix = ndimage.filters.convolve(img, Kx) | |
Iy = ndimage.filters.convolve(img, Ky) | |
G = np.hypot(Ix, Iy) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def non_max_suppression(img, D): | |
M, N = img.shape | |
Z = np.zeros((M,N), dtype=np.int32) | |
angle = D * 180. / np.pi | |
angle[angle < 0] += 180 | |
for i in range(1,M-1): | |
for j in range(1,N-1): | |
try: |