Simple Object Classification with HOG and SVM

Do object recognition with HOG feature and SVM classifier.

[Usage]

Training Algorithm Flow

  1. manually select representive _sample_s to generate TrainSet and TestSet

  2. Extract HOG feature for each samples either in TrainSet or in TestSet

  3. Attatch label to each sample

  4. Set parameters and train Model (you can determine save Model or not. If saved, Model will be saved into a XML file.)

  5. Predict samples in TestSet with SVM Model

Training Example Code with EndToEnd

1
2
3
4
5
6
7
8
9
10
....
....

string dirPath = "..."
HOG_SVM hog_svm;
hog_svm.SetSvmParameter(....);
float result = hog_svm.EndToEnd(dirPath);

....
....

Predict with Model in in practice:

1
2
3
4
5
6
7
8
9
10
....
....

Mat src = .... ;
string modelPath = "..."
HOG_SVM hog_svm(modelPath);
float result = hog_svm.predict(src);

....
....

[Example]

White rectangle bounding boxes denote the topk (which select topk Main Region with Depth Segmentation Algorithm in the previous blog) color candidate regions to calculate HOG features, which will be classified by an SVM classifier. The recognition results of the right image show that cuboid regions filtered by the classifier are marked red, while the rest white regions are removed from candidate regions after classifying.

Color Depth
Color Depth
Regions Classification
Regions Classification

Capture picture with camera

Capture data with camera

Press $Space$ to capture
Press $ESC$ to exit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*************************************************************************
> File Name: SplitVideo.cpp
> Author: zyy
> Mail: zyy34472@gmail.com
************************************************************************/


#include <iostream>
#include <string>
#include <ctime>
#include <sys/stat.h>
#include <sys/types.h>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

string getCurrentTime()
{

time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%Y-%m-%d-%X",localtime(&t) );
mkdir(tmp, S_IRWXU);
return string(tmp);
}

int main ()
{

Mat frame;
VideoCapture vc(0);
int i, fps, mykey;
string now = getCurrentTime();
for (i = 0, fps = 30; mykey = waitKey(1000/fps); i++) {
vc >> frame;
imshow("test", frame);
if (mykey == 32)
imwrite("./" + now + "/" + to_string(i) + ".jpg", frame);
else if (mykey == 27)
break;
}
return 0;
}

Split Video

Split Video to a batch of pictures

Input: Video file path, Output directory
Output: png from frame to frame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*************************************************************************
> File Name: SplitVideo.cpp
> Author: zyy
> Mail: zyy34472@gmail.com
************************************************************************/


#include <unistd.h>
#include <iostream>
#include <opencv2/opencv.hpp>
#include <video.hpp>

using namespace std;
using namespace cv;

int main (int argc, char** argv)
{

if (argc < 3) {
cout << "[Usage] " << endl;
cout << "\t ./SplitVideo filename outputdir" << endl;
return -1;
}
string outdir = string(argv[2]);
string cmd = "mkdir " + outdir +" 2>/dev/null";
system(cmd.c_str());
VideoCapture vc;
vc.open(argv[1]);
if (!vc.isOpened()) {
cout << "Cann't open " << argv[1] <<endl;
return -1;
}
Mat frame;
for (int i = 1; vc.read(frame); i++)
imwrite(outdir+ "/" + to_string(i) + ".png", frame);
return 0;
}

STL ALGO SET OPERATION

Five Type of Set algorithms

In <stl_algo.h>

Set algorithms: includes, set_union, set_intersection, set_difference, set_symmetric_difference. All of these algorithms have the precondition that their input ranges are sorted and the postcondition that their output ranges are sorted.

Each algorithm has two impletementation, one of them with user-defined Compare function. In this article, we just list the first one and annotate requirement check code.

OpenCV3.0-RealSense-Configuration

1. Configure RealSense

Install the driver of RealSense F200/R200

EX:   
    intel_rs_dcm_f200_1.4.27.41944.exe
    intel_rs_dcm_r200_2.0.3.39488.exe

$ (RSSDK_DIR) / props

VS2010-13.Integration.MD.props
VS2010-13.Integration.MT.props

Visual Studio 2010-2013 Integration:

Open property manager in view->PropertyManager. 

For multi-threaded runtime projects, add property sheet
  VS2010-13.Integration.MT.props to all Debug/Release builds
For multi-threaded DLL runtime projects,
  Add property sheet VS2010-13.Integration.MD.props to all Debug/Release builds.

Build your SDK projects

2. Configure OpenCV 3.0

Install the Opencv 3.0 for Windows to

EX:    
    D:\OpenCV3.0

Configure environment variables

System variables:
[Path] add D:\OpenCV3.0\opencv\build\x86\vc12\bin
User variables:
new [opencv] = D:\OpenCV3.0\opencv\build\x86

New VS2013 Project

Include directory:
EX:
    D:\OpenCV3.0\opencv\build\include
    D:\OpenCV3.0\opencv\build\include\opencv
    D:\OpenCV3.0\opencv\build\include\opencv2
Library directory:
EX:
    D:\OpenCV3.0\opencv\build\x86\vc12\lib
Linker->Input->Additional dependencies
opencv_ts300d.lib
opencv_world300d.lib
libjasperd.lib
libjpegd.lib
libpngd.lib
libtiffd.lib
libwebpd.lib
opencv_calib3d300d.lib
opencv_core300d.lib
opencv_features2d300d.lib
opencv_flann300d.lib
opencv_highgui300d.lib
opencv_imgcodecs300d.lib
opencv_imgproc300d.lib
opencv_ml300d.lib
opencv_objdetect300d.lib
opencv_photo300d.lib
opencv_shape300d.lib
opencv_stitching300d.lib
opencv_superres300d.lib
opencv_video300d.lib
opencv_videoio300d.lib
opencv_videostab300d.lib
zlibd.lib
msvcrtd.lib
libcmtd.lib

Gedit Garbled

Gedit Garbled

1
2
3
gsettings set org.gnome.gedit.preferences.encodings auto-detected "['GB18030', 'GB2312', 'GBK', 'UTF-8', 'BIG5', 'CURRENT', 'UTF-16']"

gsettings set org.gnome.gedit.preferences.encodings shown-in-menu "['GB18030', 'GB2312', 'GBK', 'UTF-8', 'BIG5', 'CURRENT', 'UTF-16']"