added triangular mesh and scaling for z axis.

This commit is contained in:
Tom Schofield 2012-05-04 16:48:17 +01:00
parent a9c8a56850
commit 8b9c3eaebc
6 changed files with 191 additions and 76 deletions

View File

@ -42,6 +42,9 @@ ofxXmlSettings RefractiveIndex::XML;
void RefractiveIndex::setup()
{
camDist=1000;
//camera.setOrientation(ofVec3f(1,-1,1));
bool save_config = false;
cout << "Loading configuration..." << endl;
@ -109,7 +112,6 @@ void RefractiveIndex::setup()
_analysisVector.push_back(new ShadowScapesAnalysis(D));
_analysisVector.push_back(new RelaxRateAnalysis());
_analysisVector.push_back(new IResponseAnalysis());
@ -132,9 +134,7 @@ void RefractiveIndex::setup()
_currentAnalysis = NULL;
_state = ISTATE_UNDEF;
fbo.allocate(_vid_w,_vid_h);
_meshRotation=0;
}
@ -151,6 +151,16 @@ void RefractiveIndex::start_analysis()
_analysisAdapator = new AnalysisAdaptor(_currentAnalysis);
_currentAnalysis->setup(_vid_w, _vid_h);
_analysisAdapator->start();
//allocate fbo for HD
fbo.allocate(1920,1080);
// fbo.allocate( _currentAnalysis->_mesh_size_multiplier *_vid_w,_currentAnalysis->_mesh_size_multiplier * _vid_h);
camera.setPosition(fbo.getWidth()/2, fbo.getHeight()/2,_currentAnalysis->_mesh_size_multiplier *500);
_meshRotation=0;
}
void RefractiveIndex::stop_analysis()
@ -213,30 +223,46 @@ void RefractiveIndex::draw()
// black
ofBackground(0, 0, 0);
if(_currentAnalysis){
_currentAnalysis->draw();
if(_currentAnalysis->meshIsComplete){
fbo.begin();
fbo.begin();
glShadeModel(GL_SMOOTH);
ofClear(0,0,0);
camera.begin();
ofSetColor(0, 0, 0);
ofRect(0, 0, fbo.getWidth(), fbo.getHeight());
//this is a hack, I don't know how to colour the fbo with black pixels so I'm drawing a massive black rectangle in the background
ofPushMatrix();
ofTranslate(fbo.getWidth()/2, fbo.getHeight()/2);
ofRotateY(_meshRotation );
ofTranslate(-fbo.getWidth()/2, -fbo.getHeight()/2);
_meshRotation+=0.5;
ofTranslate(0, 0,-500);
ofRect(-fbo.getWidth(), -fbo.getHeight(), fbo.getWidth()*3, fbo.getHeight()*3);
ofPopMatrix();
ofSetColor(255);
float xDiff= (fbo.getWidth()- (_currentAnalysis->_mesh_size_multiplier *_vid_w))/2;
float yDiff= (fbo.getHeight()- (_currentAnalysis->_mesh_size_multiplier *_vid_h))/2;
ofTranslate(xDiff,yDiff,-_currentAnalysis->zPlaneAverage );
_currentAnalysis->aMesh.draw();
fbo.end();
camera.end();
fbo.end();
ofPixels pixels;
fbo.readToPixels(pixels);
ofSaveImage(pixels,_currentAnalysis->meshFileName, OF_IMAGE_QUALITY_BEST);
//saving jpgs doesn't work maybe because of of_image_quality
//saving jpgs doesn't work - pngs only!
}
}
}

View File

@ -10,6 +10,8 @@
#include "ofxOpenCv.h"
#include "ofxXmlSettings.h"
#include "ofxOpenCv.h"
#define MODE_DRAWING 0xEEFF
#define MODE_ANALYSING 0xFFEE
@ -55,7 +57,8 @@ protected:
vector<AbstractAnalysis*> _analysisVector;
float _meshRotation;
float camDist;
ofEasyCam cam;
public:
// acquisition
static ofPixels _pixels;
@ -70,8 +73,8 @@ public:
// this should be in xml
static string _location;
static ofxXmlSettings XML; // made this static so we can access RUN_NUM in the analyses
ofCamera camera;
ofPixels keepOnScreen;
ofFbo fbo;
ofLight light;
};

View File

@ -8,6 +8,8 @@
void AbstractAnalysis::setup(int camWidth, int camHeight) {
meshIsComplete=false;
imageForContourAvailable=false;
_cam_w = camWidth; _cam_h = camHeight;
if(RefractiveIndex::_mode == MODE_DRAWING) {
ofFileDialogResult r = ofSystemLoadDialog("choooose da folda", true);

View File

@ -62,10 +62,20 @@ public:
// event
ofEvent<string> _synthesize_cb;
bool meshIsComplete;
bool imageForContourAvailable;
ofMesh aMesh;
string meshFileName;
//difference between our image size and the size of the fbo
float widthScaleFactor;
float heightScaleFactor;
ofImage contourImage;
ofPixels meshPix;
ofPixels publicColorImage;
float zPlaneAverage;
//added Tom 1/5/12 defines the stretch we make to the mesh to make it fit HD proportions
float _mesh_size_multiplier;
protected:
int _cam_w, _cam_h;
string _whole_file_path_analysis, _whole_file_path_synthesis;

View File

@ -29,6 +29,8 @@ void IResponseAnalysis::setup(int camWidth, int camHeight)
meshIsComplete=false;
_gotFirstImage=false;
_mesh_size_multiplier=4;
//int acq_run_time = 20; // 20 seconds of acquiring per run
@ -175,24 +177,18 @@ void IResponseAnalysis::synthesise()
else{
imagePixels1[i]=0;
}
if(imagePixels2[i]-backgroundPixels[i]>0){
imagePixels2[i]-=backgroundPixels[i];
}
else{
imagePixels2[i]=0;
}
}
//update the images with their new background subtracted selves
image1.setFromPixels(imagePixels1);
image5.setFromPixels(imagePixels2);
//subtract background end///////////////
//flag the main app that we aren't read yet
meshIsComplete=false;
//make a mesh - this mesh will be drawn in the main app
setMeshFromPixels(make3DZmap(image1, image5, _background), image1,image5, aMesh);
setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1,image1, aMesh);
//meshPix=make3DZmap(image1, image5, _background);
//with jpgs this was refusing to save out
meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_IResponseSynthesis_"+ofToString(_run_cnt,2)+".png";
_saved_filenames_synthesis.push_back(meshFileName);
@ -200,6 +196,7 @@ void IResponseAnalysis::synthesise()
//flag that we are finished
meshIsComplete=true;
_synth_save_cnt++;
}
}
}
@ -417,65 +414,139 @@ void IResponseAnalysis::save_cb(Timer& timer)
saveImageAnalysis(file_name);
}
void IResponseAnalysis::setMeshFromPixels(ofPixels somePixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh){
void IResponseAnalysis::setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh){
int x=0;
int y=0;
//get rid of all previous vectors and colours
mesh.clear();
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
//unsigned char * thesePixels =currentSecondImage.getPixels();
ofColor meshColour=ofColor(255,0,0);
for(int i=0;i<somePixels.size();i+=3){
mesh.addVertex(ofVec3f(x,y,- somePixels.getColor(x, y).getBrightness() ));
// add colour from current second image of two - this is a hang over from when i was comparing two images
mesh.addColor( currentSecondImage.getColor(x, y) );
x++;
if(x>=somePixels.getWidth()){
x=0;
y++;
}
int chooseColour=1 ;
//the average z position of the matrix - used later to centre the mesh on the z axis when drawing
float zPlaneAverage=0;
for(int i=0;i<sPixels.size();i++){
zPlaneAverage+=sPixels[i];
}
if (sPixels.size()!=0) {
zPlaneAverage/=sPixels.size();
//cout<<zPlaneAverage<<" zPlaneAverage "<<endl;
}
else{
cout<<"DEPTH FLOAT ARRAY IS EMPTY";
}
if(chooseColour==1){
for(int i=0;i<sPixels.size();i++){
mesh.addColor( currentSecondImage.getColor(x, y+1) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*(y+1),- sPixels[ (currentSecondImage.getWidth()*(y+1))+x ] ));
mesh.addColor( currentSecondImage.getColor(x, y) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y+1) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1 ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y+1) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1] ));
mesh.addColor( currentSecondImage.getColor(x, y) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x +1 ]));
x++;
if(x>=currentSecondImage.getWidth()-1){
x=0;
y++;
//something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector?
if(y>=479){
break;
}
}
}
}
/*else{
for(int i=0;i<somePixels.getWidth()*(somePixels.getHeight()-1);i++){
ofVec3f U;
ofVec3f V;
//tri 1
U =ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- somePixels.getColor(x, y).getBrightness() ).operator- ( ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*(y+1),- somePixels.getColor(x, y).getBrightness() ) );
V =ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- somePixels.getColor(x+1, y+1).getBrightness() ).operator-(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- somePixels.getColor(x, y).getBrightness() ));
ofVec3f normal;
float mult=5.2;
normal = ofVec3f( (U.y*V.z)-(U.z-V.y) , (U.z*V.x)-(U.x*V.z) , (U.x*V.y)-(U.y*V.x) );
mesh.addColor(meshColour);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*(y+1),- mult*(1+somePixels.getColor(x, y+1).getBrightness() ) ));
mesh.addColor(meshColour);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- mult*(1+somePixels.getColor(x, y).getBrightness() ) ));
mesh.addColor(meshColour);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- mult*(1+somePixels.getColor(x+1, y+1).getBrightness()) ));
mesh.addNormal(normal);
//tri 2
U =ofVec3f(_mesh_size_multiplier*(x),_mesh_size_multiplier*y,- somePixels.getColor(x, y).getBrightness() ).operator-(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- somePixels.getColor(x+1, y+1).getBrightness() ) );
V =ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*y,- somePixels.getColor(x+1, y).getBrightness() ).operator-(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- somePixels.getColor(x+1, y+1).getBrightness() ) );
normal = ofVec3f( (U.y*V.z)-(U.z-V.y) , (U.z*V.x)-(U.x*V.z) , (U.x*V.y)-(U.y*V.x) );
mesh.addColor(meshColour);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- mult*(1+somePixels.getColor(x+1, y+1).getBrightness()) ));
// mesh.addColor( ofColor(255,10,10) );
mesh.addColor(meshColour);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x),_mesh_size_multiplier*y,- mult*(1+somePixels.getColor(x, y).getBrightness() ) ));
mesh.addColor(meshColour);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*y,-mult*(1+ somePixels.getColor(x+1, y).getBrightness()) ));
mesh.addNormal(normal);
x++;
if(x>=somePixels.getWidth()-1){
x=0;
y++;
if(y>=479){
break;
}
}
}*/
// add colour from current second image of two - this is a hang over from when i was comparing two image
}
ofPixels IResponseAnalysis::make3DZmap(ofImage &image1, ofImage &image2, ofImage &backgroundImag){
vector<float> IResponseAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){
ofPixels imagePixels1 = image1.getPixelsRef();
ofPixels imagePixels2 = image2.getPixelsRef();
ofPixels backgroundPixels = backgroundImag.getPixelsRef();
vector<float> differences;
ofPixels difference;
//this unsigned char should be unnecessary - I would have thought - can't you just address the pixel locations in ofPixels directly?
unsigned char * thesePixels = new unsigned char[ imagePixels1.getWidth()*imagePixels1.getHeight()*3];
for(int i=0;i<imagePixels1.size();i++){
thesePixels[i]=0;
}
int x=0;
int y=0;
int chooseComparison=1;
//previous versiom which compared two images -now deprecated
if(chooseComparison==0){
//for each pixel...
for(int i=0;i<imagePixels1.size();i+=3){
ofColor colourImage1 = imagePixels1.getColor(x, y);
ofColor colourImage2 = imagePixels2.getColor(x, y);
//the brightness difference at this pixel address for both images
int thisDiff=abs(colourImage1.getBrightness()-colourImage2.getBrightness());
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
if(x>=imagePixels1.getWidth()){
x=0;
y++;
}
}
}
//the current version compares how bright this pixel is with how bright it would be following inverse square fall off from centre
if(chooseComparison==1){
//for each pixel...
@ -483,14 +554,15 @@ ofPixels IResponseAnalysis::make3DZmap(ofImage &image1, ofImage &image2, ofImage
for(int i=0;i<imagePixels1.size();i+=3){
ofColor colourImage1 = imagePixels1.getColor(x, y);
ofColor colourImage2 = imagePixels2.getColor(x, y);
//ofColor colourImage2 = imagePixels2.getColor(x, y);
float _distanceToCentre=ofDist(imagePixels1.getWidth()/2, imagePixels1.getHeight()/2, x, y);
float _presumedBrightness=ofMap( sqrt(_maxPossibleDistanceToCentre)-sqrt(_distanceToCentre), 0, sqrt(_maxPossibleDistanceToCentre), 0, 255);
//float _presumedBrightness=255;
int thisDiff=abs(colourImage1.getBrightness()-_presumedBrightness);
//cout<<thisDiff<< " thisDiff "<<endl;
float multiplier=2.0;
differences.push_back(multiplier* thisDiff);
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
@ -505,7 +577,8 @@ ofPixels IResponseAnalysis::make3DZmap(ofImage &image1, ofImage &image2, ofImage
}
difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return difference;
return differences;
}

View File

@ -63,9 +63,10 @@ protected:
ofxCvColorImage cvConvertorImage;
//mesh making function
void setMeshFromPixels(ofPixels somePixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//returns ofPixels but these pixels actually hold depth data.
ofPixels make3DZmap(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
//ofPixels make3DZmap(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
bool _gotFirstImage;
ofImage _background;
};