简体   繁体   English

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

[英]getting error in calculating the Magnitude and Orientation of a Mat Using c++ and opencv

I want to get the magnitude and orientation of all pixel values in an image. 我想获取图像中所有像素值的大小和方向。 This is what I have done until now: 这是我到目前为止所做的:

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));
  }
   }

but I get this error in atan and sqrt lines : 但我在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)

does anyone know what is the problem? 有谁知道这是什么问题?

Thanks in advance... 提前致谢...

-----------After Editing------------- -----------编辑后-----------------

#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 ;
    }
}

I changed the sqrt into this but still I get error: 我将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 should get number (double, float or long), but you are providing matrix. atan应该得到数字(双精度,浮点型或长整型),但是您要提供矩阵。 You should use 你应该用

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

I wrote 'float' in my example since I don't know what type you are actually using. 我在示例中写了“ float”,因为我不知道您实际上使用的是哪种类型。 Replace it if necessary. 如有必要,请更换。

Same problem in sqrt. sqrt中的同样问题。

Edit (after edit of question): 编辑(问题编辑后):

You are using sqrt in the wrong way. 您以错误的方式使用sqrt。 See its documentation . 请参阅其文档 It should be: 它应该是:

sqrt(gradAdd, magnitude);

Even better will be to use function magnitude instead of all the multiplications and square roots: 更好的是使用函数幅度而不是所有的乘法和平方根:

magnitude(grad_x, grad_y, magnit);

Also I recommend you not to give names to matrices that are functions of OpenCV. 另外,我建议您不要给作为OpenCV函数的矩阵命名。 This will create confusion. 这会造成混乱。

the problem is in the following line: 问题在以下行中:

orientation = atan( grad_y / grad_x ) * 180/PI ;

Because Orientation is of Mat type and should contain float value(because you have declared it as CV_32FC1) so, you can not put the values directly in it. 由于“方向”是Mat类型,并且应包含浮点值(因为已将其声明为CV_32FC1),因此无法将值直接放入其中。 You should do it as following: 您应该按照以下步骤进行操作:

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

PS: Just thing once, how can you put a single value in a Matrix PS:只需说一次,如何将单个值放入矩阵

Use the followinf code for element wise computation for obtaining orientation and magnitude. 使用followinf代码进行元素智能计算以获得方向和大小。 If you want to use a function use magnitude as defined in above answer 如果要使用函数,请使用上述答案中定义的幅度

     cv::Sobel( dis, grad_x, CV_32F, 1, 0, 3);     
     cv::Sobel( dis, grad_y, CV_32F, 0, 1, 3); 
     Mat orientation(width, height, CV_32FC1);
     Mat magnitude(width, height, CV_32FC1);
     for(int i = 0; i < grad_y.rows; ++i)
     for(int j= 0; j< grad_y.cols; ++j)
     {
      orientation.at<float>(i,j) = atan2(grad_y.at<float>(i,j),grad_x.at<float>(i,j) ) * 180/PI ;
       magnitude.at<float>(i,j)= sqrt(grad_x.at<float>(i,j)*grad_x.at<float>(i,j)+grad_y.at<float>(i,j)*grad_y.at<float>(i,j));
      }

Please see if you define your matrix as CV_8UC1 then typecast argument for sqrt as float/double as it is only defined for float/double and not for integers 请查看是否将矩阵定义为CV_8UC1,然后将sqrt的typecast参数定义为float / double,因为它仅针对float / double而不针对整数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM