This forum is disabled, please visit https://forum.opencv.org

1 | initial version |

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!

2 | No.2 Revision |

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.

Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.