【发布时间】:2026-01-23 21:25:01
【问题描述】:
我正在尝试对 OpenCV 中的输入图像执行方向估计。我使用 sobel 函数来获取图像的渐变,并使用我在互联网上找到的另一个名为 calculateOrientations 的函数来计算方向。
代码如下:
void computeGradient(cv::Mat inputImg)
{
// Gradient X
cv::Sobel(inputImg, grad_x, CV_16S, 1, 0, 5, 1, 0, cv::BORDER_DEFAULT);
cv::convertScaleAbs(grad_x, abs_grad_x);
// Gradient Y
cv::Sobel(inputImg, grad_y, CV_16S, 0, 1, 5, 1, 0, cv::BORDER_DEFAULT);
cv::convertScaleAbs(grad_y, abs_grad_y);
// convert from CV_8U to CV_32F
abs_grad_x.convertTo(abs_grad_x2, CV_32F, 1. / 255);
abs_grad_y.convertTo(abs_grad_y2, CV_32F, 1. / 255);
// calculate orientations
calculateOrientations(abs_grad_x2, abs_grad_y2);
}
void calculateOrientations(cv::Mat gradientX, cv::Mat gradientY)
{
// Create container element
orientation = cv::Mat(gradientX.rows, gradientX.cols, CV_32F);
// Calculate orientations of gradients --> in degrees
// Loop over all matrix values and calculate the accompagnied orientation
for (int i = 0; i < gradientX.rows; i++){
for (int j = 0; j < gradientX.cols; j++){
// Retrieve a single value
float valueX = gradientX.at<float>(i, j);
float valueY = gradientY.at<float>(i, j);
// Calculate the corresponding single direction, done by applying the arctangens function
float result = cv::fastAtan2(valueX, valueY);
// Store in orientation matrix element
orientation.at<float>(i, j) = result;
}
}
}
现在,我需要确定获得的方向是否正确。为此,我想在方向矩阵上为每个大小为 5x5 的块绘制箭头。有人可以建议我如何在这上面画箭头吗?谢谢你。
【问题讨论】:
-
您想在
<float>矩阵或一些BGR 图像上绘图吗?也许您可以在黑色图像上绘制一个箭头,然后对于每个方向值,计算旋转角度并将箭头扭曲到您的输出图像位置。或者创建一个函数来绘制一个旋转箭头到某个位置...
标签: c++ opencv image-processing filtering sobel