Since Matlab implementation of HOG+SVM object detection algorithm was too slow, I decided to make use of the speed of C++ and re-implemented the algorithm in it. However, I'm more or less unfamiliar with this powerful programming language.
Anyway, at the first stage I'm going to extract the features from a set of images, each of which is of size 40*40 pixels. These images are cropped from a larger image (which I call it the base image) through a for-loop
. This loop is very fast in Matlab, and I expected it to be much faster in C++, but it is too slow and takes 30 seconds to be finished.
The following code shows the part that is very slow and I would be grateful if you first tell me where I have made a mistake (why it is too slow), and then please make your suggestions to run this part as fast as possible.
Mat img = imread("BaseImage.jpg"); // The base image
HOGDescriptor hog;
vector<float> hogvect;
vector<Point> locs;
Mat features;
vector<Mat> StoreFeatures;
for (int i = 0; i < img.rows - 40; ++i)
{
for (int j = 0; j < img.cols - 40; ++j)
{
hog.cellSize = Size(2, 2);
hog.blockSize = Size(2,2);
hog.blockStride = Size(1, 1);
hog.winSize = Size (40, 40);
hog.compute(img(Rect(j, i, 40, 40)), hogvect, Size(4, 4), Size(0, 0), locs); // Here, at each loop, a 40 * 40 pixels image is cropped from the base image, and its features are extracted through HOG.
features.create(hogvect.size(), 1, CV_32FC1);
for (int c = 0; c < hogvect.size(); ++c)
{
*features.ptr<float>(c) = hogvect.at(c);
}
StoringFeatures.push_back(features); // A vector to store the features
}
}
I'm using CV 3.0.
Edit:
There are two main reasons causing the slowness of the code:
1: The hog.compute
function.
2: The following for-loop
:
for (int c = 0; c < hogvect.size(); ++c)
{
*features.ptr<float>(c) = hogvect.at(c);
}
compute
? – Piotr Aleksander Chmielowski Mar 17 '16 at 9:07