Ask Your Question

Revision history [back]

The following code is some sample code from a project I am doing in C++. It uses as input two gradient containers to define the orientation of the gradient for each point.

Mat calculateOrientations(Mat gradientX, Mat gradientY){
    // Create container element
    Mat orientation = 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 = fastAtan2(valueX,valueY);
            // Store in orientation matrix element
            orientation.at<float>(i,j) = result;
        }
    }

    return orientation;
}

Using this information, you can decide to calculate the average orientation for like a bin of 5x5 pixels easily. Hope this helps you out!

The following code is some sample code from a project I am doing in C++. It uses as input two gradient containers to define the orientation of the gradient for each point.

Mat calculateOrientations(Mat gradientX, Mat gradientY){
    // Create container element
    Mat orientation = 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 = fastAtan2(valueX,valueY);
            // Store in orientation matrix element
            orientation.at<float>(i,j) = result;
        }
    }

    return orientation;
}

Using this information, you can decide to calculate the average orientation for like a bin of 5x5 pixels easily. Hope this helps you out!

Addition 1 :

As suggested, these angles can be used to create arrows that point in the angle direction and take the absolute vector size when combining both gradients as length. An example result of those orientation map on a microscopic organism can be seen below, which is done by using matlab, but i get similar images in OpenCV, just havent created one lately.

image description