I am trying to develop a facial emotion system using OpenCV 2.4.9 and C++ in Visual Studio 2013. I need to extract shape based facial features using pyramid HOG, i used cv::phase() for gradient orientation in range of 0-360... i tried to use cv::fastAtan2() for gradient 0-180 by first making the -ve gradient to be positive, but there are some dimension problem and code end with exception..if possible for anyone..kindly help me to solve this..thanks in advance. code i am using is as follows:
enter code here
include "opencv2/imgproc/imgproc.hpp"
include "opencv2/highgui/highgui.hpp"
include <stdlib.h>
include <stdio.h>
include <iostream>
include <fstream> // library that contains file input/output functions
include <math.h>
include <stdio.h>
using namespace cv; using namespace std;
/* @function main */ int main(int argc, char* argv) {
Mat src, src_gray;
Mat grad;
char* window_name = "Sobel Edge Detector";
int scale = 1;
int delta = 0;
int ddepth = CV_16S;//16S for 0-360 degree; 8U for 0-90 degree;16U for 0-90
int c;
/// Load an image
src = imread( "whole_face_0.jpg");
if (!src.data)
{
return -1;
}
//GaussianBlur(src, src, Size(3, 3), 0, 0, BORDER_DEFAULT);
/// Convert it to gray
//cvtColor(src, src_gray, CV_RGB2GRAY);
src_gray = src;
/// Create window
namedWindow(window_name, CV_WINDOW_AUTOSIZE);
/// Generate grad_x and grad_y
Mat grad_x, grad_y;
//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.
Mat abs_grad_x, abs_grad_y;//
//to calculate the gradient in x direction we use : x_order = 1 and y_ order = 0. We do analogously for the y direction
/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
//Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
src_gray = src;
//Scharr(src_gray, grad_x, CV_16S, 1, 0, scale, delta, BORDER_DEFAULT);
Sobel(src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
convertScaleAbs(grad_x, abs_grad_x); //CV_8U type for Sobel output. It is unsigned integer 8 bit. So it can store only positive values.
/// Gradient Y
//Scharr(src_gray, grad_y, CV_16S, 0, 1, scale, delta, BORDER_DEFAULT);//ddepth=CV_16S
Sobel(src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
//Calculates an absolute value of each matrix element
convertScaleAbs(grad_y, abs_grad_y);
//we try to approximate the gradient by adding both directional gradients
//(note that this is not an exact calculation at all! but it is good for our purposes).
/// Total Gradient (approximate)
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
/////////////// gradient angle computation //////////////////
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);
Mat angle;
for (int g = 0; g < grad_x.rows;g++)
for (int k = 0; k < grad_x.cols; k++){
if (grad_x.at<float>(g, k) < 0)
grad_x.at<float>(g, k) = grad_x.at<float>(g, k)*(-1);
if (grad_y.at<float>(g, k) < 0)
grad_y.at<float>(g, k) = grad_y.at<float>(g, k)*(-1);
}
for (int g = 0; g < grad_x.rows; g++)
for (int k = 0; k < grad_x.cols; k++){
angle.at<float>(g, k) = cv::fastAtan2(grad_y.at<float>(g, k), grad_x.at<float>(g, k));
//cv::phase(abs_grad_x, abs_grad_y, orientation, true); //it accept orientation type as 16F or 32F
}
Mat gradient_magnitude(grad_x.size(), grad_x.type());
magnitude(grad_x, grad_y, gradient_magnitude);
/// Total Gradient (approximate)
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
imshow(window_name, grad);
waitKey(0);
return 0;
}