定向梯度直方图
Histogram of oriented gradiants
本文关键字:直方图 更新时间:2023-10-16
对于一个项目,我正在编写一些代码来计算一些图像的HoG,但在使用atan2函数时,我的方向仅在0~90度之间。我猜这个问题是由于OpenCV的filter2D函数引起的,但我不确定这是原因还是我做错了什么:
Vector<Vector<Mat_<float>>> HoG(Mat image) {
Mat img_x;
Mat img_y;
IplImage img = image;
Mat kern_x = (Mat_<char>(1, 3) << -1, 0, 1);
Mat kern_y = (Mat_<char>(3, 1) << -1, 0, 1);
filter2D(image, img_x, image.depth(), kern_x);
filter2D(image, img_y, image.depth(), kern_y);
Vector<Vector<Mat_<float>>> histograms;
for(int y = 0; y < image.rows - size; y += size) {
Vector<Mat_<float>> temp_hist;
for(int x = 0; x < image.cols - size; x += size) {
float total_mag = 0;
Mat hist = Mat::zeros(1, 8, CV_32FC1);
for(int i = y; i < y + size; ++i) {
for(int j = x; j < x + size; ++j) {
float grad_x = (float)img_x.at<uchar>(i, j);
float grad_y = (float)img_y.at<uchar>(i, j);
double ori = myatan2(grad_x, grad_y);
float mag = sqrt(pow(grad_x, 2) + pow(grad_y, 2));
int bin = round(ori/45);
hist.at<float>(0, (bin - 1 < 0 ? 7 : bin - 1)) += - (float)(ori - ((round(ori/45) - 1) * 45.0 + 22.5)) / 45.0f;
hist.at<float>(0, bin) += -(float)(ori - ((round(ori/45) - 1) * 45.0 + 22.5)) / 45.0f;
total_mag += mag;
}
}
// Normalize the histogram
for(int i = 0; i < 8; ++i) {
hist.at<float>(0, i) = hist.at<float>(0, i) / total_mag;
}
temp_hist.push_back(hist);
}
histograms.push_back(temp_hist);
}
return histograms;
}
如果你有任何其他技巧来提高我的代码或其他东西的速度,这当然也是受欢迎的。
我注意到了这一点:
float grad_x = (float)img_x.at<uchar>(i, j);
float grad_y = (float)img_y.at<uchar>(i, j);
您似乎在使用uchar
。这不应该是char
吗?
相关文章:
- 如何声明一个标准::提升直方图的向量?提升直方图的类型是什么?
- 二进制图像的空间直方图
- 如何在c++中仅使用for循环打印此代码的垂直直方图
- 创建直方图以显示每个类别/箱中带有星号的项目数量
- 打印带C++的直方图
- 将直方图的 2D 数组传递给函数 (C++)?
- 如何在OpenCV 3.4.7中计算CV_32FC1 cv::GpuMat图像的(范围)直方图
- 如何平行化直方图添加?
- 根C++直方图中的错误
- 动态分配许多 Boost::直方图对象
- 使用 OpenCV 原始指针和 lambda 作为直方图的不同结果
- 如何计算图像中每个块的直方图,然后将它们连接起来?
- C++:使用数组、指针和 for 循环制作直方图。我就在那里,我可能想得太辛苦了
- 将两个 1D 直方图合并为一个
- 使用STD :: MAP在直方图峰周围的平均值
- 使用标准库或Boost库从一组数据中计算直方图
- OPENCV MAT问题:直方图和此循环之间的差异
- 优化直方图更新
- 在OPENCV中归一化函数中的直方图
- 从直方图OPENCV中的峰中获取色调值