【发布时间】:2013-07-08 20:07:33
【问题描述】:
好的,所以我决定使用定向梯度直方图是一种更好的图像指纹识别方法,而不是创建 sobel 导数的直方图。我想我终于弄清楚了,但是当我测试我的代码时,我得到了以下信息:
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;
}
如您所见,我用不同的变量来定义大小但无济于事,所以我将它们注释掉并尝试手动设置它们。依然没有。我究竟做错了什么?任何帮助将不胜感激。
谢谢!
【问题讨论】:
-
断言发生在哪一行?
-
它说它发生在 hog.cpp 的第 65 行,但我发现上面的实际代码中没有提到哪一行,但我认为这是我定义 HOGDescriptor 的第 68 行。
-
找到断言所在的行应该是您尝试的第一件事。断言发生时你能看到调用堆栈吗?
-
好的,我正在使用 xCode,它看起来对调用堆栈不友好,但我想我现在已经正确设置了它。错误似乎发生在第 77 行或第 78 行。这里是 77:
HOGDescriptor hog(Size(320, 412), Size(16, 16), Size(8, 8), Size(8, 8), gbins, -1, HOGDescriptor::L2Hys, 0.2, gammaC, nlevels);和 78:hog.compute(dst, descriptorsValues, Size(0,0), Size(0,0), locations);。 -
这也是我发现的。 :) 你看到我的回答了吗?