title: Computing gradient orientation in c++ using opencv functions
body: <p>Can anyone help me out with this?</p>
  
  <p>I am trying to calculate gradient orientation using the Sobel operator in OpenCV for gradient in x and y direction. I am using the atan2 function for computing the tangent in radians, which I later convert to degrees, but all the angles I am getting are between 0 and 90 degrees.</p>
  
  <p>My expectation is to get angles between 0 and 360 degrees. The image I am using is grayscale. The code segment is here below.</p>
  
  <pre><code>Mat PeripheralArea;
  Mat grad_x, grad_y;  // this is the matrix for the gradients in x and y directions
  int off_set_y = 0, off_set_x = 0;
  int scale = 1, num_bins = 8, bin = 0;
  int delta=-1 ;
  int ddepth = CV_16S;
  
  GaussianBlur(PeripheralArea, PeripheralArea, Size(3, 3), 0, 0, BORDER_DEFAULT);
  Sobel(PeripheralArea, grad_y, ddepth, 0, 1,3,scale, delta, BORDER_DEFAULT);
  Sobel(PeripheralArea, grad_x, ddepth, 1, 0,3, scale, delta, BORDER_DEFAULT);
  
  for (int row_y1 = 0, row_y2 = 0; row_y1 &lt; grad_y.rows / 5, row_y2 &lt; grad_x.rows / 5; row_y1++, row_y2++) {
      for (int col_x1 = 0, col_x2 = 0; col_x1 &lt; grad_y.cols / 5, col_x2 &lt; grad_x.cols / 5; col_x1++, col_x2++) {
          gradient_direction_radians = (double) atan2((double) grad_y.at&lt;uchar&gt;(row_y1 + off_set_y, col_x1 + off_set_x), (double) grad_x.at&lt;uchar&gt;(row_y2 + off_set_y, col_x2 + off_set_x));
          gradient_direction_degrees = (int) (180 * gradient_direction_radians / 3.1415);
          gradient_direction_degrees = gradient_direction_degrees &lt; 0
                                       ? gradient_direction_degrees+360
                                       : gradient_direction_degrees;
  
      }
  
  }
  </code></pre>
  
  <p>Note the <code>off_set_x</code> and <code>off_set_y</code> variable are not part of the computation 
   but to offset to different square blocks for which I eventually want to 
   compute an histogram feature vector</p>