gpt4 book ai didi

opencv - 使用 HOGDescriptor 的断言失败

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:35 25 4
gpt4 key购买 nike

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

OpenCV Error: Assertion failed ((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;
}

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

谢谢!

最佳答案

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

(winSize - blockSize) % blockStride == 0

heightwidth 维度上。

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

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

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

关于opencv - 使用 HOGDescriptor 的断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17535096/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com