Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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. The goal is to find the orientation angle. Personally I do not know where the problem is 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; }

click to hide/show revision 2
retagged

updated 2014-05-13 04:08:55 -0500

berak gravatar image

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. The goal is to find the orientation angle. Personally I do not know where the problem is 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; }

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. The goal is to find the orientation angle. Personally I do not know where the problem is 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"

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

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)

(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

white // Calculate orientations of gradients --> in degrees // Loop over all matrix values and calculate the accompanied orientation

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

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

}

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

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. The goal is So I need to find the orientation angle. Personally I do not know where is the problem is 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;
 }