# 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;
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);
int scale = 1;
int delta = 0;
int ddepth = CV_16S;

//Scharr( image, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );

// Scharr( image, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( image, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );

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

// 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 );

namedWindow("ImageSobelGx", CV_WINDOW_AUTOSIZE );

namedWindow("ImageSobelGy", CV_WINDOW_AUTOSIZE );

waitKey(0);
return 0;
}

edit retag close merge delete

1

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

1

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

1

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

1

btw, the 2nd

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

Sort by » oldest newest most voted

I do not know what 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);
// 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:

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.

more

It is true that we manage to have an image of the orientation of the pressure gradient on the other hand my objective it is to have the angle of orientation of the image in degree. I thank you for your answer

OK, for that purpose, just call the function phase() like this: phase(grad_x, grad_y, orientation, true); The normalize() and imshow() functions are just for visual illustration.

Ok , but I need the value of orientation in degree !! how I can see if it is type Mat !! thank you :)

The orientation matrix contains degree values (see http://docs.opencv.org/modules/core/doc/operations_on_arrays.html#void phase(InputArray x, InputArray y, OutputArray angle, bool angleInDegrees) for more details). If you want to see those values, test this code: ofstream file1("orient1.txt"); file1 << orientation; file1.close();

And then open the orient1.txt file to see if that what you want.

I have an other problem . I want to cut Cut an image in many small images with opencv !! Can you help me please !!!

You can refer to the simple technique that LBP method use to calculate histogram from small images (non-overlapping sub-regions) in the file https://github.com/bytefish/opencv/blob/master/lbp/histogram.cpp, see the function spatial_histogram() where each cell is a small image whose histogram is computed. But for your future problem, please consider to open a new question so other people can notice that this is a solved one. I didn't debug your program, but it has a clear flaw: you are using absolute values of gradients to compute the gradient direction instead of the gradients themselves!

Note that for a gradient gx=1 gy=0 the orientation is 0, but if gx=-1 the orientation is PI/2 (even if the absolute values match).

The absolute values need to be computed for displaying the gradients (as you cannot display values <0), but they shouldn't be used for further processing.

more

Thank you for your answer but even if I remove value absolute during the execution of the program I have indistinctness that I changed nothing !!! :/ Please help me

Official site

GitHub

Wiki

Documentation