Skip to content

Instantly share code, notes, and snippets.

@satoruhiga
Created December 26, 2011 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satoruhiga/1520713 to your computer and use it in GitHub Desktop.
Save satoruhiga/1520713 to your computer and use it in GitHub Desktop.
cvFlannTest.cpp
#include "testApp.h"
#include "ofxCv.h"
using namespace ofxCv;
using namespace cv;
#include <opencv2/flann/flann.hpp>
ofMesh mesh;
cv::flann::Index_<float> *flannIndex;
//--------------------------------------------------------------
void testApp::setup()
{
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(0);
for (int i = 0; i < 10000; i++)
{
ofVec3f v;
v.x = ofRandomWidth();
v.y = ofRandomHeight();
mesh.addVertex(v);
}
mesh.setMode(OF_PRIMITIVE_POINTS);
vector<ofVec3f>& vertices = mesh.getVertices();
Mat mat(vertices.size(), 3, CV_32F, &vertices[0]);
flannIndex = new cv::flann::Index_<float>(mat, cvflann::CompositeIndexParams());
}
//--------------------------------------------------------------
void testApp::update()
{
ofSetWindowTitle(ofToString(ofGetFrameRate(), 2));
}
//--------------------------------------------------------------
void testApp::draw()
{
ofEnableAlphaBlending();
const int num_n = 1000;
ofVec3f pos(mouseX, mouseY, 0);
Mat mat(1, 3, CV_32F, pos.getPtr());
Mat indices(1, num_n, CV_32S), dists(1, num_n, CV_32F);
int num = flannIndex->radiusSearch(mat, indices, dists, 10000, cvflann::SearchParams(512));
for (int i = 0; i < num; i++)
{
int n = indices.at<int>(i);
ofPoint t = mesh.getVertex(n);
float a = dists.at<float>(i);
a = ofMap(sqrt(a), 0, sqrt(10000), 255, 0, true);
ofSetColor(a);
ofLine(mouseX, mouseY, t.x, t.y);
}
ofSetColor(255);
mesh.draw();
ofNoFill();
ofCircle(mouseX, mouseY, 100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment