From da5da62160cce5a8989d8a8827b2a6b197b98987 Mon Sep 17 00:00:00 2001 From: Jamie Allen Date: Tue, 8 May 2012 21:59:35 +0200 Subject: [PATCH] diffnoise, strobe and relaxrate updates --- example/RefractiveIndex.cpp | 13 +- src/ColorMultiAnalysis.cpp | 9 + src/ColorSingleAnalysis.cpp | 9 + src/DiffNoiseAnalysis.cpp | 584 ++++++++++++++++++++++++++++-- src/DiffNoiseAnalysis.h | 15 +- src/IResponseAnalysis.cpp | 17 +- src/IResponseAnalysis.h | 3 - src/RelaxRateAnalysis.cpp | 684 +++++++++++++++++++++++++++++++++--- src/RelaxRateAnalysis.h | 37 +- src/StrobeAnalysis.cpp | 631 ++++++++++++++++++++++++++++++--- src/StrobeAnalysis.h | 14 +- 11 files changed, 1870 insertions(+), 146 deletions(-) diff --git a/example/RefractiveIndex.cpp b/example/RefractiveIndex.cpp index ae93986..7db8f9e 100644 --- a/example/RefractiveIndex.cpp +++ b/example/RefractiveIndex.cpp @@ -43,7 +43,10 @@ ofxXmlSettings RefractiveIndex::XML; void RefractiveIndex::setup() { camDist=1000; + ofBackground(0, 0, 0); + ofSetBackgroundAuto(false); + //camera.setOrientation(ofVec3f(1,-1,1)); bool save_config = false; @@ -104,7 +107,6 @@ void RefractiveIndex::setup() //void ofPixels::allocate(int w, int h, ofImageType type) _pixels.allocate(_vid_w, _vid_h, OF_IMAGE_COLOR); - //TODO: whichever one of these is first - it always runs twice ? _analysisVector.push_back(new ShadowScapesAnalysis(V)); //1 @@ -150,7 +152,6 @@ void RefractiveIndex::start_analysis() _currentAnalysis->setup(_vid_w, _vid_h); _analysisAdapator->start(); - //allocate fbo for HD fbo.allocate(1920,1080); @@ -228,19 +229,23 @@ void RefractiveIndex::draw() if(_currentAnalysis->meshIsComplete){ fbo.begin(); - glShadeModel(GL_SMOOTH); ofClear(0,0,0); + + glShadeModel(GL_SMOOTH); + camera.begin(); //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(0, 0,-500); ofSetColor(0, 0, 0); ofRect(-fbo.getWidth(), -fbo.getHeight(), fbo.getWidth()*3, fbo.getHeight()*3); ofPopMatrix(); ofSetColor(255); - + */ + float xDiff= (fbo.getWidth()- 1.33333*(_currentAnalysis->_mesh_size_multiplier * _vid_w))/2; float yDiff= (fbo.getHeight()- 1.0*(_currentAnalysis->_mesh_size_multiplier * _vid_h))/2; diff --git a/src/ColorMultiAnalysis.cpp b/src/ColorMultiAnalysis.cpp index 9ee979e..37bd861 100755 --- a/src/ColorMultiAnalysis.cpp +++ b/src/ColorMultiAnalysis.cpp @@ -234,7 +234,16 @@ void ColorMultiAnalysis::synthesise() } } + // TOM'S fix of why the last file gets overwritten again and again - but seems to prevent the files being written to the screen. + /* + _RUN_DONE = true; + */ + + meshIsComplete=false; + _synth_save_cnt=0; + // _saved_filenames_synthesis has processed all the files in the analysis images folder + while(!_RUN_DONE && _state != STATE_STOP) Thread::sleep(3); } diff --git a/src/ColorSingleAnalysis.cpp b/src/ColorSingleAnalysis.cpp index ac8efa5..0be3978 100755 --- a/src/ColorSingleAnalysis.cpp +++ b/src/ColorSingleAnalysis.cpp @@ -270,7 +270,16 @@ void ColorSingleAnalysis::synthesise() } } + // TOM'S fix of why the last file gets overwritten again and again - but seems to prevent the files being written to the screen. + /* + _RUN_DONE = true; + */ + + meshIsComplete=false; + _synth_save_cnt=0; + // _saved_filenames_synthesis has processed all the files in the analysis images folder + while(!_RUN_DONE && _state != STATE_STOP) Thread::sleep(3); diff --git a/src/DiffNoiseAnalysis.cpp b/src/DiffNoiseAnalysis.cpp index b43a896..8d5d12e 100755 --- a/src/DiffNoiseAnalysis.cpp +++ b/src/DiffNoiseAnalysis.cpp @@ -18,16 +18,31 @@ void DiffNoiseAnalysis::setup(int camWidth, int camHeight) AbstractAnalysis::setup(camWidth, camHeight); NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_diffnoise", NUMBER_RUNS); + cout << "NUM_RUN DiffNoiseAnalysis " << NUM_RUN << endl; //NUM_RUN = 5; //flag for main sketch meshIsComplete=false; _gotFirstImage=false; - - _mesh_size_multiplier=4; - + _mesh_size_multiplier = 2; + vertexSubsampling = 1; + chooseColour = 5; + multiplier = 0.8; + blendMode = OF_BLENDMODE_ADD; + //blendMode = OF_BLENDMODE_MULTIPLY; + //blendMode = OF_BLENDMODE_SUBTRACT; + //blendMode = OF_BLENDMODE_ALPHA; + //blendMode = OF_BLENDMODE_SCREEN; + + meshMode = OF_PRIMITIVE_TRIANGLES; + //meshMode = OF_PRIMITIVE_TRIANGLE_STRIP; + //meshMode = OF_PRIMITIVE_TRIANGLE_FAN; + //meshMode = OF_PRIMITIVE_LINES; + //meshMode = OF_PRIMITIVE_LINE_STRIP; + //meshMode = OF_PRIMITIVE_POINTS; + int acq_run_time; // 10 seconds of acquiring per run acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_diffnoise", ACQUIRE_TIME); @@ -99,7 +114,6 @@ void DiffNoiseAnalysis::setup(int camWidth, int camHeight) cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvConvertorImage.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); - } @@ -159,45 +173,133 @@ void DiffNoiseAnalysis::synthesise() cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height); //cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height); - cvColorImage1.invert(); - cvColorImage1.blur(5); - cvColorImage1.erode(); - cvColorImage1.erode(); - cvColorImage1.blur(5); - cvColorImage1.erode(); - cvColorImage1.erode(); + //cvColorImage1.erode(); + //cvColorImage1.dilate(); + + //cvColorImage1.blur(5); + //cvColorImage2.blur(5); + cvColorImage1.convertToGrayscalePlanarImage(cvGrayImage1, 1); + //cvColorImage2.convertToGrayscalePlanarImage(cvGrayImage2, 1); + + //cvGrayImage1.convertToRange(0.0, 255.0); + //cvGrayImage2.convertToRange(0.0, 255.0); + + //cvGrayImage1.adaptiveThreshold( 200, 0); + //cvGrayImage2.adaptiveThreshold( 200, 0); + + //cvGrayImage1.contrastStretch(); + //cvGrayImage2.contrastStretch(); - //////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// - string file_name; + //cvGrayImage1.brightnessContrast(1.0,0.5); + //cvGrayImage2.brightnessContrast(1.0,0.5); + + cvColorImage1.setFromGrayscalePlanarImages(cvGrayImage1, cvGrayImage1, cvGrayImage1); + //cvColorImage2.setFromGrayscalePlanarImages(cvGrayImage2, cvGrayImage2, cvGrayImage2); + + cvPyrMeanShiftFiltering(cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), 1, 1); + //cvPyrMeanShiftFiltering(cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), 1, 1); + + //cvFloatImage1 = cvColorImage1; + //cvGrayImage1 = cvColorImage1; - file_name = ofToString(_synth_save_cnt, 2)+"_DiffNoiseAnalysis_"+ofToString(_run_cnt,2)+".jpg"; + //cvXorS( cvColorImage1.getCvImage(), cvScalarAll(150), cvColorImage1.getCvImage(), 0 ); + //cvXorS( cvColorImage2.getCvImage(), cvScalarAll(150), cvColorImage1.getCvImage(), 0 ); + //cvSmooth( cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), CV_GAUSSIAN, 5, 5); + //cvSmooth( cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), CV_GAUSSIAN, 5, 5); + + //cvCanny(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 100, 100, 3); + //cvLaplace(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0); + //cvGrayImage1 = cvCreateImage(cvSize(image1.width, image1.height),IPL_DEPTH_16S,1); + //cvSobel(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0, 1, 3); - //<---- THE OLD WAY OF SAVING - works on OSX but generates BLACK FRAMES on WINDOWS ----> - // ofSaveImage(cvGrayImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST); + + // convert the CV image + image1.setFromPixels(cvColorImage1.getPixelsRef()); + //image5.setFromPixels(cvColorImage2.getPixelsRef()); + + ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// + if(!_gotFirstImage){ + cout<<"background image is"<< _saved_filenames_analysis[i]< - //ofImage image; - //image.allocate(cvColorImage1.width, cvGrayImage1.height, OF_IMAGE_COLOR); + ofPixels imagePixels1 = image1.getPixelsRef(); + //ofPixels imagePixels2 = image5.getPixelsRef(); + ofPixels backgroundPixels = _background.getPixelsRef(); - //*** This needs to be here for OSX of we get a BAD ACCESS ERROR. DOES IT BREAK WINDOWS? ***// - //image.setUseTexture(false); + //DIFFERENCING SUBSEQUENT IMAGES + /* + for(int i=0;i0){ + imagePixels1[i]-=imagePixels2[i]; + } + else{ + imagePixels1[i]=0; + } + } + + */ + //DIFFERENCING THE BACKGROUND + /* + for(int i=0;i0){ + imagePixels1[i]-=backgroundPixels[i]; + } + else{ + imagePixels1[i]=0; + } + } + */ + + //update the images with their new background subtracted selves + image1.setFromPixels(imagePixels1); - //image.setFromPixels(cvColorImage1.getPixels(), cvColorImage1.width, cvColorImage1.height, OF_IMAGE_COLOR); - //image.saveImage(_whole_file_path_synthesis+"/"+file_name); + //flag the main app that we aren't read yet + meshIsComplete=false; - //_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name); + //make a mesh - this mesh will be drawn in the main app + setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); - // <--- REALLY NEW SAVING METHOD --- 26 feb 2012 --- consolidated the save function into Abstract Analysis> /// - saveImageSynthesis(file_name, &cvColorImage1, OF_IMAGE_COLOR); + //setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); + + /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// + //string file_name; + + //with jpgs this was refusing to save out + meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_DiffNoiseAnalysis_"+ofToString(_run_cnt,2)+".png"; + _saved_filenames_synthesis.push_back(meshFileName); + + //file_name = ofToString(_synth_save_cnt, 2)+"_ColorMultiAnalysis_"+ofToString(_run_cnt,2)+".jpg"; + + //flag that we are finished + meshIsComplete=true; _synth_save_cnt++; - // } + //} + } else { + cout<<"couldn't load image from "<<_saved_filenames_analysis[i]<= (_frame_cnt_max-_fade_in_frames) && _frame_cnt <= _frame_cnt_max) { for (int i=1; i < ofGetHeight() ; i=i+rectSize) @@ -444,3 +565,408 @@ void DiffNoiseAnalysis::save_cb(Timer& timer) saveImageAnalysis(file_name); } + + +void DiffNoiseAnalysis::setMeshFromPixels(vector 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(meshMode); + + ofColor meshColour=ofColor(255,255,255); + + //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=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==2){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==3){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==4){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==5){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + +} + + +vector DiffNoiseAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){ + + ofPixels imagePixels1 = image1.getPixelsRef(); + //ofPixels imagePixels2 = image2.getPixelsRef(); + ofPixels backgroundPixels = backgroundImag.getPixelsRef(); + vector 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.getWidth()){ + x=0; + y++; + } + } + } + + //difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3); + return differences; +} diff --git a/src/DiffNoiseAnalysis.h b/src/DiffNoiseAnalysis.h index 7f124f1..9088808 100755 --- a/src/DiffNoiseAnalysis.h +++ b/src/DiffNoiseAnalysis.h @@ -63,7 +63,20 @@ protected: //this is the temporary container to allow us to convert and save out greyscale images ofxCvColorImage cvConvertorImage; + + //mesh making function + void setMeshFromPixels(vector sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh); + + //depth map function + vector _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage); + + int vertexSubsampling; + int chooseColour; + ofPrimitiveMode meshMode; + ofBlendMode blendMode; + float multiplier; + bool _gotFirstImage; ofImage _background; - + }; diff --git a/src/IResponseAnalysis.cpp b/src/IResponseAnalysis.cpp index 3f83462..cf90d6e 100755 --- a/src/IResponseAnalysis.cpp +++ b/src/IResponseAnalysis.cpp @@ -90,9 +90,7 @@ void IResponseAnalysis::setup(int camWidth, int camHeight) cvColorImage2.clear(); cvGrayImage2.clear(); cvGrayDiff2.clear(); - - cvConvertorImage.clear(); - + cvColorImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayDiff1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); @@ -100,15 +98,10 @@ void IResponseAnalysis::setup(int camWidth, int camHeight) cvColorImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); - - cvConvertorImage.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); - } - void IResponseAnalysis::acquire() { - Timer* save_timer; TimerCallback save_callback(*this, &IResponseAnalysis::save_cb); @@ -205,6 +198,14 @@ void IResponseAnalysis::synthesise() //} } + // TOM'S fix of why the last file gets overwritten again and again - but seems to prevent the files being written to the screen. + /* + _RUN_DONE = true; + */ + + meshIsComplete=false; + _synth_save_cnt=0; + // _saved_filenames_synthesis has processed all the files in the analysis images folder while(!_RUN_DONE && _state != STATE_STOP) Thread::sleep(3); diff --git a/src/IResponseAnalysis.h b/src/IResponseAnalysis.h index 0450ac2..9f069ce 100755 --- a/src/IResponseAnalysis.h +++ b/src/IResponseAnalysis.h @@ -59,9 +59,6 @@ protected: ofxCvContourFinder cvContourFinder1; - //this is the temporary container to allow us to convert and save out greyscale images - ofxCvColorImage cvConvertorImage; - //mesh making function void setMeshFromPixels(vector sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh); //returns ofPixels but these pixels actually hold depth data. diff --git a/src/RelaxRateAnalysis.cpp b/src/RelaxRateAnalysis.cpp index d68b193..405c7e8 100755 --- a/src/RelaxRateAnalysis.cpp +++ b/src/RelaxRateAnalysis.cpp @@ -26,8 +26,24 @@ void RelaxRateAnalysis::setup(int camWidth, int camHeight) //flag for main sketch meshIsComplete=false; _gotFirstImage=false; + _mesh_size_multiplier = 2; + vertexSubsampling_x = 1; //must be at least '1' + vertexSubsampling_y = 7; //must be at least '1' + chooseColour = 2; + multiplier = 1; - _mesh_size_multiplier=4; + //blendMode = OF_BLENDMODE_ADD; + //blendMode = OF_BLENDMODE_MULTIPLY; + blendMode = OF_BLENDMODE_SUBTRACT; + //blendMode = OF_BLENDMODE_ALPHA; + //blendMode = OF_BLENDMODE_SCREEN; + + //meshMode = OF_PRIMITIVE_TRIANGLES; + //meshMode = OF_PRIMITIVE_TRIANGLE_STRIP; + //meshMode = OF_PRIMITIVE_TRIANGLE_FAN; + //meshMode = OF_PRIMITIVE_LINES; + meshMode = OF_PRIMITIVE_LINE_STRIP; + //meshMode = OF_PRIMITIVE_POINTS; int acq_run_time; // 10 seconds of acquiring per run acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_relaxrate", ACQUIRE_TIME); @@ -63,17 +79,46 @@ void RelaxRateAnalysis::setup(int camWidth, int camHeight) _image_shown = false; image1.clear(); + image2.clear(); + image3.clear(); + image4.clear(); + image5.clear(); + + // images use for drawing the synthesized files to the screen /// image1.setUseTexture(false); // the non texture image that is needed to first load the image + image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1 + + // images used for re-loading and saving out the synthesized files /// + image3.setUseTexture(false); + image4.setUseTexture(false); + image5.setUseTexture(false); + image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR); + image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR); + image3.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR); + image4.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR); + image5.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR); // clear() apparently fixes the "OF_WARNING: in allocate, reallocating a ofxCvImage" // that we're getting in OSX/Windows and is maybe crashing Windows // http://forum.openframeworks.cc/index.php?topic=1867.0 cvColorImage1.clear(); + cvGrayImage1.clear(); cvGrayDiff1.clear(); + cvColorImage2.clear(); + cvGrayImage2.clear(); + cvGrayDiff2.clear(); + + cvColorImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); + cvGrayImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayDiff1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); + + cvColorImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); + cvGrayImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); + cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); + } @@ -107,76 +152,194 @@ void RelaxRateAnalysis::acquire() void RelaxRateAnalysis::synthesise() { - //cout << "IResponseAnalysis::saving synthesis...\n"; + // cout << "RelaxRate::saving synthesis...\n"; if(_state == STATE_STOP) return; - cvContourFinderVect.clear(); - - - for(float i=1;i<_saved_filenames_analysis.size();i++){ - + for(float i=1;i<_saved_filenames_analysis.size()-1;i++){ + + // cout << "StrobeAnalysis::synthesis FOR LOOP...\n"; + + // cout << "_saved_filenames_analysis[i]" << _saved_filenames_analysis[i] << endl; + if(_state == STATE_STOP) return; - + + if(!image1.loadImage(_saved_filenames_analysis[i])){ + //couldn't load image + // cout << "didn't load image" << endl; + } + if(image1.loadImage(_saved_filenames_analysis[i])){ + //cout << "LOADED image1!!!" << endl; + if(image5.loadImage(_saved_filenames_analysis[i+1])){ - ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// - - cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height); - cvColorImage1.convertToGrayscalePlanarImage(cvGrayDiff1, 1); - - cvGrayDiff1.threshold(_treshold); - - rfiCvContourFinder* cf = new rfiCvContourFinder(); - - cf->findContours(cvGrayDiff1, 20, (image1.width * image1.height) / 4, _maxblobs, true); - - cvContourFinderVect.push_back(cf); - + ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// + + cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height); + cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height); + + //cvColorImage1.erode(); + //cvColorImage1.erode(); + //cvColorImage1.dilate(); + + cvColorImage1.blur(1); + cvColorImage2.blur(1); + + //cvXorS( cvColorImage1.getCvImage(), cvScalarAll(150), cvColorImage1.getCvImage(), 0 ); + //cvXorS( cvColorImage2.getCvImage(), cvScalarAll(150), cvColorImage1.getCvImage(), 0 ); + + cvColorImage1.convertToGrayscalePlanarImage(cvGrayImage1, 1); + cvColorImage2.convertToGrayscalePlanarImage(cvGrayImage2, 1); + + //cvGrayImage1.convertToRange(0.0, 255.0); + //cvGrayImage2.convertToRange(0.0, 255.0); + + //cvGrayImage1.adaptiveThreshold( 200, 0); + //cvGrayImage2.adaptiveThreshold( 200, 0); + + //cvGrayImage1.contrastStretch(); + //cvGrayImage2.contrastStretch(); + + cvGrayDiff1.absDiff(cvGrayImage1, cvGrayImage2); + + //cvGrayDiff1.brightnessContrast(1.0,0.5); + + //cvGrayImage1.brightnessContrast(1.0,0.5); + //cvGrayImage2.brightnessContrast(1.0,0.5); + + cvColorImage1.setFromGrayscalePlanarImages(cvGrayImage1, cvGrayImage1, cvGrayImage1); + //cvColorImage2.setFromGrayscalePlanarImages(cvGrayImage2, cvGrayImage2, cvGrayImage2); + + //cvPyrMeanShiftFiltering(cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), 1, 1); + //cvPyrMeanShiftFiltering(cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), 1, 1); + + //cvFloatImage1 = cvColorImage1; + //cvGrayImage1 = cvColorImage1; + + + //cvSmooth( cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), CV_GAUSSIAN, 5, 5); + //cvSmooth( cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), CV_GAUSSIAN, 5, 5); + + //cvCanny(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 100, 100, 3); + //cvLaplace(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0); + + //cvGrayImage1 = cvCreateImage(cvSize(image1.width, image1.height),IPL_DEPTH_16S,1); + //cvSobel(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0, 1, 3); + + // convert the CV image + image1.setFromPixels(cvColorImage1.getPixelsRef()); + image5.setFromPixels(cvColorImage2.getPixelsRef()); + + ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// + if(!_gotFirstImage){ + cout<<"background image is"<< _saved_filenames_analysis[i]<0){ + imagePixels1[i]-=imagePixels2[i]; + } + else{ + imagePixels1[i]=0; + } + } + */ + + //DIFFERENCING THE BACKGROUND + + for(int i=0;i0){ + imagePixels1[i]-=backgroundPixels[i]; + } + else{ + imagePixels1[i]=0; + } + } + + //update the images with their new background subtracted selves + image1.setFromPixels(imagePixels1); + + //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(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); + + //setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); + + /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// + //string file_name; + + //with jpgs this was refusing to save out + meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_RelaxRateAnalysis_"+ofToString(_run_cnt,2)+".png"; + _saved_filenames_synthesis.push_back(meshFileName); + + //file_name = ofToString(_synth_save_cnt, 2)+"_ColorMultiAnalysis_"+ofToString(_run_cnt,2)+".jpg"; + + //flag that we are finished + meshIsComplete=true; + _synth_save_cnt++; + + } + } else { + cout<<"couldn't load image from "<<_saved_filenames_analysis[i]< 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(meshMode); + + ofColor meshColour=ofColor(255,255,255); + + //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=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling_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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==2){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling_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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==3){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling_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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==4){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling_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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==5){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling_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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + +} + + +vector RelaxRateAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){ + + ofPixels imagePixels1 = image1.getPixelsRef(); + //ofPixels imagePixels2 = image2.getPixelsRef(); + ofPixels backgroundPixels = backgroundImag.getPixelsRef(); + vector 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.getWidth()){ + x=0; + y++; + } + } + } + + //difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3); + return differences; +} + diff --git a/src/RelaxRateAnalysis.h b/src/RelaxRateAnalysis.h index c785eda..85a2d88 100755 --- a/src/RelaxRateAnalysis.h +++ b/src/RelaxRateAnalysis.h @@ -44,14 +44,47 @@ protected: bool _show_image, _image_shown; - ofImage image1; - ofxCvColorImage cvColorImage1; + ofImage image1; + ofImage image2; + ofImage image3; + ofImage image4; + ofImage image5; + ofImage image6; + + ofxCvColorImage cvColorImage1; + ofxCvColorImage cvColorImage2; + ofxCvColorImage cvColorImage3; + ofxCvColorImage cvColorImage4; + ofxCvColorImage cvColorImage5; + ofxCvColorImage cvColorImage6; + ofxCvGrayscaleImage cvGrayDiff1; + ofxCvGrayscaleImage cvGrayDiff2; + + ofxCvGrayscaleImage cvGrayImage1; + ofxCvGrayscaleImage cvGrayImage2; + ofxCvGrayscaleImage cvGrayImage3; + ofxCvGrayscaleImage cvGrayImage4; + vector cvContourFinderVect; vector cvContourFinderVectDisplay; + //mesh making function + void setMeshFromPixels(vector sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh); + + //depth map function + vector _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage); + + int vertexSubsampling_x; + int vertexSubsampling_y; + int chooseColour; + ofPrimitiveMode meshMode; + ofBlendMode blendMode; + float multiplier; + bool _gotFirstImage; ofImage _background; + }; diff --git a/src/StrobeAnalysis.cpp b/src/StrobeAnalysis.cpp index b4d7336..387e004 100755 --- a/src/StrobeAnalysis.cpp +++ b/src/StrobeAnalysis.cpp @@ -24,8 +24,24 @@ void StrobeAnalysis::setup(int camWidth, int camHeight) //flag for main sketch meshIsComplete=false; _gotFirstImage=false; + _mesh_size_multiplier = 1; + vertexSubsampling = 2; + chooseColour = 2; + multiplier = 0.5; - _mesh_size_multiplier=4; + //blendMode = OF_BLENDMODE_ADD; + //blendMode = OF_BLENDMODE_MULTIPLY; + blendMode = OF_BLENDMODE_SUBTRACT; + //blendMode = OF_BLENDMODE_ALPHA; + //blendMode = OF_BLENDMODE_SCREEN; + + //meshMode = OF_PRIMITIVE_TRIANGLES; + //meshMode = OF_PRIMITIVE_TRIANGLE_STRIP; + //meshMode = OF_PRIMITIVE_TRIANGLE_FAN; + meshMode = OF_PRIMITIVE_LINES; + //meshMode = OF_PRIMITIVE_LINE_STRIP; + //meshMode = OF_PRIMITIVE_POINTS; + int acq_run_time; // 10 seconds of acquiring per run acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_strobe", ACQUIRE_TIME); @@ -87,7 +103,7 @@ void StrobeAnalysis::setup(int camWidth, int camHeight) cvGrayImage2.clear(); cvGrayDiff2.clear(); - cvConvertorImage.clear(); + cvColorImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); @@ -97,7 +113,6 @@ void StrobeAnalysis::setup(int camWidth, int camHeight) cvGrayImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); - cvConvertorImage.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); } @@ -151,60 +166,150 @@ void StrobeAnalysis::synthesise() } if(image1.loadImage(_saved_filenames_analysis[i])){ - // cout << "LOADED image1!!!" << endl; + //cout << "LOADED image1!!!" << endl; if(image5.loadImage(_saved_filenames_analysis[i+1])){ - - ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// - - cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height); - cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height); - cvColorImage1.convertToGrayscalePlanarImage(cvGrayImage1, 1); - cvColorImage2.convertToGrayscalePlanarImage(cvGrayImage2, 1); - - cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1); - cvGrayDiff1.dilate(); - cvGrayDiff1.dilate(); - cvGrayDiff1.contrastStretch(); - - /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// - string file_name; - - file_name = ofToString(_synth_save_cnt, 2)+"_StrobeAnalysis_"+ofToString(_run_cnt,2)+".jpg"; - - - //<---- THE OLD WAY OF SAVING - works on OSX but generates BLACK FRAMES on WINDOWS ----> - // ofSaveImage(cvGrayImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST); - - - //<---- NEW SAVING - seems to fix WINDOWS saving out BLACK FRAMES PROBLEM ----> - //ofImage image; - //image.allocate(cvGrayImage1.width, cvGrayImage1.height, OF_IMAGE_GRAYSCALE); + ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// - //*** 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++; - + cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height); + cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height); + + //cvColorImage1.erode(); + //cvColorImage1.erode(); + //cvColorImage1.dilate(); + + cvColorImage1.blur(1); + cvColorImage2.blur(1); + + //cvXorS( cvColorImage1.getCvImage(), cvScalarAll(150), cvColorImage1.getCvImage(), 0 ); + //cvXorS( cvColorImage2.getCvImage(), cvScalarAll(150), cvColorImage1.getCvImage(), 0 ); + cvColorImage1.convertToGrayscalePlanarImage(cvGrayImage1, 1); + cvColorImage2.convertToGrayscalePlanarImage(cvGrayImage2, 1); + + //cvGrayImage1.convertToRange(0.0, 255.0); + //cvGrayImage2.convertToRange(0.0, 255.0); + + //cvGrayImage1.adaptiveThreshold( 200, 0); + //cvGrayImage2.adaptiveThreshold( 200, 0); + + //cvGrayImage1.contrastStretch(); + //cvGrayImage2.contrastStretch(); + + cvGrayDiff1.absDiff(cvGrayImage1, cvGrayImage2); + + cvGrayDiff1.brightnessContrast(1.0,0.5); + + //cvGrayImage1.brightnessContrast(1.0,0.5); + //cvGrayImage2.brightnessContrast(1.0,0.5); + + cvColorImage1.setFromGrayscalePlanarImages(cvGrayImage1, cvGrayImage1, cvGrayImage1); + //cvColorImage2.setFromGrayscalePlanarImages(cvGrayImage2, cvGrayImage2, cvGrayImage2); + + //cvPyrMeanShiftFiltering(cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), 1, 1); + //cvPyrMeanShiftFiltering(cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), 1, 1); + + //cvFloatImage1 = cvColorImage1; + //cvGrayImage1 = cvColorImage1; + + + //cvSmooth( cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), CV_GAUSSIAN, 5, 5); + //cvSmooth( cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), CV_GAUSSIAN, 5, 5); + + //cvCanny(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 100, 100, 3); + //cvLaplace(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0); + + //cvGrayImage1 = cvCreateImage(cvSize(image1.width, image1.height),IPL_DEPTH_16S,1); + //cvSobel(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0, 1, 3); + + // convert the CV image + image1.setFromPixels(cvColorImage1.getPixelsRef()); + image5.setFromPixels(cvColorImage2.getPixelsRef()); + + ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// + if(!_gotFirstImage){ + cout<<"background image is"<< _saved_filenames_analysis[i]<0){ + imagePixels1[i]-=imagePixels2[i]; + } + else{ + imagePixels1[i]=0; + } + } + */ + + //DIFFERENCING THE BACKGROUND + + for(int i=0;i0){ + imagePixels1[i]-=backgroundPixels[i]; + } + else{ + imagePixels1[i]=0; + } + } + + //update the images with their new background subtracted selves + image1.setFromPixels(imagePixels1); + + //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(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); + + //setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); + + /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// + //string file_name; + + //with jpgs this was refusing to save out + meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_StrobeAnalysis_"+ofToString(_run_cnt,2)+".png"; + _saved_filenames_synthesis.push_back(meshFileName); + + //file_name = ofToString(_synth_save_cnt, 2)+"_ColorMultiAnalysis_"+ofToString(_run_cnt,2)+".jpg"; + + //flag that we are finished + meshIsComplete=true; + _synth_save_cnt++; + + } + } else { + cout<<"couldn't load image from "<<_saved_filenames_analysis[i]<= _fade_in_frames && _frame_cnt < (_frame_cnt_max-_fade_in_frames)){ @@ -289,8 +414,9 @@ void StrobeAnalysis::draw() ofRect(0, 0, ofGetWidth(), ofGetHeight()); // cout << "FADE OUT STROBE TIME " << endl; } - + ofDisableAlphaBlending(); + } else { //_state = STATE_SYNTHESISING; _RUN_DONE = true; @@ -433,3 +559,408 @@ void StrobeAnalysis::save_cb(Timer& timer) saveImageAnalysis(file_name); } + +void StrobeAnalysis::setMeshFromPixels(vector 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(meshMode); + + ofColor meshColour=ofColor(255,255,255); + + //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=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==2){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==3){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==4){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==5){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //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>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + +} + + +vector StrobeAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){ + + ofPixels imagePixels1 = image1.getPixelsRef(); + //ofPixels imagePixels2 = image2.getPixelsRef(); + ofPixels backgroundPixels = backgroundImag.getPixelsRef(); + vector 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.getWidth()){ + x=0; + y++; + } + } + } + + //difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3); + return differences; +} + diff --git a/src/StrobeAnalysis.h b/src/StrobeAnalysis.h index b86c79a..1c8842a 100755 --- a/src/StrobeAnalysis.h +++ b/src/StrobeAnalysis.h @@ -65,10 +65,20 @@ protected: ofxCvContourFinder cvContourFinder1; - //this is the temporary container to allow us to convert and save out greyscale images - ofxCvColorImage cvConvertorImage; + //mesh making function + void setMeshFromPixels(vector sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh); + + //depth map function + vector _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage); + + int vertexSubsampling; + int chooseColour; + ofPrimitiveMode meshMode; + ofBlendMode blendMode; + float multiplier; bool _gotFirstImage; ofImage _background; + };