flann function in opencv is good for point match, which find nearest point from A points to B points, it is useful for tracking, iterative closest point (ICP) algorithm...
I copy the code from this website:
https://github.com/royshil/morethantechnical/blob/master/ICP/MyICP.cpp
and make it simple for me to understand.
code:
#include "cv.h"
#include "highgui.h"
#include <iostream>
#include <vector>
#include <limits>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Mat X(5,2,CV_32SC1);
X.at<Point>(0,0)=Point(1,1);
X.at<Point>(1,0)=Point(5,5);
X.at<Point>(2,0)=Point(10,10);
X.at<Point>(3,0)=Point(15,15);
X.at<Point>(4,0)=Point(20,20);
Mat destinations(10,2,X.type());
destinations.at<Point>(0,0)=Point(30,30);
destinations.at<Point>(1,0)=Point(6,6);
destinations.at<Point>(2,0)=Point(11,11);
destinations.at<Point>(3,0)=Point(17,17);
destinations.at<Point>(4,0)=Point(23,23);
destinations.at<Point>(5,0)=Point(25,25);
destinations.at<Point>(6,0)=Point(2,2);
destinations.at<Point>(7,0)=Point(16,16);
destinations.at<Point>(8,0)=Point(40,40);
destinations.at<Point>(9,0)=Point(21,21);
cv::Mat m_indices(X.rows, 1, CV_32S);
cv::Mat m_dists(X.rows, 1, CV_32F);
Mat dest_32f; destinations.convertTo(dest_32f,CV_32FC2); //convert
Mat X_32f; X.convertTo(X_32f,CV_32FC2);
cv::flann::Index flann_index(dest_32f, cv::flann::KDTreeIndexParams(2)); // using 2 randomized kdtrees, 1 is ok ,too
flann_index.knnSearch(X_32f, m_indices, m_dists, 1, cv::flann::SearchParams(64) );
cout<<"indice:"<<m_indices<<endl;
cout<<"dist :"<<m_dists<<endl;
for(int i=0;i<5;i++)
{
cout<<i<<" in X match to "<<m_indices.at<int>(i)<<" in destinations"<<endl;
cout<<"distance is : "<<m_dists.at<float>(i)<<endl;
}
system("pause");
}
the output should be:
indice:[6;
1;
2;
7;
9]
dist :[2;
2;
2;
2;
2]
0 in X match to 6 in destinations
distance is : 2
1 in X match to 1 in destinations
distance is : 2
2 in X match to 2 in destinations
distance is : 2
3 in X match to 7 in destinations
distance is : 2
4 in X match to 9 in destinations
distance is : 2
Press any key to continue . . .
2017年6月2日 星期五
2017年2月1日 星期三
simple opencv drawing opengl example
the opencv should be cmaked and check the "WITH_OPENGL"draw 2 sphere and a rotating cube in opencv by opengl using opencv setopengldrawcallback
float gggg=55;
float gginin=3.14/6;
void on_opengl(void* param)
{
//draw a cube
glLoadIdentity();
gluLookAt(20*cos(gginin), 0, 20*sin(gginin), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glTranslated(0.0, 0.0, -1.0);
glDrawArrays(GL_POINTS, 0, 1);
glRotatef( gggg, 1, 0, 0 );
//glRotatef( gggg, 0, 1, 0 );
//glRotatef( gggg, 0, 0, 1 );
static const int coords[6][4][3] = {
{ { +1, -1, -1 }, { -1, -1, -1 }, { -1, +1, -1 }, { +1, +1, -1 } },
{ { +1, +1, -1 }, { -1, +1, -1 }, { -1, +1, +1 }, { +1, +1, +1 } },
{ { +1, -1, +1 }, { +1, -1, -1 }, { +1, +1, -1 }, { +1, +1, +1 } },
{ { -1, -1, -1 }, { -1, -1, +1 }, { -1, +1, +1 }, { -1, +1, -1 } },
{ { +1, -1, +1 }, { -1, -1, +1 }, { -1, -1, -1 }, { +1, -1, -1 } },
{ { -1, -1, +1 }, { +1, -1, +1 }, { +1, +1, +1 }, { -1, +1, +1 } }
};
for (int i = 0; i < 6; ++i) {
glColor3ub( i*20, 100+i*10, i*42 );
glBegin(GL_QUADS);
for (int j = 0; j < 4; ++j) {
glVertex3d(0.2 * coords[i][j][0], 0.2 * coords[i][j][1], 0.2 * coords[i][j][2]);
}
glEnd();
}
//draw a sphere
glLoadIdentity();
gluLookAt(20*cos(gginin), 0, 20*sin(gginin), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glTranslatef(1.0f,2.0f,1.0f);
glColor3ub( 255,0,0);
glBegin(GL_TRIANGLES);
GLUquadricObj *quadric;
quadric = gluNewQuadric();
gluQuadricDrawStyle(quadric, GLU_FILL );
gluSphere( quadric , .1 , 36 , 18 );
glEnd();
//draw another sphere
glLoadIdentity();
gluLookAt(20*cos(gginin), 0, 20*sin(gginin), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glTranslatef(4.1f, 2.45f, 10.0f);
glBegin(GL_TRIANGLES);
GLUquadricObj *quadric2;
quadric2 = gluNewQuadric();
gluQuadricDrawStyle(quadric2, GLU_FILL );
gluSphere( quadric , .6 , 36 , 18 );
glEnd();
glLoadIdentity();
}
int _tmain(int argc, _TCHAR* argv[])
{
string openGLWindowName = "OpenGL Test";
namedWindow(openGLWindowName, WINDOW_OPENGL);
resizeWindow(openGLWindowName, 640, 480);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective(90.0, 640/480, 1, 1000000.0);
glMatrixMode(GL_MODELVIEW);
setOpenGlContext(openGLWindowName);
setOpenGlDrawCallback(openGLWindowName, on_opengl, NULL);
while(true)
{
updateWindow(openGLWindowName);// when needed
gggg++;
gginin+=3.14/36;
cvWaitKey(30);
cout<<"ggininder"<<endl;
}
return 0;
}
訂閱:
文章 (Atom)