Merge LBP and HOG feature descriptors

Question:

Am working on age,gender estimation project. So far I have tried with LBP (Local Binary Patterns) + SVM (Support Vector Machines) in order to train it for gender classification but am getting too much false positive while working with LBP+SVM, so I tried with HOG (Histogram of Gradients) + SVM, and surprisingly accurracy increased upto 90%, so I just though of merging the features of both the descriptors and train SVM using that. The code for this is as follows:

    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)

But when tried this line: data.append(fd + hist) simply trying to concat both the feature discriptors, and throws me following error:

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

So can someone point me in order to merge two features into single and then train the SVM for that.

Asked By: U.Swap

||

Answers:

The problem is you are trying to add two arrays of different sizes. One array has 11340 elements and the other one has 26. You should change the logic when storing those values and not adding them together

Answered By: Razvan

I figured out the issue, one can simply stack the numpy array, with any feature descriptor of similar shape, like HOG and LBPH works on grayscale image so in that case depth for features produced by LBP,HOG will always be one, so we can stack both of them using 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()

Now,all of then can be stacked as usual,

feat = np.hstack([desc_hist, hog_hist, td_hist])
Answered By: U.Swap
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.