合并 LBP 和 HOG 特征描述符

Merge LBP and HOG feature descriptors

正在从事年龄、性别估计项目。到目前为止,我已经尝试使用 LBP(局部二进制模式)+ SVM(支持向量机)来训练它进行性别分类,但是在使用 LBP + SVM 时出现太多误报,所以我尝试使用 HOG(梯度直方图) + SVM,令人惊讶的是准确率提高了 90%,所以我只是想合并描述符的特征并使用它训练 SVM。代码如下:

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    fd = hog(gray, orientations, pixels_per_cell, cells_per_block, visualize, normalize) #HOG descriptor here.

    hist = desc.describe(gray) #get the LBP histogram here.

    # extract the label from the image path, then update the
    # label and data lists
    labels.append(imagePath.split("/")[-2])
    data.append(fd + hist) # tried concatinate both featurs, but gives error on this line.

# train a Linear SVM on the data
model = LinearSVC(C=100.0, random_state=42)
model.fit(data, labels)

但是当尝试这一行时:data.append(fd + hist) 只是试图连接两个特征描述符,并抛出以下错误:

Traceback (most recent call last): File "/home/swap/Ubuntu-Home/swap/openCV/gender_age_weight_recog/tarin.py",

line 41, in data.append(fd+hist ) ValueError: operands could not be broadcast together with shapes (11340,) (26,)

有人可以指点我将两个特征合并为一个特征,然后为此训练 SVM。

问题是您正在尝试添加两个不同大小的数组。一个数组有 11340 个元素,另一个数组有 26 个。存储这些值时应更改逻辑,而不是将它们相加

我想通了这个问题,可以简单地堆叠 numpy 数组,具有任何相似形状的特征描述符,如 HOG 和 LBPH 适用于灰度图像,因此在这种情况下 LBP,HOG will always be one 产生的特征的深度,所以我们可以使用 numpy 堆叠它们,

    desc_hist = desc.describe(gray_img)
    hog_hist = hog(gray_img, orientations, pixels_per_cell, cells_per_block, 'L1', visualize, normalize)      
    feat = np.hstack([desc_hist, hog_hist])

but suppose one wants to merge hsv histogram which works on 3 channel image(RGB), then it can be flattened to 1D array and then can be stacked to posses that feature as well.

hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hist = cv2.calcHist([hsv], [0, 1, 2], None, bins,
                    [0, 180, 0, 256, 0, 256])

hist = cv2.normalize(hist)

# return the flattened histogram as the feature vector
td_hist = hist.flatten()

现在,所有的都可以stacked像往常一样,

feat = np.hstack([desc_hist, hog_hist, td_hist])