Ask Your Question

# 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;
}

edit retag close merge delete

## Comments

1

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

( 2014-05-13 04:08:08 -0600 )edit
1

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

( 2014-05-13 04:22:45 -0600 )edit
1

no problem, nice result ;)

( 2014-05-13 04:23:29 -0600 )edit

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

( 2014-05-13 04:44:51 -0600 )edit

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

( 2014-05-13 05:05:35 -0600 )edit

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

( 2014-05-13 06:15:32 -0600 )edit
1

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 !

( 2014-05-13 06:28:17 -0600 )edit

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

( 2014-05-13 06:40:32 -0600 )edit

## 2 answers

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

more

## Comments

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

( 2014-05-13 09:27:49 -0600 )edit

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.

( 2014-05-13 09:37:46 -0600 )edit

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

( 2014-05-13 09:47:57 -0600 )edit

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.

( 2014-05-13 09:50:58 -0600 )edit

yes yes :) Thank you for your answer tuannhtn :)

( 2014-05-13 10:06:54 -0600 )edit

You are welcome.

( 2014-05-13 10:11:45 -0600 )edit

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

( 2014-05-13 10:32:08 -0600 )edit

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.

( 2014-05-13 10:48:11 -0600 )edit

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

## Comments

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

( 2014-05-13 07:25:08 -0600 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2014-05-13 03:56:39 -0600

Seen: 14,622 times

Last updated: May 13 '14