Thursday, July 12, 2012

C/C++ Programming Notes 2: int to string conversion

For computer vision related experiments, output experiments results on top of the images is usually a convenient way to check the results, such as Person Id, location pixel values and so on.

However, most likely, the putText like functions, prefer "string" as the input type. Then how are we going to convert the digital (int/double) data type to string, is a small but important question. 

In Matlab: 
str = ['PersonId ' num2str(id)];

In C++
#include <iostream>
#include <sstream>

using namespace std;
int main(int argc, char *argv[]) {


    std::ostringstream convert;
    int id = 5;
    string txt;
    convert << id;
    txt = "PersonId " + convert.str();
    cout << txt << endl;
    return 0;
}

C/C++ Programming Notes 1. dynamic allocate memory

In the following example: I will show you three different way to dynamically allocate memory in run time.
1. malloc/free, which is the C style programming
2. new/delete, which is the C++ style programming
3. vector, which is the STL style programming

About which is better than the others, it is really depend on your application. However, normally speaking, if you are programming for C++, new / delete is alway recommended than malloc/free. Comparing to new/delete and vector. I would prefer vector, since the STL provide various interfaces for us to effectively manage our memory.

Example Code:


#include <iostream>
#include <vector>

using namespace std;
int main(int argc, char *argv[]) {
// dynamic alloc by using malloc/free 
int *arr_malloc;
int count = 5;
// dynamic alloc by using malloc/free
for (int i = 1; i <= count; i++){
arr_malloc = (int*)malloc(i);
for(int j = 0; j < i; j++){
arr_malloc[j] = j * 2;
}
for(int j = 0; j < i; j++){
cout << arr_malloc[j] << " ";
}
cout << endl;
}
free(arr_malloc);
// dynamic alloc by using new/delete
cout << "new/delete" << endl;
for(int i = 1; i <= count; i++){
int* arr_new = new int[i];
for(int j = 0; j < i; j++){
arr_new[j] = j * 2;
}
for (int j = 0; j < i; j++) {
cout << arr_new[j] << " ";
}
cout << endl;
delete [] arr_new;
}
// dynamics alloc by using vector
cout << "vector" << endl;
vector<int> arr_vector;
for (int i = 1; i <= count; i++) {
arr_vector.resize(i);
for(int j = 0; j < i; j++){
arr_vector[j] = j * 2;
}
for (int j = 0; j < i; j++) {
cout << arr_vector[j] << " ";
}
cout << endl;
arr_vector.clear();
}
return 0;
}

Monday, July 9, 2012

Gonna be due tomorrow

To my little princess:

Look at your mom day, she is still so lovely and cute, isn't she?_^ Look how big you are. Isn't you really like me, just too shine and slow mood in your own pace for everything? Well, even though your mom doesn't like that, I do. :)

Take your time and be prepared, my little princess. We are ready for you anytime. :) these days mom has been working and work out a lot. She is preparing for your arrival.

Recently, you have been moved slower and more gentle than before. Are you also saving energy to come out? Don't worry, once you grown up, daddy could work out with you, if you like of course. :)

Mom is still picking and window shopping in Anthropologie. Sleep tight and do your best for us all. :)

Best,
Binlong Li

Sunday, July 8, 2012

A good C++ lib for Machine Learning, Pattern Recognition students

Just find a good lib including various machine learning / optimization algorithms implemented, which is cross-platforms and with modern C++ techniques.

http://dlib.net

Note:
This is a really good place to learn some C++ programming for optimization and machine learning.

Of course, I have just found this place. I, myself, still need some time to dig and play with this library. I will keep it updated. :)

PS: another good MATLAB implementation for Hungarian algorithm: (This  link only for my own ref)

http://www.mathworks.com/matlabcentral/fileexchange/26836-lapjv-jonker-volgenant-algorithm-for-linear-assignment-problem-v2-5


Saturday, July 7, 2012

Baby is still there

Hi Yihan, my baby,
    Even though, these days, mom tried various methods to "chase" you out, such as stairs, raspberry leaf tea, and so on. You are still sooooo calm. Isn't it mom's womb just too comfortable, and the outside world just a little bit too scary...

    Don't be afraid my princess, you will be just fine. Mom, daddy and your grand parents, are all looking forward to you. We will all love you without any hesitance. We only hope you gonna be healthy and happy. You will be just fine.

    Be brave my little angle.

From Daddy,
Binlong Li

(PS: later, the blogs with "Letter to the future" Label, will be the message to my coming little princess. When she grown up, she might be interested at reading this little corners, where her father left for her to understand the small little steps she has even been. )

Friday, July 6, 2012

Convert images into video in Mac OSX

When people do experiments, it always happened to saving the results into video. However, generating video by using openCV or other tools sometimes will leading to a pain, due to the variance of systems and software.

Therefore, it's always easier to save results as images, then convert them into video. Under Windows OS, you could easily do this with VirtualDub. However, VirtualDub is a Windows Only software. Therefore, under Mac OSX, it is not an option.

Fortunately, VirtualDub is based on ffmpeg, which is designed to be cross-platform. Therefore, we could always use ffmpeg command line to covert images into video.

Suppose all the images are under one directory named: ./samples, and images are named with pattern: img_0000.jpg  img_0001.jpg ... img_5024.jpg and so on. The desired output video name is: test.mp4

After "cd" to the ./samples folder. The following command line will be enough:

ffmpeg -qscale 1 -r 20 -b 9600 -i img_%4d.jpg test.mp4

Here, the img_%4d.jpg section could be modified depend on your own images pattern.

Adaboost algorithm

Adaboost, which stands for adaptive boost, is a powerful classification techniques, by utilizing multiple weak classifiers. It implies inner connection with SVM. In some recent papers. It has been used for feature selection of multiple person tracking related algorithms.

I, myself, have known about this algorithm for a very long time, since the class of pattern recognition, (taught by Prof. Jennifer Dy in Northeastern University). However, it has always been a mystery for me since then. However, today, I found an excellent tutorial. (A Short Introduction to Boosting, http://www.yorku.ca/gisweb/eats4400/boost.pdf)

The tutorial could be a good supplementary materials for students to read. Before or after reading the tutorial, I would recommend people to read Adaboost documents for OpenCV, which is a open Computer Vision Library. This documents as a contact version of the Adaboost algorithm, with a decent implementation. The link is as following: http://opencv.willowgarage.com/documentation/cpp/boosting.html

Thursday, July 5, 2012

Come back to blog

Just read one article in weibo, I believe, it's good for me to keep writing blogs. It might be a small gift to my on coming daughter.
This blog might include both Chinese and English articles.
Ps: I am writing this with " Blogger" iPhone app from google.

prettify