gpt4 book ai didi

c++ - 计算梯度方向

转载 作者:搜寻专家 更新时间:2023-10-31 01:08:32 24 4
gpt4 key购买 nike

我想从深度图中计算梯度的角度并将其分组到某些方向(8 个扇区)但是我的函数只计算前 3 个方向

cv::Mat calcAngles(cv::Mat dimg)//dimg is depth map
{
const int directions_num = 8;//number of directions
const int degree_grade = 360;
int range_coeff = 255 / (directions_num + 1);//just for visualize
cv::Mat x_edge, y_edge, full_edge, angles;
dimg.copyTo(x_edge);
dimg.copyTo(y_edge);
dimg.copyTo(full_edge);
//compute gradients

Sobel( dimg, x_edge, CV_8U, 1, 0, 5, 1, 19, 4 );
Sobel( dimg, y_edge, CV_8U, 0, 1, 5, 1, 19, 4 );
Sobel( dimg, full_edge, CV_8U, 1, 1, 5, 1, 19, 4 );

float freq[directions_num + 1];//for collect direction's frequency
memset(freq, 0, sizeof(freq));

angles = cv::Mat::zeros(dimg.rows, dimg.cols, CV_8U);//store directions here
for(int i = 0; i < angles.rows; i++)
{
for(int j = 0; j < angles.cols; j++)
{
angles.at<uchar>(i, j) = (((int)cv::fastAtan2(y_edge.at<uchar>(i, j), x_edge.at<uchar>(i, j))) / (degree_grade/directions_num) + 1
) * (dimg.at<uchar>(i, j) ? 1 : 0);//fastatan returns values from 0 to 360, if i not mistaken. I want group angles by directions_num sectors. I use first 'direction' (zero value) for zero values from depth map (zero value at my depth map suggest that it is bad pixel)
freq[angles.at<uchar>(i, j)] += 1;
}
}
for(int i = 0; i < directions_num + 1; i++)
{
printf("%2.2f\t", freq[i]);
}
printf("\n");
angles *= range_coeff;//for visualization
return angles;
}

从其中一帧出来:

47359.00        15018.00        8199.00 6224.00 0.00    0.00    0.00    0.00    0.00

(第一个值是“零像素”,接下来是 n 位的梯度数,但只有 3 个不为零)

可视化

enter image description here

有出路吗?或者这些结果还可以吗?PS对不起我的写作错误。英语不是我的母语。

最佳答案

您为 Sobel 输出使用了 CV_8U 类型。它是 8 位无符号整数。所以它只能存储正值。这就是 fastAtan2 返回小于或等于 90 的原因。将类型更改为 CV_16S 并使用 short 类型访问元素:

cv::Sobel(dimg, x_edge, CV_16S, 1, 0, 5, 1, 19, 4);
cv::Sobel(dimg, y_edge, CV_16S, 0, 1, 5, 1, 19, 4);

cv::fastAtan2(y_edge.at<short>(i, j), x_edge.at<short>(i, j))

关于c++ - 计算梯度方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17945237/

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