#pragma once #define IMAGE_STORE_SIZE 256 #define MIN_TILE_SIZE 8 #define MAX_TILE_SIZE 16 #include "ofMain.h" #include "ofxJSONElement.h" #include "ofxOpenCv.h" //#include using namespace cvflann; long ofToLong(const string& intString); class imageStore : public ofThread{ public: float interval; //time between refreshes in seconds std::string instagram_url; ofxJSONElement response; std::map images; vector imageptrs; deque to_update; //Matrix dataset; doesn't need to be retained? //need to be able to add arbitrary data and keep it? //see pucilar behaviour if we delete the data after indexing it //we see some puzzling performance anomalies if we add data //maybe rebuild the index rather than use index.addPoints() //this way the data can be kept contiguously //or maybe just get a pointer to vector data with an offset vector data; //to begin, get a single point per image //retain a pointer to the flann indexer Index >* index; //Index > index(dataset, cv::flann::KDTreeIndexParams(4)); imageStore(){ instagram_url = "https://api.instagram.com/v1/tags/tycleeson/media/recent?client_id=c1d74d701fdf4ddd9f8d30ee9e8f944b"; interval=5.00f; ofImage img; img.allocate(MAX_TILE_SIZE,MAX_TILE_SIZE,OF_IMAGE_COLOR); img.clear(); img.setUseTexture(true); img.update(); images["000000"]=img; colours["000000"]=ofColor(0,0,0); } ~imageStore(){ delete index; } void set_interval(float _interval){ interval=_interval; } void start(){ startThread(true, false); // blocking, verbose } void stop(){ stopThread(); } //naive implementation //too slow! //looks shit! //how to make the search algorithm faster and better //http://www.semanticmetadata.net/lire/ std::map colours; ofImage& get_image(const ofColor& col){ //float shortest_dist=999999.0f; int sd=1000; ofImage& im=images.begin()->second; if( lock() ){ std::string s=images.begin()->first; for (map::iterator it=images.begin();it!=images.end();++it){ ofColor& c=colours[it->first]; int rd=c.v[0]-col.v[0]; int gd=c.v[1]-col.v[1]; int bd=c.v[2]-col.v[2]; //float dist=pow((float)((rd*rd)+(gd*gd)+(bd*bd)),0.5); int dist=abs(rd)+abs(gd)+abs(bd); if (distsecond; s=it->first; } } unlock(); } //cerr<<"got image "<second; if( lock() ){ float* test=new float[3]; test[0]=r; test[1]=g; test[2]=b; Matrix query(test,1,3); Matrix indices(new int[1], query.rows, 1); Matrix dists(new float[1], query.rows, 1); index->knnSearch(query, indices, dists, 1,SearchParams(4)); im=*imageptrs[*indices[0]]; //int i=rand()%imageptrs.size(); //im=images[imageptrs[i]]; //cerr<<"returning image "< dataset(&data[0],data.size()/3,3); index= new Index >(dataset, KDTreeIndexParams(4)); index->buildIndex(); unlock(); } cout << "Api: " << instagram_url<::iterator i=images.begin();i!=images.end();++i){ if(i->second.isUsingTexture()){ drawcount++; } } cout<<"loaded "<::iterator i=images.begin();i!=images.end();++i){ if(i->second.isUsingTexture()){ i->second.draw(ofToLong(i->first)%(long)(ofGetWidth()-i->second.getWidth()+1),ofToLong(i->first)%(long)(ofGetHeight()-i->second.getHeight()+1)); } } */ map::iterator it=images.begin(); if (it!=images.end()){ for (int i=0;isecond.isUsingTexture()){ it->second.draw(i*MAX_TILE_SIZE,j*MAX_TILE_SIZE); } it++; if (it==images.end()) it=images.begin(); } } } unlock(); } } };