#include <opencv2/opencv.hpp>
#include <iostream>
#include <time.h>
#define _DEBUG false
using namespace cv;
using namespace cv::ml;
using namespace std;
void get_svm_detector(const Ptr<SVM>& svm, vector< float > & hog_detector );
void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData );
void load_images( const string & prefix, vector< Mat > & img_lst );
void sample_neg( const vector< Mat > & full_neg_lst, vector< Mat > & neg_lst, const Size & size );
Mat get_hogdescriptor_visu(const Mat& color_origImg, vector<float>& descriptorValues, const Size & size );
void compute_hog( const vector< Mat > & img_lst, vector< Mat > & gradient_lst, const Size & size );
void train_svm( const vector< Mat > & gradient_lst, const vector< int > & labels );
void draw_locations( Mat & img, const vector< Rect > & locations, const Scalar & color );
void test_it( const Size & size );
void get_svm_detector(const Ptr<SVM>& svm, vector< float > & hog_detector )
{
Mat sv = svm->getSupportVectors();
const int sv_total = sv.rows;
Mat alpha, svidx;
double rho = svm->getDecisionFunction(0, alpha, svidx);
CV_Assert( alpha.total() == 1 && svidx.total() == 1 && sv_total == 1 );
CV_Assert( (alpha.type() == CV_64F && alpha.at<double>(0) == 1.) ||
(alpha.type() == CV_32F && alpha.at<float>(0) == 1.f) );
CV_Assert( sv.type() == CV_32F );
hog_detector.clear();
hog_detector.resize(sv.cols + 1);
memcpy(&hog_detector[0], sv.ptr(), sv.cols*sizeof(hog_detector[0]));
hog_detector[sv.cols] = (float)-rho;
}
* Convert training/testing set to be used by OpenCV Machine Learning algorithms.
* TrainData is a matrix of size (#samples x max(#cols,#rows) per samples), in 32FC1.
* Transposition of samples are made if needed.
*/
void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData )
{
const int rows = (int)train_samples.size();
const int cols = (int)std::max( train_samples[0].cols, train_samples[0].rows );
cv::Mat tmp(1, cols, CV_32FC1);
trainData = cv::Mat(rows, cols, CV_32FC1 );
vector< Mat >::const_iterator itr = train_samples.begin();
vector< Mat >::const_iterator end = train_samples.end();
for( int i = 0 ; itr != end ; ++itr, ++i )
{
CV_Assert( itr->cols == 1 ||
itr->rows == 1 );
if( itr->cols == 1 )
{
transpose( *(itr), tmp );
tmp.copyTo( trainData.row( i ) );
}
else if( itr->rows == 1 )
{
itr->copyTo( trainData.row( i ) );
}
}
}
void load_images( const string & prefix, vector< Mat > & img_lst )
{
vector<String> files;
glob(prefix, files);
for (size_t i = 0; i < files.size(); ++i)
{
Mat img = imread(files[i]);
if (img.empty())
{
cout << files[i] << " is invalid!" << endl;
continue;
}
#ifdef _DEBUG
imshow("image", img);
waitKey(10);
#endif
img_lst.push_back(img);
}
}
void sample_neg( const vector< Mat > & full_neg_lst, vector< Mat > & neg_lst, const Size & size )
{
Rect box;
box.width = size.width;
box.height = size.height;
const int size_x = box.width;
const int size_y = box.height;
srand( (unsigned int)time( NULL ) );
vector< Mat >::const_iterator img = full_neg_lst.begin();
vector< Mat >::const_iterator end = full_neg_lst.end();
for( ; img != end ; ++img )
{
box.x = rand() % (img->cols - size_x);
box.y = rand() % (img->rows - size_y);
Mat roi = (*img)(box);
neg_lst.push_back( roi.clone() );
imshow( "img", roi.clone() );
waitKey(0);
}
}
Mat get_hogdescriptor_visu(const Mat& color_origImg, vector<float>& descriptorValues, const Size & size )
{
const int DIMX = size.width;
const int DIMY = size.height;
float zoomFac = 3;
Mat visu;
resize(color_origImg, visu, Size( (int)(color_origImg.cols*zoomFac), (int)(color_origImg.rows*zoomFac) ) );
int cellSize = 8;
int gradientBinSize = 9;
float radRangeForOneBin = (float)(CV_PI/(float)gradientBinSize);
int cells_in_x_dir = DIMX / cellSize;
int cells_in_y_dir = DIMY / cellSize;
float*** gradientStrengths = new float**[cells_in_y_dir];
int** cellUpdateCounter = new int*[cells_in_y_dir];
for (int y=0; y<cells_in_y_dir; y++)
{
gradientStrengths[y] = new float*[cells_in_x_dir];
cellUpdateCounter[y] = new int[cells_in_x_dir];
for (int x=0; x<cells_in_x_dir; x++)
{
gradientStrengths[y][x] = new float[gradientBinSize];
cellUpdateCounter[y][x] = 0;
for (int bin=0; bin<gradientBinSize; bin++)
gradientStrengths[y][x][bin] = 0.0;
}
}
int blocks_in_x_dir = cells_in_x_dir - 1;
int blocks_in_y_dir = cells_in_y_dir - 1;
int descriptorDataIdx = 0;
int cellx = 0;
int celly = 0;
for (int blockx=0; blockx<blocks_in_x_dir; blockx++)
{
for (int blocky=0; blocky<blocks_in_y_dir; blocky++)
{
for (int cellNr=0; cellNr<4; cellNr++)
{
cellx = blockx;
celly = blocky;
if (cellNr==1) celly++;
if (cellNr==2) cellx++;
if (cellNr==3)
{
cellx++;
celly++;
}
for (int bin=0; bin<gradientBinSize; bin++)
{
float gradientStrength = descriptorValues[ descriptorDataIdx ];
descriptorDataIdx++;
gradientStrengths[celly][cellx][bin] += gradientStrength;
}
cellUpdateCounter[celly][cellx]++;
}
}
}
for (celly=0; celly<cells_in_y_dir; celly++)
{
for (cellx=0; cellx<cells_in_x_dir; cellx++)
{
float NrUpdatesForThisCell = (float)cellUpdateCounter[celly][cellx];
for (int bin=0; bin<gradientBinSize; bin++)
{
gradientStrengths[celly][cellx][bin] /= NrUpdatesForThisCell;
}
}
}
for (celly=0; celly<cells_in_y_dir; celly++)
{
for (cellx=0; cellx<cells_in_x_dir; cellx++)
{
int drawX = cellx * cellSize;
int drawY = celly * cellSize;
int mx = drawX + cellSize/2;
int my = drawY + cellSize/2;
rectangle(visu, Point((int)(drawX*zoomFac), (int)(drawY*zoomFac)), Point((int)((drawX+cellSize)*zoomFac), (int)((drawY+cellSize)*zoomFac)), Scalar(100,100,100), 1);
for (int bin=0; bin<gradientBinSize; bin++)
{
float currentGradStrength = gradientStrengths[celly][cellx][bin];
if (currentGradStrength==0)
continue;
float currRad = bin * radRangeForOneBin + radRangeForOneBin/2;
float dirVecX = cos( currRad );
float dirVecY = sin( currRad );
float maxVecLen = (float)(cellSize/2.f);
float scale = 2.5;
float x1 = mx - dirVecX * currentGradStrength * maxVecLen * scale;
float y1 = my - dirVecY * currentGradStrength * maxVecLen * scale;
float x2 = mx + dirVecX * currentGradStrength * maxVecLen * scale;
float y2 = my + dirVecY * currentGradStrength * maxVecLen * scale;
line(visu, Point((int)(x1*zoomFac),(int)(y1*zoomFac)), Point((int)(x2*zoomFac),(int)(y2*zoomFac)), Scalar(0,255,0), 1);
}
}
}
for (int y=0; y<cells_in_y_dir; y++)
{
for (int x=0; x<cells_in_x_dir; x++)
{
delete[] gradientStrengths[y][x];
}
delete[] gradientStrengths[y];
delete[] cellUpdateCounter[y];
}
delete[] gradientStrengths;
delete[] cellUpdateCounter;
return visu;
}
void compute_hog( const vector< Mat > & img_lst, vector< Mat > & gradient_lst, const Size & size )
{
HOGDescriptor hog;
hog.winSize = size;
Mat gray;
vector< Point > location;
vector< float > descriptors;
vector< Mat >::const_iterator img = img_lst.begin();
vector< Mat >::const_iterator end = img_lst.end();
for( ; img != end ; ++img )
{
cvtColor( *img, gray, COLOR_BGR2GRAY );
hog.compute( gray, descriptors, Size( 8, 8 ), Size( 0, 0 ), location );
gradient_lst.push_back( Mat( descriptors ).clone() );
imshow( "gradient", get_hogdescriptor_visu( img->clone(), descriptors, size ) );
waitKey( 0 );
}
}
void train_svm( const vector< Mat > & gradient_lst, const vector< int > & labels )
{
Mat train_data;
convert_to_ml( gradient_lst, train_data );
clog << "Start training...";
Ptr<SVM> svm = SVM::create();
svm->setCoef0(0.0);
svm->setDegree(3);
svm->setTermCriteria(TermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, 1e-3 ));
svm->setGamma(0);
svm->setKernel(SVM::LINEAR);
svm->setNu(0.5);
svm->setP(0.1);
svm->setC(0.01);
svm->setType(SVM::EPS_SVR);
svm->train(train_data, ROW_SAMPLE, Mat(labels));
clog << "...[done]" << endl;
svm->save( "armour.yml" );
}
void draw_locations( Mat & img, const vector< Rect > & locations, const Scalar & color )
{
if( !locations.empty() )
{
vector< Rect >::const_iterator loc = locations.begin();
vector< Rect >::const_iterator end = locations.end();
for( ; loc != end ; ++loc )
{
rectangle( img, *loc, color, 2 );
}
}
}
int main( int argc, char** argv )
{
vector< Mat > pos_lst;
vector< Mat > full_neg_lst;
vector< Mat > neg_lst;
vector< Mat > gradient_lst;
vector< int > labels;
string pos_dir = "/home/sunny/HOG/HOG_train/pos/";
string neg_dir = "/home/sunny/HOG/HOG_train/neg/";
cout << "positive images are being loaded." << endl;
load_images( pos_dir, pos_lst );
Size pos_image_size = pos_lst[0].size();
for (size_t i = 0; i < pos_lst.size(); ++i)
{
if( pos_lst[i].size() != pos_image_size)
{
cout << "All positive images should be same size!" << endl;
exit( -1 );
}
}
labels.assign( pos_lst.size(), +1 );
const unsigned int old = (unsigned int)labels.size();
cout << "negative images are being loaded." << endl;
load_images( neg_dir, full_neg_lst );
sample_neg( full_neg_lst, neg_lst, pos_image_size );
cout << "negative images have finished." << endl;
labels.insert( labels.end(), neg_lst.size(), -1 );
CV_Assert( old < labels.size() );
compute_hog( pos_lst, gradient_lst, pos_image_size );
compute_hog( neg_lst, gradient_lst, pos_image_size );
train_svm( gradient_lst, labels );
return 0;
}