c++ - 在使用 c++ 和 opencv 计算垫子的大小和方向时出错

标签 c++ image opencv mat

我想获取图像中所有像素值的大小和方向。这是我到目前为止所做的:

cv::Sobel( dis, grad_x, dis.depth(), 1, 0, 3);     
cv::Sobel( dis, grad_y, dis.depth(), 0, 1, 3); 
Mat orientation(width, height, CV_32FC1);

#define PI 3.14159265
 for(int i = 0; i < grad_y.rows; ++i){
  for(int j= 0; j< grad_y.cols; ++j){
       orientation = atan( grad_y / grad_x ) * 180/PI ;
       magnitude= sqrt((grad_x)*(grad_x)+(grad_y)*(grad_y));
  }
   }

但我在 atan 和 sqrt 行中收到此错误:

error C2665: 'atan' : none of the 3 overloads could convert all the argument type   
 math.h(108):could be 'double atan(double)
 math.h(505):float atan(float)
 math.h(553):long double atan(long double)

谁知道问题出在哪里?

提前致谢...

------------编辑后------------

#define PI 3.14159265
  for(int i=0; i<grad_y.rows; i++){
    for(int j=0; j<grad_y.cols; j++){
        orientation = atan( grad_y.at<float>(i,j) / grad_x.at<float>(i,j) ) * 180/PI ;
    }
}

我将 sqrt 改成了这个,但仍然出现错误:

 Mat magnitude(m_pmdDevice->GetY(), m_pmdDevice->GetX(), CV_32FC1);
    Mat gradAdd(m_pmdDevice->GetY(), m_pmdDevice->GetX(), CV_32FC1);
    grad_x = grad_x.mul(grad_x);
    grad_y = grad_y.mul(grad_y);
    cv::add(grad_x, grad_y, gradAdd);
    magnitude = sqrt(gradAdd);

最佳答案

atan 应该得到数字( double 、 float 或长整型),但您提供的是矩阵。你应该使用

orientation = atan( grad_y.at<float>(i,j) / grad_x.at<float>(i,j) ) * 180/PI ;

我在示例中写了“float”,因为我不知道您实际使用的是什么类型。必要时更换它。

同样的问题在 sqrt 中。

编辑(问题编辑后):

您以错误的方式使用了 sqrt。查看其 documentation .应该是:

sqrt(gradAdd, magnitude);

更好的方法是使用函数 magnitude而不是所有的乘法和平方根:

magnitude(grad_x, grad_y, magnit);

此外,我建议您不要为作为 OpenCV 函数的矩阵命名。这会造成困惑。

关于c++ - 在使用 c++ 和 opencv 计算垫子的大小和方向时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881950/

相关文章:

c++ - 从 map 中删除条目显示 Valgrind 中的泄漏

c++ - 为什么这个 vector 迭代器不能递增?

html - Angular 4+ 背景 css 引用 URL 图片路径

ios - 如何在没有去拜耳的情况下对 RAW 图像进行下采样以在较小的屏幕上进行编辑?

c++ - 文字运算符模板在 GCC 4.8 中不起作用

c++ - 对主 C++ 的递归调用

c++ - 使用 C++ 和 opencv 进行图像缩放的 Spline Catmull-Rom

image - OpenCV:如何将 HERSHEY 以外的其他字体与 cvPutText(如 Arial)一起使用

python - 在python opencv中获取强度为255的二进制图像的像素位置

OpenCV:训练级联分类器