Created
December 26, 2011 07:45
-
-
Save satoruhiga/1520713 to your computer and use it in GitHub Desktop.
cvFlannTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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