Ask Your Question

Revision history [back]

I do not know what cause the error you got, but I have several solutions that you can consider to use:

  1. Use solution from this question.
  2. Don't use the two for loops:

    for(int i = 0; i < abs_grad_x.rows; i++){ for(int j = 0; j < abs_grad_x.cols; j++){ // Retrieve a single value

            float valueX = abs_grad_x.at<float>(i,j);
            float valueY = abs_grad_x.at<float>(i,j);
            // Calculate the corresponding single direction, done by applying the arctangens function
            float result = fastAtan2(valueX,valueY);
            // Store in orientation matrix element
            orientation.at<float>(i,j) = result;
        }
     }
    

    Just do like this:

    Mat orientation = Mat::zeros(abs_grad_x.rows, abs_grad_y.cols, CV_32F); //to store the gradients grad_x.convertTo(grad_x,CV_32F);

    grad_y.convertTo(grad_y,CV_32F);

    phase(grad_x, grad_y, orientation);

    cv::normalize(orientation, orientation, 0x00, 0xFF, cv::NORM_MINMAX, CV_8U);

    namedWindow("Orientation", CV_WINDOW_AUTOSIZE );

    imshow( "Orientation", orientation );

The phase() function of OpenCV is for computing the orientations from gradient components as your wish and it has good performance due to optimizations inside the OpenCV. Hope this help.

I do not know what cause causes the error you got, but I have several solutions that you can consider to use:

  1. Use solution from this question.
  2. Don't use the two for loops:

    for(int i = 0; i < abs_grad_x.rows; i++){ for(int j = 0; j < abs_grad_x.cols; j++){ // Retrieve a single value

            float valueX = abs_grad_x.at<float>(i,j);
            float valueY = abs_grad_x.at<float>(i,j);
            // Calculate the corresponding single direction, done by applying the arctangens function
            float result = fastAtan2(valueX,valueY);
            // Store in orientation matrix element
            orientation.at<float>(i,j) = result;
        }
     }
    

    Just do like this:

    Mat orientation = Mat::zeros(abs_grad_x.rows, abs_grad_y.cols, CV_32F); //to store the gradients grad_x.convertTo(grad_x,CV_32F);

    grad_y.convertTo(grad_y,CV_32F);

    phase(grad_x, grad_y, orientation);

    cv::normalize(orientation, orientation, 0x00, 0xFF, cv::NORM_MINMAX, CV_8U);

    namedWindow("Orientation", CV_WINDOW_AUTOSIZE );

    imshow( "Orientation", orientation );

The phase() function of OpenCV is for computing the orientations from gradient components as your wish and it has good performance due to optimizations inside the OpenCV. Hope this help.