1

好的,所以我决定使用定向梯度直方图是一种更好的图像指纹识别方法,而不是创建索贝尔导数的直方图。我想我终于弄清楚了,但是当我测试我的代码时,我得到以下信息:

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;
}

如您所见,我用不同的变量来定义大小但无济于事,所以我将它们注释掉并尝试手动设置它们。依然没有。我究竟做错了什么?任何帮助将不胜感激。

谢谢!

4

1 回答 1

9

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

(winSize - blockSize) % blockStride == 0

在两个维度上heightwidth

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

(412 - 16) % 8 = 4    //Problem!!

可能最简单的解决方法是将窗口尺寸从cv::Size(320,412)可被 8 整除的数值增加cv::Size(320,416),但具体尺寸取决于您的具体要求。只需注意断言在说什么!

于 2013-07-08T22:21:46.007 回答