Gabor特征提取和SVM

时间:2018-07-17 10:03:16

标签: python svm gabor-filter

我想从经过Gabor滤波的图像中提取Gabor特征,然后应用SVM进行分类。我的最终目标是我要分离文本和图形。我想使用Gabor特征向量,例如Local Energy,Mean,Amplitude或相位振幅,方差等。尽管在这里,我使用Haralick功能进行分类。基本上,“ clf_svm.fit(train_features,train_labels)”显示了错误。 这是我的代码:

import cv2
import os
import glob
import numpy as np
from skimage import io
from sklearn.svm import LinearSVC
import mahotas as mt


img_dir = "C://Users//USER//Pictures//Saved Pictures//testing" 
data_path = os.path.join(img_dir,'*g')
files = glob.glob(data_path)
data = []
num=0
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()
    path = "H://summerinternship//train"
    path=os.path.normpath(path)
    path=os.path.join(path,'pic'+str(num)+'.png')
    print (path)
    cv2.imwrite(path,res1)
    num=num+1

def extract_features(image):
    # calculate haralick texture features for 4 types of adjacency
    textures = mt.features.haralick(image)

    # take the mean of it and return it
    ht_mean  = textures.mean(axis=0)
    return ht_mean

train_path  = "H://summerinternship//train"
train_names = os.listdir(train_path)

train_features = [[]]
train_labels   = [[]]
i = 1
print ("[STATUS] Started extracting haralick textures..")
for train_name in train_names:
    cur_path = train_path + "/" + train_name
    cur_label = train_name
    i = 1

    for file in glob.glob(cur_path + "/*.png"):
        print ("Processing Image - {} in {}".format(i, cur_label))
        # read the training image
        image = cv2.imread(file)

        # convert the image to grayscale
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # extract haralick texture from the image
        features = extract_features(gray)

        # append the feature vector and label
        train_features.append(features)
        train_labels.append(cur_label)

        # show loop update
        i += 1

# have a look at the size of our feature vector and labels
print ("Training features: {}".format(np.array(train_features).shape))
print ("Training labels: {}".format(np.array(train_labels).shape))

# create the classifier
print ("[STATUS] Creating the classifier..")
clf_svm = LinearSVC(random_state=9)

# fit the training data and labels
print ("[STATUS] Fitting data/label to model..")
clf_svm.fit(train_features, train_labels)

# loop over the test images
test_path = "H://summerinternship//test"
for file in glob.glob(test_path + "/*.png"):
    # read the input image
    image = cv2.imread(file)

    # convert to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # extract haralick texture from the image
    features = extract_features(gray)

    # evaluate the model and predict label
    prediction = clf_svm.predict(features.reshape(1, -1))[0]

    # show the label
    cv2.putText(image, prediction, (20,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0,255,255), 3)
    print ("Prediction - {}".format(prediction))

    # display the output image
    cv2.imshow("Test_Image", image)
    cv2.waitKey(0)

但是我遇到这样的错误:

ValueError: Found array with 0 feature(s) (shape=(1, 0)) while a minimum of 1 is required.

0 个答案:

没有答案