1
votes

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() 
2

2 Answers

2
votes

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.

2
votes

I can define more kernels just by changing the parameters such as theta,lamda that is frequency and orientation.I can generate Gabor filter bank then I will apply various machine learning algorithm for classification.

code after batch processing:

import cv2
import os
import glob
import numpy as np


img_dir = "C://Users//USER//Pictures//Saved Pictures" 
data_path = os.path.join(img_dir,'*g')
files = glob.glob(data_path)
data = []
for f1 in files:
    img = cv2.imread(f1,0)
    data.append(img)
    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()
    cv2.imwrite("checking.tif",res1)