# Calculates the angle of orientation of the image with the gradient method

Hello, I have an image I applied the gradient method for the orientation angle of the latter. following the execution of this code I images for gradient directions Dx and DY. So I need to find the orientation angle. Personally I do not know where is the problem because I have no error! Please help me

```
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
using namespace std;
using namespace cv;
//#include "Functions.h"
int main()
{
Mat image;
//image = imread("lena.jpg",1);
image = imread("uEyeImg0.tif",1);
if(image.empty())
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
/// Convert it to gray
//cvtColor( image, image, CV_RGB2GRAY );
//resize(image,image,Size(0,0),0.5,0.5,INTER_LINEAR);
namedWindow("Image", CV_WINDOW_AUTOSIZE );
imshow("Image", image);
/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
int scale = 1;
int delta = 0;
int ddepth = CV_16S;
Mat grad;
/// Gradient X
//Scharr( image, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );
/// Gradient Y
// Scharr( image, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );
/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
Mat orientation = Mat(abs_grad_x.rows, abs_grad_y.cols, CV_32F); //to store the gradients
Mat img=Mat(abs_grad_x.rows, abs_grad_y.cols, CV_32F);//to draw out the map
img = cv::Scalar(255,255,255);//all white
// Calculate orientations of gradients --> in degrees
// Loop over all matrix values and calculate the accompanied orientation
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;
}
}
namedWindow("ImageSobel", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobel", grad );
namedWindow("ImageSobelGx", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobelGx", abs_grad_x );
namedWindow("ImageSobelGy", CV_WINDOW_AUTOSIZE );
imshow( "ImageSobelGy", abs_grad_y );
waitKey(0);
return 0;
}
```

if you edit your question, you will find the 10101 button. use it to format your code, please.

I'm sorry I am a beginner this is the first time I write in a forum. thank you berak

no problem, nice result ;)

can you help me berak , please how I can make debugging and how I can recover stoker and the orientation angle?

now, what was the exact problem, again ? you probably should add that above ;)

I want to get back the angle of orientation of the image : the rule is atan=dy/dx

btw, the 2nd

float valueY = abs_grad_x.at<float>(i,j);

should probably be float valueY = abs_grad_y.at<float>(i,j);

( you got abs_grad_x for both x and y)

and we still don't know, what actually went wrong !

Yes it is true but even if I corrected this typing error I manage not to get back on the console for example the value of the angle of orientation :/ thank you