gpt4 book ai didi

python - 合并 LBP 和 HOG 特征描述符

转载 作者:太空宇宙 更新时间:2023-11-03 21:25:43 27 4
gpt4 key购买 nike

我正在从事年龄、性别估计项目。到目前为止,我已经尝试使用 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。

最佳答案

我想通了这个问题,可以简单地堆叠 numpy 数组,具有任何相似形状的特征描述符,例如 HOG 和 LBPH 适用于灰度图像,因此在这种情况下 LBP 产生的特征深度,HOG 将始终成为一个,所以我们可以使用 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()

现在,所有这些都可以像往常一样堆叠

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

关于python - 合并 LBP 和 HOG 特征描述符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41457140/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com