Skip to content

Instantly share code, notes, and snippets.

@usernaamee
Created June 9, 2017 13:31
Show Gist options
  • Save usernaamee/61398081d805e4e5b0a1a855306342bd to your computer and use it in GitHub Desktop.
Save usernaamee/61398081d805e4e5b0a1a855306342bd to your computer and use it in GitHub Desktop.
wld-feature-extractor
import cv2
import sys
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
def get_wld_feats(gray_img):
f00 =np.array([[1, 1, 1], [1, -8, 1], [1, 1, 1]])
f10 = np.array([[0, -1, 0], [0, 0, 0], [0, 1, 0]])
f11 = np.array([[0, 0, 0], [1, 0, -1], [0, 0, 0]])
f00_img = signal.convolve2d(gray_img, f00, mode='same')
f11_img = signal.convolve2d(gray_img, f11, mode='same')
f10_img = signal.convolve2d(gray_img, f10, mode='same')
chi = np.arctan(f00_img/(gray_img+1e-10))
chi_j = chi.flatten()
theta = np.pi + np.arctan2(f11_img, (f10_img+1e-10))
M = 6
T = 8
S = 4
WLD = np.zeros((chi_j.shape[0], T))
T_intervals = np.linspace(0, 2*np.pi, T+1)
M_intervals = np.linspace(-np.pi/2.0, np.pi/2.0, M+1)
h_tms = np.zeros((T, M, S))
for i in range(T):
t_lo = T_intervals[i]
t_hi = T_intervals[i+1]
for j in range(M):
m_lo = M_intervals[j]
m_hi = M_intervals[j+1]
S_intervals = np.linspace(m_lo, m_hi, S + 1)
for k in range(S):
s_lo = S_intervals[k]
s_hi = S_intervals[k+1]
h_tms[i, j, k] = np.sum((chi >= s_lo) & (chi <= s_hi) & (theta >= t_lo) & (theta <= t_hi))
h = []
h_img = []
for i in range(M):
hm = []
for j in range(T):
l = list(h_tms[j, i, :])
hm += l
h_img.append(hm)
h += hm
return np.array(h)
imgpath = sys.argv[1]
img = cv2.imread(imgpath)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
feats = get_wld_feats(gray_img)
print feats.shape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment