title: Determining image gradient direction from sobel?
body: <p>I am attempting to determine the image gradient direction using the results from openCV's Sobel method. </p>
  
  <p>I understand this should be a very simple task. I have copied the methods from a number of resources and answers from here but whatever I do the resultant directions are always between 0 - 57 degrees (I would expect the range to be from 0-360).</p>
  
  <p>I believe all the depths are correct. I have tried calculating the direction using the 16S data as well as 8U data.</p>
  
  <p>I just can't see where I'm going wrong? Can anyone spot my mistake?</p>
  
  <pre><code>void getGradients(IplImage* original, cv::Mat* gradArray)
  {
      cv::Mat original_Mat(original, true);
  
      // Convert it to gray
      cv::cvtColor( original_Mat, original_Mat, CV_RGB2GRAY );
      //cv::blur(original_Mat, original_Mat, cv::Size(7,7));
  
      /// Generate grad_x and grad_y
      cv::Mat grad_x = cv::Mat::zeros(original-&gt;height, original-&gt;width, CV_16S); 
      cv::Mat grad_y = cv::Mat::zeros(original-&gt;height, original-&gt;width, CV_16S);
  
      cv::Mat abs_grad_x = cv::Mat::zeros(original-&gt;height, original-&gt;width, CV_8U);
      cv::Mat abs_grad_y = cv::Mat::zeros(original-&gt;height, original-&gt;width, CV_8U);;
  
      /// Gradient X
      cv::Sobel(original_Mat, grad_x, CV_16S, 1, 0, 3);
      cv::convertScaleAbs( grad_x, abs_grad_x );
  
      /// Gradient Y
      cv::Sobel(original_Mat, grad_y, CV_16S, 0, 1, 3);
      cv::convertScaleAbs( grad_y, abs_grad_y );
  
      uchar* pixelX = abs_grad_x.data;
      uchar* pixelY = abs_grad_y.data;
      uchar* grad1 = gradArray[0].data;
      uchar* grad2 = gradArray[1].data;
      uchar* grad3 = gradArray[2].data;
      uchar* grad4 = gradArray[3].data;
      uchar* grad5 = gradArray[4].data;
      uchar* grad6 = gradArray[5].data;
      uchar* grad7 = gradArray[6].data;
      uchar* grad8 = gradArray[7].data;
      int count = 0;
      int min = 999999;
      int max = 0;
  
      for(int i = 0; i &lt; grad_x.rows * grad_x.cols; i++) 
      {
              int directionRAD = atan2(pixelY[i], pixelX[i]);
              int directionDEG = directionRAD / PI * 180;
  
              if(directionDEG &lt; min){min = directionDEG;}
              if(directionDEG &gt; max){max = directionDEG;}
  
              if(directionDEG &gt;= 0 &amp;&amp; directionDEG &lt;= 45)         { grad1[i] = 255; count++;}         
              if(directionDEG &gt;= 45 &amp;&amp; directionDEG &lt;= 90)        { grad2[i] = 255; count++;}         
              if(directionDEG &gt;= 90 &amp;&amp; directionDEG &lt;= 135)       { grad3[i] = 255; count++;}         
              if(directionDEG &gt;= 135 &amp;&amp; directionDEG &lt;= 190)      { grad4[i] = 255; count++;}         
              if(directionDEG &gt;= 190 &amp;&amp; directionDEG &lt;= 225)      { grad5[i] = 255; count++;}         
              if(directionDEG &gt;= 225 &amp;&amp; directionDEG &lt;= 270)      { grad6[i] = 255; count++;}     
              if(directionDEG &gt;= 270 &amp;&amp; directionDEG &lt;= 315)      { grad7[i] = 255; count++;}
              if(directionDEG &gt;= 315 &amp;&amp; directionDEG &lt;= 360)      { grad8[i] = 255; count++;}
  
              if(directionDEG &lt; 0 || directionDEG &gt; 360)
              {
                  cout&lt;&lt;"Weird gradient direction given in method: getGradients.";
              }               
      }
  }
  </code></pre>