Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
     }
share|improve this question
    
Which line of this loop is the slowest? compute? – Piotr Aleksander Chmielowski Mar 17 '16 at 9:07
    
And two questions: 1. which version of CV are you using? 2. is this function parallelized? – Piotr Aleksander Chmielowski Mar 17 '16 at 9:09
    
@PiotrAleksanderChmielowski, thank you so much for the reply. Apparently, the compute is the problem, because the loop itself is not slow in this case. I'm using CV 3.0. Regarding the parallelization, I have to admit that I haven't applied it. However, due to the fact that I have dual-core processor, this process, I think, would be performed 2 times faster, but it is also not effective in my case at all. – Federico Mar 17 '16 at 9:34
2  
I don't want to be negative here. However, let me note that just because C++ is considered one of the fastest languages out there does not mean that code that is written in C++ is automatically faster than code in other languages. A fair amount of time and expertise is required to optimize code to run (substantially) faster and since you said that you are unfamiliar with C++, chances are slim that you will outperform an implementation that has likely been written by experts and optimized for a long time, even if it was done in a "less performant" language. – Nobody Mar 17 '16 at 9:35
    
@Federico - Now I don't understand: so the loop is slow or fast? If you want a help, you have to specify what is the problem. So I'm repeating my question: What line of your code is slower than you expect? – Piotr Aleksander Chmielowski Mar 17 '16 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.