好的,所以我决定使用定向梯度直方图是一种更好的图像指纹识别方法,而不是创建索博尔衍生物的直方图。我想我终于弄清楚了,但是当我测试我的代码时,我得到以下内容:
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;
}
正如你所看到的,我弄乱了不同的变量以定义尺寸,但无济于事,我将它们评论出来并尝试手动设置它们。依然没有。我究竟做错了什么?任何帮助将不胜感激。
谢谢!
答案 0 :(得分:6)
您正在错误地初始化HOGDescriptor
。
断言表明前三个输入参数中的每一个都必须满足约束条件:
(winSize - blockSize) % blockStride == 0
同时包含height
和width
维度。
问题是winSize.height
不满足此约束,考虑使用以下内容初始化hog
的其他参数:
(412 - 16) % 8 = 4 //Problem!!
最简单的解决方法可能是将窗口尺寸从cv::Size(320,412)
增加到可被8整除的值,可能是cv::Size(320,416)
,但具体尺寸将取决于您的具体要求。请注意断言所说的内容!