gradient orientation for 0-180 histogram bins in opencv 2.4.9 visual studio 2013 [closed]
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:
#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 ...
Why not use cv::HOGDescriptor?
You can calculate the magnitude and angle by using cv::cartToPolar.
And I think the depth of your sobel results should be CV_32F not CV_16S.
The book OpenCV 3 Blueprints has a chapter Recognizing Facial Expressions with Machine Learning
I would strongly recommend it.
thank you @matman for your time. actually i want to modify the the internal function of HOG that is why not using built in cv::HOGDescriptor ..i looked cv::cartToPolar but it also computes gradient angle in range 0-360 degree, i need 0-180 degree??
@sturkmen the book you recommended is looking helpful but complete chapter is not accessible for me,,can you please email me at [email protected] ..thank you.
@kim we must respect authors rights and support them.one of authors of the book is @StevenPuttemans. I congratulate him for the greate job.
@kim if you want the complete chapter, get the book :D