ICF(Integral Channel Features) + WaldBoost example (opencv, ICFDetector uses example)

Refer to this page about official reference of ICF

Other page for referencing
http://d.hatena.ne.jp/takmin/20151218/1450447204 (in Japaneses)

ICF+WaldBoost is upgraded on Google Summer of Code 2015(GSC).
And For use this, you build OpenCV included opencv_contrib.
Github address is -> https://github.com/Itseez/opencv_contrib
ICF is located in xobjdetect module.

The method to build including extra module(opencv_contrib) is refer to this page.

ICF(integral Channel Features) and ACF is feature like Haar and WaldBoost is learning algorithm like AdaBoost.
WaldBoost and ICF is a pair. For using ICF feature, WaldBoost algorithm must be run in OpenCV.

Good result Introduced in the Internet but,
Parameter settings for best learning is difficult and uncomfortable showing the learning process.

Anyway, let's find how to use it.

0. Data prepare part.
- make Positive images path list and Negative images path list.
- don't worry size and color channel, when learning these are corrected.
- But if negative image size is small(correction size I don't know), error is occurred when learning part.
int main(int, char)
 int showOrno = 1;

 FILE* fp1;
 fopen_s(&fp1, "positive.txt", "w");
 int n = 1500; //positive data count
 char str[100];
 //char str1[100] = "M:\\____videoSample____\\Pedestrian\\6464\\"; //base path
 char str1[100] = "M:\\____videoSample____\\Pedestrian\\"; //base path
 Mat testImg;
 for (int i = 1; i <= n; ++i)
  sprintf_s(str, "%sp%d.jpg", str1, i); //make string  
  testImg = imread(str); //img loading and check
  if (testImg.empty())
   printf("image loading error\n");
   return 0;
  printf("(%d/%d) - %s - %d,%d\n", i, n, str,testImg.cols, testImg.rows); //check string
  if (showOrno)
   imshow("p_test", testImg);
  fprintf_s(fp1, str); //write
  fprintf_s(fp1, "\n");

 FILE* fp2;
 fopen_s(&fp2, "negative.txt", "w");
 char base_str2[100] = "M:\\____videoSample____\\allBack\\";
 int n2 = 3000;
 for (int i = 1; i <= n2; ++i)
  sprintf_s(str, "%sn%d.jpg", base_str2, i); 
  testImg = imread(str);
  if (testImg.empty())
   printf("image loading error\n");
   return 0;
  //resize(testImg, testImg, Size(500,500));
  //imwrite(str, testImg);
  printf("(%d/%d) - %s - %d,%d\n", i, n2, str, testImg.cols, testImg.rows); //check string
  if (showOrno)
   imshow("n_test", testImg);
  fprintf_s(fp2, str);
  fprintf_s(fp1, "\n");


 // the camera will be deinitialized automatically in VideoCapture destructor
 return 0;

1. learning part.
It is difficult to discover exact paramer values.

void main()
 cv::String pos_list = "positive.txt"; // positive image list
 cv::String neg_list = "negative.txt"; // negative image list

 //cv::String pos_list = "poslist.txt"; // positive image list
 //cv::String neg_list = "neglist.txt"; // negative image list

 // string list to vector
 std::vector< cv::String > pos_img_files, neg_img_files;
 ReadList(pos_list, pos_img_files);
 ReadList(neg_list, neg_img_files);

 // set icf parameter
 cv::xobjdetect::ICFDetectorParams icf_params;
 icf_params.feature_count = 250; // 25000; //features count
 icf_params.weak_count = 100; //weak classifier count of using waldboost in cascade
 icf_params.features_type = "icf"; // "icf"; //icf or acf
 icf_params.bg_per_image = 5; //how many background image creation in a image
 icf_params.model_n_cols = 64; // 96; //model width size
 icf_params.model_n_rows = 64; // 160; //model height size
 icf_params.alpha = 0.02; //may be false negative rate in using waldboost
 icf_params.is_grayscale = false; // false; //gray scale image?
 icf_params.use_fast_log = false; //use fast log function
 // learning
 cv::xobjdetect::ICFDetector icf_detector;
 icf_detector.train(pos_img_files, neg_img_files, icf_params); //some said 3 hours takes when learning 2416 postive/1218 background image

 // save result of learning
 cv::FileStorage cvfs("icf_model.txt", cv::FileStorage::WRITE); //
 cvfs << "icf"; // save 
2. detection part.
values are scores matched rect vector.
I think 1 is best. 0 is worst.
void main()

 cv::xobjdetect::ICFDetector icf_detector;

 // model loading
 cv::FileStorage cvfs("icf_model.txt", cv::FileStorage::READ);

 // test image
 std::string img_file = "pp.jpg";
 cv::Mat img = cv::imread(img_file);

 // image detect
 std::vector < cv::Rect > objs;
 std::vector < float > values;
 icf_detector.detect(img, objs, 1.2, cv::Size(128, 128), cv::Size(256, 256), 8.0, 20, values);
 char TestStr[100];

 // rect draw
 for (int i = 0; i < objs.size(); ++i)
  rectangle(img, objs[i], Scalar(255, 0, 0));
  sprintf_s(TestStr, "%0.2lf", values[i]);
  //check score
  printf("[%d] = %lf\n", i, values[i]);  
  putText(img, TestStr, Point(objs[i].x, objs[i].y), CV_FONT_NORMAL, 1, Scalar(255, 255, 255), 1); //OutImg is Mat class; 
 namedWindow("test", 0);
 imshow("test", img);


this is some result.


  1. according to your result image dedector correctnes is not good ist it ?

    1. I still did not get good result.
      But I cannot say the detector is bad.
      Because I don't understand the algorithm.
      Thank you.

  2. Hi, did you get better results? Thanks for sharing your example.

  3. Hi, did you get better results? Thanks for sharing your example.


Post a Comment

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

Optical Flow sample source code using OpenCV

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

SICK LMS511 sensor data acquisition interface (source code, C++/MFC)

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)