简体   繁体   English

如何使用 Gabor 滤波器从图像中提取特征?

[英]How to use Gabor filters for feature extraction from image?

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.我想应用 Gabor 滤波器从图像中提取特征,然后在训练数据上应用 NN 或 SVM。虽然我没有应用批处理,但它会完成,或者如果你能帮助我完成机器学习部分,它会对我很好。谢谢。 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() 

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 .这是使用带有 scikit-image 的 gabor 过滤器进行纹理提取的不错教程: 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.您可能希望使用深度学习/迁移学习(取决于您拥有多少数据)来自动提取特征而不是手工制作的特征。

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.我可以通过改变参数,例如频率和方向的 theta,lamda 来定义更多的核。我可以生成 Gabor 滤波器组,然后我将应用各种机器学习算法进行分类。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM