How to use Gabor filters for feature extraction from image?

Akash Chakraborty picture Akash Chakraborty · Jul 11, 2018 · Viewed 8.7k times · Source

I want to apply Gabor filter for feature extraction from image then on the trained data I will be applying NN or SVM.I didn't applied batch processing though but it will be done or if you can help me with the machine learning part it will be great for me.Thank you. Here is my code:

import cv2
import numpy as np
import glob

img=glob.glob("C://Users//USER//Pictures//Saved Pictures//tuhin.jpg")

img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)[1]  
ret, labels = cv2.connectedComponents(img)
label_hue = np.uint8(179*labels/np.max(labels))
blank_ch = 255*np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)
labeled_img[label_hue==0] = 0
cv2.imshow('labeled.png', labeled_img)
cv2.waitKey()

def build_filters():
filters = []
ksize = 31
for theta in np.arange(0, np.pi, np.pi / 16):
    kern = cv2.getGaborKernel((ksize, ksize), 4.0, theta, 10.0, 0.5, 0, 
              ktype=cv2.CV_32F)
    kern /= 1.5*kern.sum()
    filters.append(kern)
    return filters

def process(img, filters):
    accum = np.zeros_like(img)
    for kern in filters:
        fimg = cv2.filter2D(img, cv2.CV_8UC3, kern)
        np.maximum(accum, fimg, accum)
        return accum

filters=build_filters()
res1=process(img,filters)
cv2.imshow('result',res1)
cv2.waitKey(0)
cv2.destroyAllWindows() 

Answer

Sandipan Dey picture Sandipan Dey · Jul 11, 2018

This is a nice tutorial on texture extraction using gabor filter with scikit-image: http://scikit-image.org/docs/0.11.x/auto_examples/plot_gabor.html. You may want to have a look at it.

You may want to use deep learning / transfer learning (depending on how much data you have) to extract the features automatically instead of hand-crafted features.