How to calculate the absolute value of complex numbers in opencv


can any one help me about how to get the absolute value of a complex matrix.the matrix contains real value in one channel and imaginary value in another one channel.please help me if s possible means give me some example.

Thanks in advance Arangarajan

Let's assume you have 2 components: X and Y, two matrices of the same size and type. In your case it can be real/im values.

// n rows, m cols, type float; we assume the following matrices are filled
cv::Mat X(n,m,CV_32F);
cv::Mat Y(n,m,CV_32F);

You can compute the absolute value of each complex number like this:

// create a new matrix for storage
cv::Mat A(n,m,CV_32F,cv::Scalar(0.0));
for(int i=0;i<n;i++){
  // pointer to row(i) values
  const float* rowi_x = X.ptr<float>(i);
  const float* rowi_y = Y.ptr<float>(i);
  float* rowi_a = A.ptr<float>(i);
  for(int j=0;j<=m;j++){
     rowi_a[j] = sqrt(rowi_x[j]*rowi_x[j]+rowi_y[j]*rowi_y[j]);