合并LBP和HOG特征描述符



我正在从事年龄、性别评估项目。到目前为止,我已经尝试了使用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(上次调用):文件"/home/swap/Uubuntu home/swap/openCV/gender_age_weight_recog/tarin.py",

第41行,indata.append(fd+hist)ValueError:操作数无法与形状(11340,)(26,)一起广播

所以有人能告诉我将两个特征合并为一个特征,然后为此训练SVM吗。

我发现了这个问题,可以简单地将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])

但假设有人想合并在3通道上工作的hsv直方图图像(RGB),然后可以将其展平为1D阵列,然后可以堆叠以同时具有该特征。

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])

问题是您试图添加两个不同大小的数组。一个阵列有11340个元件,而另一个阵列则有26个。您应该在存储这些值时更改逻辑,而不是将它们加在一起

最新更新