Sift matching C++ source code / using opencv library

Created Date : 2011.10
Language : C/C++
Tool : Microsoft Visual C++ 2008
Library & Utilized : OpenCV 2.3
Reference : SIFT reference
etc. : template Image, WebCam

I made SIFT matching program using OpenCV 2.3.
I was wondering how to know the object pose.
In the internet, there are many source about sift, surf. But most of code introduced about only descripter and matching. There is no code to find object pose.
So I made this code and I should disclose this code.

This code uses openCV functions very useful.
cvExtractSURF, cvFindHomography...

I made matching code to the class. Class file name is MareMatchingClass.h/cpp.
You can use my class in the source very easily.

1. Create Matching class
   CMareMatchingClass MMathing;   

2.Input PatchImg

3.Find PatchImg in the background img
   MMathing.GetObjectRectAndBestH(BackGroundImg, &rect4pt);

4.Drawing the rect(rect4pt).
5.Repeat, go to the 3.

The class is consist of like below process;
1. Extract Feature -> use cvExtractSURF function
2. Find Matching point
3. Select some feature in the mached feature points, randomly.
4. calculate Homography matrix. This is geometry relationship between patch and background image.
5. transform features in the patch image by Homography matrix.
6. compare the transformed features to the background features.
7. evaluate how much is the homography exact.
7. repeat 4~6 and select best H.

<source code>

I think the source code is not best.
There are still shortage the source code.
It would need futher improvemnet.
so I want to discuss with you. Please leave your valueable opinion.
Thank you.
Have a nice day~. ^^

Oh~ english is very difficult.....


STL vector(with 2 more elements) sorting code / STL์—์„œ 2๊ฐœ ์ด์ƒ์˜ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” vector ์ •๋ ฌ code

The source is simple but I always don't remember the code well.
If you similar to me, refer to my code easily.

#include <iostream>
#include <cmath>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;

class element{
    element(float a, int b){
        value = a;
        index = b;
    float value;
    int index;

bool compare(const element &a, const element &b )
    return a.value < b.value; //์˜ค๋ฆ„์ฐจ์ˆœ(ascending order)
    //return a.value > b.value; //๋‚ด๋ฆผ์ฐจ์ˆœ(descending order)

void main()
    srand( (unsigned int)time(NULL));

    vector< element > A;

    // ๊ฐ’ ๋„ฃ๊ธฐ (input value)/////////////////////////////////////////////
    for(int i=0; i<20000; ++i){
        A.push_back( element(rand()%10, i) );

    //๊ฐ’ ์ถœ๋ ฅ  (data print)///////////////////////////////////////////////
    printf("์ •๋ ฌ ์ „(Before ordering) \n");
    for(i=0; i<20000; ++i){
        printf("A[%d] -> value %f :: index -> %d\n", i, A[i].value, A[i].index );

    //์ •๋ ฌ (sorting)

    //๊ฐ’ ์ถœ๋ ฅ (Value print)
    printf("์ •๋ ฌ ํ›„(After ordering)\n");
    for(i=0; i<20000; ++i){
        printf("A[%d] -> value %f :: index -> %d\n", i, A[i].value, A[i].index );


Incremental K-means matlab source code

Created Date : 2011.10.
Language : Matlab 2010
Tool : -
Library & Utilized :-
Reference :An Incremental K-means algorithm(D.T. Pham, S.S. Dimov and C.D. Nguyen)
Etc. :-

I made Incremental K-means algorithm as matlab source code.
I made the code base on above table which is introduced in the paper.
The incremental K-means is similar to K-means but the different point is number of cluster class is increasing. but we have to set the maximum number.

Below figure is result of clustering. the sample data is normal vector of the 3D point that is acquied by bumblebee.
I think the K-means algorithm is sensitive to error or outlier data. because the algorithm use euclidean distance. and The result of clustering is different every time because the initial position of the class is selected randomly.

I hope my code help to your problem and study.
thank you.
Give me your valuable comments. ^^

<source code>
Clustering result of the sample datas.

enlarge image of the above figure


BumBleBee 2D, 3D data acquisition source code / ๋ฒ”๋ธ”๋น„ 2D, 3D ๋ฐ์ดํ„ฐ ํš๋“ ์†Œ์Šค

Created Date : 2009.10.
Language : C++
Tool : Visual Studio C++ 2008
Library & Utilized : Point Grey-FlyCapture, Triclops, OpenCV 2.1
Reference : PointGrey Bumblebee Reference, www.cylod.com, www.ptgrey.com/
Etc. : STL

BumBleBee Stereo Camera Data Acquisition Source code.

This is Stereo Camera. The name is BumBleBee. This is product of PointGrey Company.
This camera is IEEE 1394 capble type.
This camera can obtain 3D cloud data rapidly and continously.

I need 2 library for using this camera(Triclops SDK, FlyCapture).
You can download these libs on the www.ptgrey.com site(support).
You have to use my source after install libs. and you have to set path(To include directory, lib directory). and you also need opencv 2.1 lib.

I made the acquisition code as class. The class name is CSensorStereo2.
You can use this class like below source code.
The sequence is 'Open->GetData->Close'.
I did that 2D data save Iplimage in opencv and 3D depth data save as Txt file.
The source code is very easy to use ^^.
If you have any question, Plz give your comments to me.

Thank you.

source code is shared on Github

#include <stdio.h>
#include "SensorStereo2.h"
#include <time.h>

void main()

int Width = 320;
int Height = 240; 

CSensorStereo2 CSS2;

char str[1000];

while(1) {

//get 1 frame data(Image, Depth information)

//Show Image

//Save Depth
printf("%s\n", str);

FILE * fp; fp = fopen(str,"w");
for(int i=0; i<Width; ++i)
  for(int j=0; j<Height; ++j)
    fprintf(fp,"%lf %lf %lf\n", CSS2.pDepth[i][j].x, CSS2.pDepth[i][j].y, CSS2.pDepth[i][j].z );


if(cvWaitKey(1) >= 0 )




Catmull-Rom Spline interpolation/ C++ source code/ ์Šคํ”Œ๋ผ์ธ ๋ณด๊ฐ„

Created Date : 2010.11.
Language : C++
Tool : Visual Studio C++ 2008
Library & Utilized : -
Reference :Catmull-Rom Spline reference
Etc. : STL

This program is Catmull-Rom Spline code.
Please search detail contents about Catmull-Rom Spline in the internet. ^^
In the main.cpp, the source code consisted of Input, calculate spline and output.
In the function of Catmull-Rom Spline, parameters means coordinate(x,y), Step, number of interpolation in a section.

You can get interpolation points easily using this function(PathToCurve).

์ด ์†Œ์Šค ์ฝ”๋“œ๋Š” Catmull-Rom ๋ณด๊ฐ„๋ฒ•์„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•œ ๊ฒƒ ์ž…๋‹ˆ๋‹ค.
์ด๋ก ์ ์ธ ๋‚ด์šฉ์€ ์ธํ„ฐ๋„ท์„ ๊ฒ€์ƒ‰ํ•˜์‹œ๋ฉด ๋งŽ์€ ์ž๋ฃŒ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์„œ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.
๋ฉ”์ธ ํ•จ์ˆ˜์—๋Š” ์ž…๋ ฅ, ๋ณด๊ฐ„, ์ถœ๋ ฅ์œผ๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๊ณ , ์ž…๋ ฅ์„ ์›ํ•˜๋Š” ์ขŒํ‘œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์—ฌ ํ•ด๋‹น ๋ฌธ์ œ์— ์ ์šฉํ•˜์‹œ๋ฉด๋  ๊ฑฐ์˜ˆ์š”.
๋ณด๊ฐ„ ํ•จ์ˆ˜๋Š” ์ขŒํ‘œ, ์Šคํ…,ํ•œ ๊ตฌ๊ฐ„ ๋ณด๊ฐ„ ๊ฐฏ์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
๋งคํŠธ๋žฉ์„ ํ†ตํ•˜์—ฌ ๋ณด๊ฐ„๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•ด๋ณด์„ธ์š”.
๊ฐ„๋‹จํ•œ ๋งคํŠธ๋žฉ ํŒŒ์ผ๋„ ์ฒจ๋ถ€ํ•ฉ๋‹ˆ๋‹ค.

์ข‹์€ ๋‹ต๋ณ€ ๋ฐ”๋ž๋‹ˆ๋‹ค. ^^

vector< pair<double , double > > InputPath;
FILE * fp1;
fp1 = fopen("input2.txt","r");
double x,y;
while( fscanf(fp1,"%lf %lf", &x, &y)!=EOF )
//printf("%lf %lf\n", x,y);
InputPath.push_back( make_pair(x,y) );

vector< pair<double , double > > curvePath;
curvePath = PathToCurve( InputPath, 1, 10);

FILE* fp2;
fp2 = fopen("out.txt","w");
for(int i=0; i<curvePath.size(); ++i)
fprintf(fp2, "%lf %lf\n", curvePath[i].first, curvePath[i].second );
//printf("%lf %lf\n", curvePath[i].first, curvePath[i].second );

5 6
3 4
3 2
5 2
7 4

1.000000 1.000000
1.174000 1.201000
1.392000 1.488000
1.648000 1.837000
1.936000 2.224000
2.250000 2.625000
2.584000 3.016000
2.932000 3.373000
3.288000 3.672000
3.646000 3.889000
4.000000 4.000000
4.359500 3.984500
4.736000 3.856000
5.126500 3.641500
5.528000 3.368000
5.937500 3.062500
6.352000 2.752000
6.768500 2.463500
7.184000 2.224000
7.595500 2.060500
8.000000 2.000000
8.400000 2.056000
8.800000 2.208000
9.200000 2.432000
9.600000 2.704000
10.000000 3.000000
10.400000 3.296000
10.800000 3.568000
11.200000 3.792000
11.600000 3.944000
12.000000 4.000000
12.418000 3.953000
12.864000 3.824000
13.326000 3.631000
13.792000 3.392000
14.250000 3.125000
14.688000 2.848000
15.094000 2.579000
15.456000 2.336000
15.762000 2.137000
16.000000 2.000000

plz give me your valuable comment. Thank you.


Rect Corner Detection / C++, OpenCV1.0 / ์‚ฌ๊ฐํ˜• ๋ชจ์„œ๋ฆฌ ์ฐพ๊ธฐ

Created Date : 2008.11.
Language : C++
Tool : Visual Studio C++6.0
Library & Utilized :OpenCV 1.0
Reference : -
Etc. : -

This program is for detecting 4 corners of rects(3 rect).
The detection process is shown below.

1. Candidate corner detection using cvGoodFeaturesToTrack function.
2. Blob Coloring - Find 3 biggest blobs
3. Angle inspection tracking outline of blob. - using cvConvexHull2 function.
    Corner's angle would be big than other angles. so we select 4 corners.

<Source Code>

๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์œผ๋กœ ์‚ฌ๊ฐํ˜• ์บ˜๋ฆฌ๋ธŒ๋ ˆ์ด์…˜ ํƒ€์ผ“์˜ ๋ชจ์„œ๋ฆฌ 4๊ตฐ๋ฐ๋ฅผ ์ฐพ๋Š”๋‹ค.

์บ˜๋ฆฌ๋ธŒ๋ ˆ์ด์…˜ ํƒ€์ผ“์˜ ์ฝ”๋„ˆ lefttop, righttop, rightbottom, leftbottom์„ ๊ฒ€์ถœํ•œ๋‹ค.

cvGoodFeaturesToTrack๋กœ ์ฝ”๋„ˆ ๊ฒ€์ถœ
CBlobResult๋กœ Blob Coloringํ•˜์—ฌ ๊ฐ€์žฅ ํฐ ๋ฉ์–ด๋ฆฌ 3๊ฐœ๋ฅผ ์ฐพ์Œ (์‚ฌ๊ฐํ˜• 3๊ฐœ)
๊ฐ Blob์— ๋Œ€ํ•ด cvConvexHull2๋กœ ์™ธ๊ณฝ ์  ๊ฒ€์ถœ
์™ธ๊ณฝ์ ์„ ๋”ฐ๋ผ๊ฐ€๋ฉด์„œ ์  ๊ฐ„์— ๊ฐ์„ ์ €์žฅ
์  ๊ฐ„์˜ ๊ฐ๋„ ์ค‘ ๊ฐ€์žฅ ๊ฐ์ด ํฐ 4์ ์„ ์„ ํƒํ•˜์—ฌ ๋ชจ์„œ๋ฆฌ๋กœ ํŒ๋‹จํ•จ
ํŒ๋‹จ๋œ 4์ ์—์„œ ์ฝ”๋„ˆ์ ๊ณผ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด 4์ ์„ ์„ ํƒํ•จ
์ด๋ ‡๊ฒŒ ํ•œ ์‚ฌ๊ฐํ˜•์— ๋Œ€ํ•œ ๋ชจ์„œ๋ฆฌ ์ ์ด ์„ ํƒ๋จ

๊ฒฐ๊ณผ๋Š” ์„ธ๊ฐœ์˜ ์‚ฌ๊ฐํ˜•์— ๋Œ€ํ•œ
Left Top, Right Top, Right Bottom, Left Bottom ์˜ ์ด๋ฏธ์ง€ ์ขŒํ‘œ๋ฅผ ์•Œ๋ ค์คŒ
๋”ฐ๋ผ์„œ ์นด๋ฉ”๋ผ ์บ˜๋ฆฌ๋ธŒ๋ ˆ์ด์…˜์„ ์œ„ํ•œ ์›”๋“œ ์ขŒํ‘œ - ์ด๋ฏธ์ง€ ์ขŒํ‘œ ๊ฐ’์„ ์ž๋™์œผ๋กœ ์ถ”์ถœ ๊ฐ€๋Šฅํ•จ
 <Source Code>


10th unmanned vehicle contest. The sponsor is Hyundai-Kia motors(Korea)

In 2010, I entered 10th unmanned vehicle contest.
The competition was sponsored by by Hyundai-Kia Motors.
In the our team, my part was software.
I had to solve many problems.
For example; Obstacle Detection and avoidance, mapping, path planing, Localization...
During prepare the competition, I programed nearly 30,000 lines. ^^
My team didn't acomplish the win. But our vehicle have finished all course as unmanned.
And I did my best. so I do not regret it.

OBS have broadcasted this competition.

This movie captured while unmaned vehicle driving.

This is architecture of software.

We have used many sensors and equipments.