1

I am using Visual Studio 2010, with OpenCV 3.0.

The following code tries to extract the HOG features in order to train a SVM classificator. However, when I try to use the HOG's function "compute" in the line

hog.compute(grayImg,descriptor,Size(),Size(),positions);

The following error appears:

Unhandled exception in 0x000007fefd9bb16d (KernelBase.dll) in TrainSVM.exe: Exception de Microsoft C++: cv::Exception at memory location 0x0026e1b0.

            String imagesPath = "Positivas/*.jpg"; 
            vector<String> fn;
            glob(imagesPath, fn, true); // recursive, if you want
            for (size_t i=0; i<fn.size(); i++)
            {
                Mat img = imread(fn[i]);     
                std::vector<cv::Point> positions;
                positions.push_back(cv::Point(0,0));
                std::vector<float> descriptor;
                cv::Mat grayImg;//(patchHeight,patchWidth,CV_8UC1,0);
                cvtColor( img, grayImg, COLOR_BGR2GRAY );
                hog.compute(grayImg,descriptor,Size(),Size(),positions);

                Mat auxDescriptor = cv::Mat(descriptor);
                Mat descriptorMat(1,auxDescriptor.rows,CV_32FC1);
                transpose(auxDescriptor, descriptorMat);
                trainingData.push_back(descriptorMat);
                trainingLabels.push_back(labelPositive);

            }

Any thoughts on this? Thanks in advance!

JoseleMG
  • 302
  • 5
  • 18
  • It's probably an assertion in OpenCV code that is failing. I don't use VS but if you can get it to show you the message associated with the exception that will help you identify the issue. – SSteve Jan 20 '16 at 15:05
  • @SSteve I have done what you suggest and I have obtained the next message: OpenCV Error: Assertion failed ((n & (n - 1)) == 0) in cv::alignSize, file C:\builds\master_PackSlave-win64-vc12-shared\opencv\modules\core\include\opencv2/core /utility.hpp, line 347 – JoseleMG Jan 21 '16 at 17:18
  • Ok, so you know there's a problem with a call to `alignSize`. Looking at the documentation for `alignSize`, it says that n must be a power of two. If VS will give you a stack trace with the exception, you can find out what's calling `alignSize` and why it's doing it with something that isn't a power of two. Or look at the documentation for `hog.compute` and see if there's a requirement that one of the parameters you pass must have a size that's a power of two. – SSteve Jan 21 '16 at 17:35
  • Thank you, you were right! I was using a window which have a size that it wasn't a power of two ;) – JoseleMG Jan 22 '16 at 10:15
  • Great! I posted this as an answer. – SSteve Jan 22 '16 at 19:18
  • @SSteve The error has appeared again :( I have described it [here](http://stackoverflow.com/questions/34951872/train-svm-and-save-it-with-opencv-3-0) and the message that appears now is: OpenCV Error: Parsing error (SVM model data is invalid, check sv_count, var_* an d class_count tags) in cv::ml::SVMImpl::write, file C:\builds\master_PackSlave-w in64-vc12-shared\opencv\modules\ml\src\svm.cpp, line 2027 – JoseleMG Jan 23 '16 at 09:22
  • That's a completely different error. – SSteve Jan 23 '16 at 17:21

1 Answers1

2

After getting the exception message we found there was an assertion failing in the call to alignSize. The documentation for alignSize requires a parameter to be a power of two. From there, we looked at the documentation for hog.compute and found that it requires a window size that is a power of two.

SSteve
  • 10,550
  • 5
  • 46
  • 72