使用HOGDescriptor失败断言

Mic*_*man 1 opencv histogram

好吧,所以我决定使用定向梯度直方图是一种更好的图像指纹识别方法,而不是创建索贝尔衍生物的直方图.我想我终于弄明白了,但是当我测试我的代码时,我得到以下内容:

OpenCV错误:断言失败((winSize.width - blockSize.width)%blockStride.width == 0 &&(winSize.height - blockSize.height)%blockStride.height == 0).

截至目前,我只想弄清楚如何正确计算HOG并查看结果; 但是在视觉上,我只想要一些非常基本的输出来查看是否创建了HOG.然后我会弄清楚如何在图像比较中使用它.

这是我的示例代码:

using namespace cv;
using namespace std;

int main(int argc, const char * argv[])
{
//    Initialize string variables.
string thePath, img, hogSaveFile;
thePath = "/Users/Mikie/Documents/Xcode/images/";
img = thePath + "HDimage.jpg";
hogSaveFile = thePath + "HDimage.yml";
//    Create mats.
Mat src;
//    Load image as grayscale.
src = imread(img, CV_LOAD_IMAGE_GRAYSCALE);
//    Verify source loaded.
if(src.empty()){
    cout << "No image data. \n ";
    return -1;
}else{
    cout << "Image loaded. \n" << "Size: " << src.cols << " X " << src.rows << "." << "\n";

}

//    Initialize float variables.
float imgWidth, imgHeight, newWidth, newHeight;
imgWidth = src.cols;
imgHeight = src.rows;
newWidth = 320;
newHeight = (imgHeight/imgWidth)*newWidth;
Mat dst = Mat::zeros(newHeight, newWidth, CV_8UC3);
resize(src, dst, Size(newWidth, newHeight), CV_INTER_LINEAR);
//    Was resize successful?
if (dst.rows < src.rows && dst.cols < src.cols) {
    cout << "Resize successful. \n" << "New size: " << dst.cols << " X " << dst.rows << "." << "\n";
} else {
    cout << "Resize failed. \n";
    return -1;
}

vector<float>theHOG(Mat dst);{
    if (dst.empty()) {
        cout << "Image lost. \n";
    } else {
        cout << "Setting up HOG. \n";
    }
    imshow("Image", dst);
    bool gammaC = true;
    int nlevels = HOGDescriptor::DEFAULT_NLEVELS;
    Size winS(newWidth, newHeight);
//        int block_size = 16;
//        int block_stride= 8;
//        int cell_size = 8;
    int gbins = 9;
    vector<float> descriptorsValues;
    vector<Point> locations;
    HOGDescriptor hog(Size(320, 412), Size(16, 16), Size(8, 8), Size(8, 8), gbins, -1, HOGDescriptor::L2Hys, 0.2, gammaC, nlevels);
    hog.compute(dst, descriptorsValues, Size(0,0), Size(0,0), locations);
    printf("descriptorsValues.size() = %ld \n", descriptorsValues.size()); //prints 960
    for (int i = 0; i <descriptorsValues.size(); i++) {
        cout << descriptorsValues[i] << endl;
    }
}
cvWaitKey(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我弄乱了不同的变量来定义大小,但无济于事,我将它们评论出来并尝试手动设置它们.依然没有.我究竟做错了什么?任何帮助将不胜感激.

谢谢!

Aur*_*ius 6

您正在初始化HOGDescriptor错误.断言声明前三个输入参数中的每一个都必须满足约束:

(winSize - blockSize) % blockStride == 0
Run Code Online (Sandbox Code Playgroud)

heightwidth维度.

问题是winSize.height不满足此约束,考虑您初始化的其他参数hog:

(412 - 16) % 8 = 4    //Problem!!
Run Code Online (Sandbox Code Playgroud)

可能最简单的解决方法是将窗口尺寸从cv::Size(320,412)可被8整除的东西增加cv::Size(320,416),但具体尺寸将取决于您的具体要求.请注意断言所说的内容!