added zmapper from inverse square law saves from main app draw()

This commit is contained in:
Tom Schofield 2012-04-12 10:21:37 +01:00
parent e29accb9ce
commit 21791b9769
7 changed files with 190 additions and 48 deletions

View File

@ -19,7 +19,7 @@
#define CAMERA_ACQU_WIDTH 640 #define CAMERA_ACQU_WIDTH 640
#define CAMERA_ACQU_HEIGHT 480 #define CAMERA_ACQU_HEIGHT 480
#define LOCATION "MIDDLESBOROUGH" #define LOCATION "BRADFORD"
#define ISTATE_UNDEF 0xEEEE #define ISTATE_UNDEF 0xEEEE
#define ISTATE_START 0xAAAA #define ISTATE_START 0xAAAA
@ -133,6 +133,9 @@ void RefractiveIndex::setup()
_currentAnalysis = NULL; _currentAnalysis = NULL;
_state = ISTATE_UNDEF; _state = ISTATE_UNDEF;
fbo.allocate(_vid_w,_vid_h);
_meshRotation=0;
} }
void RefractiveIndex::analysis_cb(string & analysis) void RefractiveIndex::analysis_cb(string & analysis)
@ -211,8 +214,30 @@ void RefractiveIndex::draw()
// black // black
ofBackground(0, 0, 0); ofBackground(0, 0, 0);
if(_currentAnalysis) if(_currentAnalysis){
_currentAnalysis->draw(); _currentAnalysis->draw();
cout<<_currentAnalysis->meshIsComplete<<endl;
if(_currentAnalysis->meshIsComplete){
fbo.begin();
ofSetColor(0, 0, 0);
ofRect(0, 0, fbo.getWidth(), fbo.getHeight());
ofPushMatrix();
ofTranslate(fbo.getWidth()/2, fbo.getHeight()/2);
ofRotateY(_meshRotation );
ofTranslate(-fbo.getWidth()/2, -fbo.getHeight()/2);
_meshRotation+=0.5;
_currentAnalysis->aMesh.draw();
fbo.end();
ofPixels pixels;
fbo.readToPixels(pixels);
cout<<_currentAnalysis->meshFileName<<endl;
ofSaveImage(pixels,_currentAnalysis->meshFileName, OF_IMAGE_QUALITY_BEST);
//saving jpgs doesn't work maybe because of of_image_quality
}
}
} }
void RefractiveIndex::setup_camera() void RefractiveIndex::setup_camera()

View File

@ -54,6 +54,8 @@ protected:
AnalysisAdaptor* _analysisAdapator; AnalysisAdaptor* _analysisAdapator;
vector<AbstractAnalysis*> _analysisVector; vector<AbstractAnalysis*> _analysisVector;
float _meshRotation;
public: public:
// acquisition // acquisition
static ofPixels _pixels; static ofPixels _pixels;
@ -69,4 +71,7 @@ public:
static string _location; static string _location;
static ofxXmlSettings XML; // made this static so we can access RUN_NUM in the analyses static ofxXmlSettings XML; // made this static so we can access RUN_NUM in the analyses
ofFbo fbo;
}; };

View File

@ -7,8 +7,8 @@
void AbstractAnalysis::setup(int camWidth, int camHeight) { void AbstractAnalysis::setup(int camWidth, int camHeight) {
meshIsComplete=false;
_cam_w = camWidth; _cam_h = camHeight; _cam_w = camWidth; _cam_h = camHeight;
if(RefractiveIndex::_mode == MODE_DRAWING) { if(RefractiveIndex::_mode == MODE_DRAWING) {
ofFileDialogResult r = ofSystemLoadDialog("choooose da folda", true); ofFileDialogResult r = ofSystemLoadDialog("choooose da folda", true);
if(!r.bSuccess) { if(!r.bSuccess) {
@ -50,6 +50,8 @@ void AbstractAnalysis::do_synthesize() {
case MODE_DRAWING: case MODE_DRAWING:
{ {
ofxFileHelper fileHelperDrawing; ofxFileHelper fileHelperDrawing;
if(!fileHelperDrawing.doesDirectoryExist(_whole_file_path_synthesis)){ if(!fileHelperDrawing.doesDirectoryExist(_whole_file_path_synthesis)){
fileHelperDrawing.makeDirectory(_whole_file_path_synthesis); fileHelperDrawing.makeDirectory(_whole_file_path_synthesis);
@ -153,6 +155,7 @@ void AbstractAnalysis::create_dir_allocate_images()
myGrayImage1.setUseTexture(false); myGrayImage1.setUseTexture(false);
myGrayImage1.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, OF_IMAGE_GRAYSCALE); myGrayImage1.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, OF_IMAGE_GRAYSCALE);
//////////////////////////////END ALLOCATE IMAGES ////////////////////////////////////////////////// //////////////////////////////END ALLOCATE IMAGES //////////////////////////////////////////////////
} }

View File

@ -61,6 +61,10 @@ public:
// event // event
ofEvent<string> _synthesize_cb; ofEvent<string> _synthesize_cb;
bool meshIsComplete;
ofMesh aMesh;
string meshFileName;
protected: protected:
int _cam_w, _cam_h; int _cam_w, _cam_h;

View File

@ -1,4 +1,4 @@
/* /*
~ author: dviid ~ author: dviid
~ contact: dviid@labs.ciid.dk ~ contact: dviid@labs.ciid.dk
*/ */

View File

@ -25,6 +25,10 @@ void IResponseAnalysis::setup(int camWidth, int camHeight)
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_iresponse", ACQUIRE_TIME); acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_iresponse", ACQUIRE_TIME);
cout << "ACQUIRE_TIME IResponseAnalysis " << acq_run_time << endl; cout << "ACQUIRE_TIME IResponseAnalysis " << acq_run_time << endl;
//flag for main sketch
meshIsComplete=false;
_gotFirstImage=false;
//int acq_run_time = 20; // 20 seconds of acquiring per run //int acq_run_time = 20; // 20 seconds of acquiring per run
@ -125,6 +129,8 @@ void IResponseAnalysis::acquire()
void IResponseAnalysis::synthesise() void IResponseAnalysis::synthesise()
{ {
cout<<"SYNTHESISING IRESPONSE";
//cout << "IResponseAnalysis::saving synthesis...\n"; //cout << "IResponseAnalysis::saving synthesis...\n";
if(_state == STATE_STOP) return; if(_state == STATE_STOP) return;
@ -147,50 +153,51 @@ void IResponseAnalysis::synthesise()
if(image5.loadImage(_saved_filenames_analysis[i+1])){ if(image5.loadImage(_saved_filenames_analysis[i+1])){
///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES //////////////////////////
if(!_gotFirstImage){
cout<<"background image is"<< _saved_filenames_analysis[i]<<endl;
_background=image1;
_gotFirstImage=true;
}
cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height); //subtract background begin///////////////
cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height);
cvColorImage1.convertToGrayscalePlanarImage(cvGrayImage1, 1); ofPixels imagePixels1 = image1.getPixelsRef();
cvColorImage2.convertToGrayscalePlanarImage(cvGrayImage2, 1); ofPixels imagePixels2 = image5.getPixelsRef();
ofPixels backgroundPixels = _background.getPixelsRef();
cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1); for(int i=0;i<imagePixels1.size();i++){
cvGrayDiff1.erode();
cvGrayDiff1.contrastStretch();
//cvGrayDiff1.blur(5);
//cvGrayDiff1.dilate();
//cvGrayDiff1.contrastStretch();
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// unsigned char val=imagePixels1[i];
string file_name; // cout<<(int)backgroundPixels[i]<< " thesePixels[i] "<<(int)imagePixels1[i]<<endl;
if(imagePixels1[i]-backgroundPixels[i]>0){
imagePixels1[i]-=backgroundPixels[i];
}
else{
imagePixels1[i]=0;
}
if(imagePixels2[i]-backgroundPixels[i]>0){
imagePixels2[i]-=backgroundPixels[i];
}
else{
imagePixels2[i]=0;
}
file_name = ofToString(_synth_save_cnt, 2)+"_IResponseSynthesis_"+ofToString(_run_cnt,2)+".jpg"; }
//update the images with their new background subtracted selves
image1.setFromPixels(imagePixels1);
image5.setFromPixels(imagePixels2);
//subtract background end///////////////
//<---- THE OLD WAY OF SAVING - works on OSX but generates BLACK FRAMES on WINDOWS ----> //flag the main app that we aren't read yet
// ofSaveImage(cvGrayImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST); meshIsComplete=false;
//make a mesh - this mesh will be drawn in the main app
setMeshFromPixels(make3DZmap(image1, image5, _background), image1,image5, aMesh);
//<---- NEW SAVING - seems to fix WINDOWS saving out BLACK FRAMES PROBLEM ----> //with jpgs this was refusing to save out
//ofImage image; meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_IResponseSynthesis_"+ofToString(_run_cnt,2)+".png";
//image.allocate(cvGrayDiff1.width, cvGrayDiff1.height, OF_IMAGE_GRAYSCALE); //flag that we are finished
meshIsComplete=true;
//*** This needs to be here for OSX of we get a BAD ACCESS ERROR. DOES IT BREAK WINDOWS? ***//
//image.setUseTexture(false);
//image.setFromPixels(cvGrayDiff1.getPixels(), cvGrayDiff1.width, cvGrayDiff1.height, OF_IMAGE_GRAYSCALE);
//image.saveImage(_whole_file_path_synthesis+"/"+file_name);
//_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
// <--- REALLY NEW SAVING METHOD --- 26 feb 2012 --- consolidated the save function into Abstract Analysis> ///
cvConvertorImage.setFromGrayscalePlanarImages(cvGrayDiff1,cvGrayDiff1,cvGrayDiff1);
saveImageSynthesis(file_name, &cvConvertorImage, OF_IMAGE_GRAYSCALE);
_synth_save_cnt++; _synth_save_cnt++;
} }
} }
} }
@ -407,3 +414,95 @@ void IResponseAnalysis::save_cb(Timer& timer)
saveImageAnalysis(file_name); saveImageAnalysis(file_name);
} }
void IResponseAnalysis::setMeshFromPixels(ofPixels somePixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh){
int x=0;
int y=0;
//get rid of all previous vectors and colours
mesh.clear();
//unsigned char * thesePixels =currentSecondImage.getPixels();
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++;
}
}
}
ofPixels IResponseAnalysis::make3DZmap(ofImage &image1, ofImage &image2, ofImage &backgroundImag){
ofPixels imagePixels1 = image1.getPixelsRef();
ofPixels imagePixels2 = image2.getPixelsRef();
ofPixels backgroundPixels = backgroundImag.getPixelsRef();
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];
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...
float _maxPossibleDistanceToCentre=ofDist(0,0,imagePixels1.getWidth()/2, imagePixels1.getHeight()/2);
for(int i=0;i<imagePixels1.size();i+=3){
ofColor colourImage1 = imagePixels1.getColor(x, y);
ofColor colourImage2 = imagePixels2.getColor(x, y);
float _distanceToCentre=ofDist(imagePixels1.getWidth()/2, imagePixels1.getHeight()/2, x, y);
float _presumedBrightness=ofMap( sqrt(_distanceToCentre), 0, sqrt(_maxPossibleDistanceToCentre), 0, 255);
int thisDiff=abs(colourImage1.getBrightness()-_presumedBrightness);
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
if(x>=imagePixels1.getWidth()){
x=0;
y++;
}
}
}
difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return difference;
}

View File

@ -62,4 +62,10 @@ protected:
//this is the temporary container to allow us to convert and save out greyscale images //this is the temporary container to allow us to convert and save out greyscale images
ofxCvColorImage cvConvertorImage; ofxCvColorImage cvConvertorImage;
//mesh making function
void setMeshFromPixels(ofPixels somePixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//returns ofPixels but these pixels actually hold depth data.
ofPixels make3DZmap(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
bool _gotFirstImage;
ofImage _background;
}; };