Reputation: 49
I'm trying to train a SVM classifier to recognize pedestrians in a set of 64x128 images. I've already done that using HOG features, and now I need to implement the same thing using SIFT and ORB. For HOG features, I had always the same number of features (3780), so that the matrix for the train was image_number by 3780. Now, using SIFT extractor I get keypoints of different sizes. How can I create a matrix for the classifier using these keypoints of different sizes?
Many thanks for your help!
I solved the descriptors' issue putting all of them in the same row. However, I found out that most of desriptors have a 0 value, so the classifier doesn't work well. Do you know how can I solve this problem?
This is a piece of the code:
DenseFeatureDetector detector;
SiftDescriptorExtractor descriptor;
vector<KeyPoint> keypoints;
//for every image I compute te SIFT
detector.detect(image, keypoints);
Mat desc;
descriptor.compute(image,keypoints, desc);
Mat v(1,30976,CV_32FC1);
for (int j = 0; j<desc.rows; j++){
for(int k = 0; k<desc.cols; k++){
v.at<float>(0,128*j+k) = desc.at<float>(j,k);
}
} //now in vector v there are all the descriptors (the problem is that most of them have 0 value)
descriptormat.push_back(v); //descriptormat is the cv::Mat that I use to train the SVM
Upvotes: 4
Views: 10148
Reputation:
Usually, people vector-quantize SIFT or ORB features and build histograms (bags-of-words model). This would give you a fixed size vector for every training and testing image.
Upvotes: 5
Reputation: 3988
You can create a big matrix and push_back the descriptors computed for each image. Example (not checked)
int main(int argc, char**argv)
{
cv::SIFT sift;
cv::Mat dataMatrix(0, 128, CV_32F); // 0 rows, 128 cols is SIFT dimension, I think there is a method that gives you the descriptor dimension exactly. type is 32F if I remember well, must check
for (int i = 1; i < argc; ++i) {
cv::Mat img = cv::imread(argv[i]);
std::vector<cv::KeyPoints> kp;
cv::Mat desc;
sift(img, cv::noArray(), keypoints, desc);
dataMatrix.push_back(desc);
}
// Now train SVM with dataMatrix
assert(dataMatrix.rows > 0);
}
Upvotes: 1