Compare commits

..

32 Commits

Author SHA1 Message Date
Jamie Allen
a51a98a099 _synth_save_cnt = 0 wasn't in the acquire() loop - fixed 2012-02-22 18:35:35 +01:00
Jamie Allen
fe35f843ef cvGrayDiff1 was not being generated properly in RelaxRate - fixed 2012-02-22 16:20:40 +01:00
Jamie Allen
2f0415952e _synth_save_cnt = 0; needed to be reset! 2012-02-22 16:17:24 +01:00
Jamie Allen
1117e632c6 small file saving "_" fix
basically a safety commit - the big changes were in the last one :)
2012-02-22 15:39:49 +01:00
Jamie Allen
2f2b38019e added proper looping structure - fixed up some un-initialised variables
the looping structure for RUN_NUM now occurs in AbstractAnalysis, so
that the sequencing is correct - a few related variables were not reset
- so those are fixed now too.
2012-02-22 15:36:22 +01:00
Jamie Allen
24ddd694c7 the synthesis (saving out to files) and displaying are working for all EXCEPT RelaxRateAnalysis
this code awairs:
* add (from david) of RelaxRate analysis synthesise and display
* any learning that Tom brings back from the Middlesbrough trip
tomorrow.

a pretty nice little piece of code, gents!
2012-02-22 02:09:19 +01:00
Jamie Allen
763d88b115 key presses working for all - still working on opencv imagery 2012-02-21 21:45:18 +01:00
Jamie Allen
b0464c5632 added opencv images - showing but work in progress...
prior to adding david's looping and key control code
2012-02-21 21:20:58 +01:00
dviid
3c13d95bc4 key input controls 2012-02-21 21:18:25 +01:00
Jamie Allen
2903c7a969 david commit 19:31 2012-02-21 19:31:58 +01:00
Jamie Allen
117eaca232 commit for david tues 21st 17:51 2012-02-21 17:51:14 +01:00
Jamie Allen
9fd244dcc1 added openCV.h to each class 2012-02-21 17:26:16 +01:00
Jamie Allen
adbe746afc removed xml shit from the main.cpp 2012-02-21 16:56:38 +01:00
Jamie Allen
5df657051d removed the xml shit in main.cpp 2012-02-21 16:48:39 +01:00
Jamie Allen
7dc3e305a8 next day still working commit 2012-02-21 12:04:49 +01:00
Jamie Allen
7bb2f2c1e1 removed a few extraneous comments 2012-02-21 02:05:47 +01:00
Jamie Allen
0cd3bca7c0 cleanups, a few comments, and cout commented out 2012-02-21 02:01:01 +01:00
Jamie Allen
78c2edc0ac couple of todos added
//TODO:  Needs to return to a black screen - not to exit...

    // i.e.: operational sequence
    //    1) starts in a black screen
    //    2) we 'start' with keypress "s" the entire set of analyses
    //    3) at any point we need to be able to restart /
stop-return-to-black / trigger each analysis individually
    //    4) should end in black screen as well
    //    5) final kill button
2012-02-21 01:50:47 +01:00
Jamie Allen
680e22ccf9 tuesday 2 AM commit -
big priorities -
* figure out why its writing so many black images
* fix the double-run that occurs when the first anlysis runs
* do something pretty with the incoming image
2012-02-21 01:39:27 +01:00
Jamie Allen
3eef15a14b added image playout for all classes 2012-02-21 01:34:24 +01:00
Jamie Allen
e17a3a0d91 now able to show images in the analysis classes!!
wooo hooo!!!
2012-02-21 01:01:49 +01:00
Jamie Allen
51c71bdc15 added vid grab to IResponse 2012-02-20 20:28:12 +01:00
Jamie Allen
2bd7704f38 put the RefractiveIndex::_vidGrabber.grabFrame(); // in each class
david's solution for the black frames...
2012-02-20 20:24:25 +01:00
Jamie Allen
e1e4e15529 safety commit 2012-02-20 20:10:32 +01:00
Jamie Allen
b514c12a53 fixed timings so everything is on the second-basis, fixed David's flags so that each section in the classes terminates on RUN_DONE 2012-02-20 19:48:43 +01:00
Jamie Allen
12d210327b added STATE_SYNTHESISING - animation loop - for each analysis - comments needs cleaning up but they all seem to work...
NOT SURE I'M USING THE STATES CORRECTLY!?
2012-02-20 03:40:38 +01:00
Jamie Allen
b045f44bf1 fixed fades and added the animation for ShadowScapesAnalysis (which will be the same for all analyses - just havne't copied it over yet)
also tried to add a key-press trigger for each analysis but wasn't able to turn off prior analyses without hanging the whole programme
2012-02-20 01:47:46 +01:00
Jamie Allen
f2593aed49 tried to add a keypress function - got almost there... need to figure out how to turn off the Analyses from RefractiveIndex.cpp... without blocking 2012-02-19 23:34:48 +01:00
Jamie Allen
d536774cc8 REDO of the cleanup of the save callbacks so i can put the fucking latency code in...
(git hub did something weird)
2012-02-19 23:00:34 +01:00
Jamie Allen
6395b1c0a0 Revert 85ce9c2e6729fc1cee394b46ac868e4086d476f9^..HEAD 2012-02-19 22:47:55 +01:00
Tom Schofield
7bb5734eed added image loading and display results stuff- changes all commented in code 2012-02-19 22:47:44 +01:00
Jamie Allen
85ce9c2e67 cleaned up the save_cb on all analyses 2012-02-19 21:45:28 +01:00
26 changed files with 2978 additions and 1664 deletions

10
README Normal file
View File

@ -0,0 +1,10 @@
WARNING: WORK IN PROGRESS...
configuring OpenFrameworks
-----------------
these files are configured according to OpenFrameworks addons - http://ofxaddons.com/howto
(1) drag-drop "ReflectiveIndex" folder into your OpenFrameworks project
(2) Install depedencies: {ofxDirList, ofxControlPanel, ofxXmlSettings};
(3) Press Play!

View File

@ -1,2 +0,0 @@
<img src="https://davidgauthier.info/rfi/img-resize/rfi3.jpg" width="100%">
<img src="https://davidgauthier.info/rfi/img-resize/rfi4.jpg" width="100%">

View File

@ -1,12 +1,5 @@
/*
todo: ////also the new shit////
(1) Look at warinings about the #define which get over written
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "RefractiveIndex.h" #include "RefractiveIndex.h"
@ -50,9 +43,11 @@ void RefractiveIndex::setup()
bool save_config = false; bool save_config = false;
cout << "Loading configuration..." << endl; cout << "Loading configuration..." << endl;
if(!XML.loadFile("../data/config.refindx")) { if(XML.loadFile("config.refindx") == false) {
ofLog(OF_LOG_ERROR) << "error loading config - using default."; ofLog(OF_LOG_ERROR) << "error loading config - using default.";
save_config = true; save_config = true;
} else {
XML.loadFile("config.refindx");
} }
// <camera> // <camera>
@ -71,6 +66,7 @@ void RefractiveIndex::setup()
// display // display
cout << "> display" << endl; cout << "> display" << endl;
ofSetFrameRate(fps); ofSetFrameRate(fps);
if(fps > 30) { if(fps > 30) {
ofSetVerticalSync(FALSE); ofSetVerticalSync(FALSE);
} else { } else {
@ -93,35 +89,38 @@ void RefractiveIndex::setup()
//getting a warning from the OFlog that the pixels aren't allocated //getting a warning from the OFlog that the pixels aren't allocated
//void ofPixels::allocate(int w, int h, ofImageType type) //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 ? //TODO: whichever one of these is first - it always runs twice ?
//_analysisVector.push_back(new ShadowScapesAnalysis(V)); _analysisVector.push_back(new ShadowScapesAnalysis(V));
//_analysisVector.push_back(new ShadowScapesAnalysis(H)); _analysisVector.push_back(new ShadowScapesAnalysis(H));
//_analysisVector.push_back(new ShadowScapesAnalysis(D)); _analysisVector.push_back(new ShadowScapesAnalysis(D));
_analysisVector.push_back(new RelaxRateAnalysis()); _analysisVector.push_back(new RelaxRateAnalysis());
//_analysisVector.push_back(new IResponseAnalysis()); _analysisVector.push_back(new IResponseAnalysis());
//_analysisVector.push_back(new ShapeFromShadingAnalysis()); _analysisVector.push_back(new ShapeFromShadingAnalysis());
//_analysisVector.push_back(new StrobeAnalysis()); _analysisVector.push_back(new StrobeAnalysis());
//_analysisVector.push_back(new CamNoiseAnalysis()); _analysisVector.push_back(new CamNoiseAnalysis());
//_analysisVector.push_back(new ColorSingleAnalysis()); _analysisVector.push_back(new ColorSingleAnalysis());
//_analysisVector.push_back(new ColorMultiAnalysis()); _analysisVector.push_back(new ColorMultiAnalysis());
//_analysisVector.push_back(new DiffNoiseAnalysis()); _analysisVector.push_back(new DiffNoiseAnalysis());
//_currentAnalysisIndx = 0;
//_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_currentAnalysisIndx = 0; //_state = ISTATE_START;
_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_state = ISTATE_START; _currentAnalysis = NULL;
_state = ISTATE_UNDEF;
} }
@ -161,7 +160,9 @@ void RefractiveIndex::state_analysis()
case ISTATE_TRANSITION: case ISTATE_TRANSITION:
if(_currentAnalysisIndx >= _analysisVector.size()) { if(_currentAnalysisIndx >= _analysisVector.size()) {
_currentAnalysisIndx = 0; _currentAnalysisIndx = 0;
_state = ISTATE_END; _currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_state = ISTATE_START;
//_state = ISTATE_END;
} else { } else {
_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++); _currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_state = ISTATE_START; _state = ISTATE_START;
@ -185,18 +186,27 @@ void RefractiveIndex::state_analysis()
void RefractiveIndex::update() void RefractiveIndex::update()
{ {
state_analysis(); state_analysis();
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
if (_vidGrabber.isFrameNew())
{
_pixels = _vidGrabber.getPixelsRef(); //get ofPixels from the camera
}
} }
void RefractiveIndex::draw() void RefractiveIndex::draw()
{ {
// refractive mauve
//ofBackground(113, 110, 136);
// black
ofBackground(0, 0, 0); ofBackground(0, 0, 0);
//TODO: Needs to return to a black screen - not to exit...
// i.e.: operational sequence
// 1) starts in a black screen
// 2) we 'start' with keypress "s" the entire set of analyses
// 3) at any point we need to be able to restart / stop-return-to-black / trigger each analysis individually
// 4) should end in black screen as well
// 5) final kill button
//ofRect(0,0,ofGetWidth(), ofGetHeight());
if(_currentAnalysis) if(_currentAnalysis)
_currentAnalysis->draw(); _currentAnalysis->draw();
} }
@ -205,15 +215,19 @@ void RefractiveIndex::setup_camera()
{ {
stop_camera(); stop_camera();
_vidGrabber.setDeviceID(_vid_id);
_vidGrabber.listDevices();
if(!_vidGrabber.initGrabber(_vid_w, _vid_h)) { if(!_vidGrabber.initGrabber(_vid_w, _vid_h)) {
ofLog(OF_LOG_ERROR) << "RefractiveIndex::setup_camera - could not initialise grabber"; ofLog(OF_LOG_ERROR) << "RefractiveIndex::setup_camera - could not initialise grabber";
return; return;
} }
_vidGrabber.setUseTexture(false);
_vidGrabber.listDevices();
_vidGrabber.setVerbose(true); _vidGrabber.setVerbose(true);
_vidGrabber.setUseTexture(false);
_vid_stream_open = true; _vid_stream_open = true;
_vidGrabber.setDeviceID(_vid_id); cout << "CAMERA SETUP " << endl;
return;
} }
@ -229,6 +243,121 @@ void RefractiveIndex::keyPressed (int key)
{ {
if( key =='f') if( key =='f')
ofToggleFullscreen(); ofToggleFullscreen();
/* TODO: complete the below... would be good to trigger the Analysis from keypresses if needed... */
// currently this doesn't work... the save_cb's in the individual
// tried to add a stop_analysis(); call but it blocks the whole programme
// i.e.: ask david how to shut off the prior Analysis if it's not finished running from here?
if(key == 'x')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysis = NULL;
_state = ISTATE_UNDEF;
}
else if(key == '1')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 0;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '2')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 1;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '3')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 2;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '4')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 3;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '5')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 4;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '6')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 5;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '7')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 6;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '8')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 7;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '9')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 8;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == '0')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 9;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
else if(key == 'q')
{
if(_currentAnalysis)
_analysisAdapator->stop();
_currentAnalysisIndx = 10;
if(!_currentAnalysis)
_state = ISTATE_TRANSITION;
}
} }
void RefractiveIndex::exit() void RefractiveIndex::exit()

View File

@ -1,8 +1,3 @@
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#pragma once #pragma once
@ -13,6 +8,7 @@
#include "AbstractAnalysis.h" #include "AbstractAnalysis.h"
#include "AnalysisAdaptor.h" #include "AnalysisAdaptor.h"
#include "ofxOpenCv.h"
class RefractiveIndex : public ofBaseApp class RefractiveIndex : public ofBaseApp
{ {
@ -66,5 +62,4 @@ public:
// this should be in xml // this should be in xml
static string _location; static string _location;
}; };

View File

@ -1,35 +1,27 @@
#include "ofAppGlutWindow.h" #include "ofAppGlutWindow.h"
#include "RefractiveIndex.h" #include "RefractiveIndex.h"
#include "ofxXmlSettings.h"
#define SCREEN_WIDTH 800 #define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600 #define SCREEN_HEIGHT 600
/////////////////////////dis is the new shit///////////
/////////////////////////dis is the new shit///////////
/////////////////////////dis is the new shit///////////
/////////////////////////dis is the new shit///////////
int main() { int main() {
ofAppGlutWindow window; ofAppGlutWindow window;
ofxXmlSettings XML; bool fullscreen;
XML.loadFile("../data/config.refindx");
bool fullscreen = (XML.getValue("config:display:fullscreen", "false") == "true" ? true : false); //fullscreen = true;
int screen_w = XML.getValue("config:display:width", SCREEN_WIDTH); fullscreen = false;
int screen_h = XML.getValue("config:display:height", SCREEN_HEIGHT);
cout << "> display configuration" << endl; cout << "> display configuration" << endl;
cout << "* fullscreen: " << (fullscreen ? "yes" : "no") << endl; cout << "* fullscreen: " << (fullscreen ? "yes" : "no") << endl;
if(!fullscreen) { if(!fullscreen) {
cout << "* screen width: " << screen_w << endl; cout << "* screen width: " << SCREEN_WIDTH << endl;
cout << "* screen height: " << screen_h << endl; cout << "* screen height: " << SCREEN_HEIGHT << endl;
} }
ofSetupOpenGL(&window, screen_w, screen_h, (fullscreen ? OF_FULLSCREEN : OF_WINDOW)); ofSetupOpenGL(&window, SCREEN_WIDTH, SCREEN_HEIGHT, (fullscreen ? OF_FULLSCREEN : OF_WINDOW));
ofRunApp(new RefractiveIndex()); ofRunApp(new RefractiveIndex());
} }

View File

@ -1,50 +1,28 @@
/* /* */
- copyright (c) 2011 Copenhagen Institute of Interaction Design (CIID)
- all rights reserved.
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "AbstractAnalysis.h" #include "AbstractAnalysis.h"
#include "RefractiveIndex.h" #include "RefractiveIndex.h"
#include "ofxFileHelper.h" #include "ofxFileHelper.h"
vector<ofMesh> AbstractAnalysis::meshes;
// this is the main threaded loop for a given analysis // this is the main threaded loop for a given analysis
void AbstractAnalysis::do_synthesize() { void AbstractAnalysis::do_synthesize() {
for(int i = 0; i < NUM_RUN; i++) {
cout << "i NUM_RUN" << i << endl;
_saved_filenames_analysis.clear();
_state = STATE_ACQUIRING; _state = STATE_ACQUIRING;
acquire(); acquire();
if(_state == STATE_STOP) goto exit;
_state = STATE_SYNTHESISING; _state = STATE_SYNTHESISING;
synthesise(); synthesise();
if(_state == STATE_STOP) goto exit;
_state = STATE_DISPLAY_RESULTS; _state = STATE_DISPLAY_RESULTS;
display_results(); displayresults();
}
exit:
ofNotifyEvent(_synthesize_cb, _name); ofNotifyEvent(_synthesize_cb, _name);
} }
@ -71,266 +49,29 @@ void AbstractAnalysis::create_dir()
} }
} }
ofxFileHelper fileHelper; ofxFileHelper fileHelperAnalysis;
_whole_file_path = ofToDataPath("") + ANALYSIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime ; ofxFileHelper fileHelperSynthesis;
cout << "_whole_file_path = " << _whole_file_path << endl;
if(!fileHelper.doesDirectoryExist(_whole_file_path)){ _whole_file_path_analysis = ANALYSIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime ;
fileHelper.makeDirectory(ANALYSIS_PATH);
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location); //cout << "_whole_file_path_analysis = " << _whole_file_path_analysis << endl;
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name);
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime); if(!fileHelperAnalysis.doesDirectoryExist(_whole_file_path_analysis)){
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH);
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location);
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name);
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime);
}
_whole_file_path_synthesis = SYNTHESIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime;
if(!fileHelperSynthesis.doesDirectoryExist(_whole_file_path_synthesis)){
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH);
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location);
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name);
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime);
} }
//////////////////////////////END DIRECTORY CREATION ////////////////////////////////////////////////// //////////////////////////////END DIRECTORY CREATION //////////////////////////////////////////////////
} }
ofPixels AbstractAnalysis::calculateListOfZValues(ofImage image1, ofImage image2, int whichComparison){
//zScale is the mapping factor from pixel difference to shift on the zPlane
int zScale=200;
ofPixels imagePixels1 = image1.getPixelsRef();
ofPixels imagePixels2 = image2.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];
//where are we in the image pixel array
int x=0;
int y=0;
//for each pixel...
for(int i=0;i<imagePixels1.size();i+=3){
//get the colour of each image at this x y location - we will use these colours for comparison according to the below criteria
ofColor colourImage1 = imagePixels1.getColor(x, y);
ofColor colourImage2 = imagePixels2.getColor(x, y);
//COMPARE THIS PIXEL'S VALUES with the first image in the sequence
int thisDiff;
//compare Red
if (whichComparison==1) {
thisDiff=ofMap((colourImage1.r-colourImage2.r),-255,255,0,zScale);
}
//compare blue
if (whichComparison==2) {
thisDiff=ofMap((colourImage1.g-colourImage2.g),-255,255,0,zScale);
}
//compare green
if (whichComparison==3) {
thisDiff=ofMap((colourImage1.b-colourImage2.b),-255,255,0,zScale);
}
//compare hue
if (whichComparison==4) {
thisDiff=ofMap((colourImage1.getHue()-colourImage2.getHue()),-255,255,0,zScale);
}
//compare brightness
if (whichComparison==5) {
thisDiff=ofMap((colourImage1.getBrightness()-colourImage2.getBrightness()),-255,255,0,zScale);
}
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
//new line
if(x>imagePixels1.getWidth()){
x=0;
y++;
}
}
difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return difference;
}
ofPixels AbstractAnalysis::calculateListOfZValues(ofImage image1, ofImage image2, int whichComparison, int colourValue){
//zScale is the mapping factor from pixel difference to shift on the zPlane
int zScale=200;
ofPixels imagePixels1 = image1.getPixelsRef();
ofPixels imagePixels2 = image2.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];
//where are we in the image pixel array
int x=0;
int y=0;
//for each pixel...
//for(int i=0;i<imagePixels1.size();i+=3){
for(int i=0;i<20;i+=3){
//get the colour of each image at this x y location - we will use these colours for comparison according to the below criteria
ofColor colourImage1 = imagePixels1.getColor(x, y);
ofColor colourImage2 = imagePixels2.getColor(x, y);
//COMPARE THIS PIXEL'S VALUES with the first image in the sequence
int thisDiff;
//compare Red
if (whichComparison==1) {
thisDiff=ofMap((colourImage1.r-colourImage2.r),-255,255,0,zScale);
}
//compare blue
if (whichComparison==2) {
thisDiff=ofMap((colourImage1.g-colourImage2.g),-255,255,0,zScale);
}
//compare green
if (whichComparison==3) {
thisDiff=ofMap((colourImage1.b-colourImage2.b),-255,255,0,zScale);
}
//compare hue
if (whichComparison==4) {
thisDiff=ofMap((colourImage1.getHue()-colourImage2.getHue()),-255,255,0,zScale);
}
//compare brightness
if (whichComparison==5) {
thisDiff=ofMap((colourImage1.getBrightness()-colourImage2.getBrightness()),-255,255,0,zScale);
}
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
//new line
if(x>imagePixels1.getWidth()){
x=0;
y++;
}
}
difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return difference;
}
void AbstractAnalysis::setMeshFromPixels(ofPixels somePixels, ofImage currentSecondImage, ofMesh * someMesh){
int x=0;
int y=0;
//get rid of all previous vectors and colours - uncomment if re-setting the mesh on the fly - ie live rather than saving it first
//someMesh->clear();
unsigned char * thesePixels =currentSecondImage.getPixels();
for(int i=0;i<somePixels.size();i+=3){
someMesh->addVertex(ofVec3f(x,y,- somePixels.getColor(x, y).getBrightness() ));
// add colour from current second image of two
someMesh->addColor( currentSecondImage.getColor(x, y) );
x++;
if(x>somePixels.getWidth()){
x=0;
y++;
}
}
}
vector<string>AbstractAnalysis:: getListOfImageFilePaths(string location, string whichAnalysis){
string path = ofToDataPath("")+"debug_analysis/"+location+"/"+whichAnalysis;
//ofxDirList dirList;
ofDirectory dirList;
int numDirs = dirList.listDir(path);
vector<string>directoryNames;
//get the last folder alphabetically - this should probably change to do something fancy with date to find most recent but don't want to code that until we are sure
string dirName=dirList.getName(numDirs-1);
const char *results=dirName.c_str();
ofFile file=ofFile(path+"/"+dirName);
vector<string>fileNamesToReturn;
// get
if(file.isDirectory()){
dirList.listDir(path+"/"+dirName);
//if there are no files, exit here
if(dirList.size()==0){
//if it's empty return an error warning
fileNamesToReturn.push_back("NO FILE HERE!");
cout<<"NO FILE HERE!";
return fileNamesToReturn;
}
for (int i=0; i<dirList.size(); i++) {
string fname=dirList.getName(i);
const char *results=fname.c_str();
//full path is what actually gets written into the vector
string fullPath=path+"/"+dirName+"/"+fname;
fileNamesToReturn.push_back(fullPath);
}
}
else{
cout<<"WARNING, DIRECTORY NOT FOUND";
fileNamesToReturn.push_back("NO FILE HERE!");
}
return fileNamesToReturn;
}
int AbstractAnalysis::getRecordedValueFromFileName(string str){
//split filename by underscore - there HAS to be a quicker way of doing things - its ONE LINE in java :(
char * cstr, *p;
vector<char *>tokens;
//string str ("Please split this phrase into tokens");
//make char pointer array
cstr = new char [str.size()+1];
//copy string to char pointer array
strcpy (cstr, str.c_str());
//tokenise char p array and put first results into pointer?
p=strtok (cstr,"_");
while (p!=NULL)
{
p=strtok(NULL,"_");
//push tokenised char into vector
tokens.push_back(p);
}
delete[] cstr;
char *p1;
//cstr = new char [str.size()+1];
//strcpy (cstr, str.c_str());
p1=strtok (tokens[tokens.size()-2],".");
return ofToInt(p1);
}
void AbstractAnalysis::saveimage(string filename)
{
if(RefractiveIndex::_pixels.isAllocated()) {
string fname = _whole_file_path + "/" + filename;
cout << "saving - " << fname << endl;
ofSaveImage(RefractiveIndex::_pixels, fname, OF_IMAGE_QUALITY_BEST);
_saved_filenames.push_back(fname);
} else {
ofLog(OF_LOG_ERROR) << "RefractiveIndex::_pixels NOT allocated...";
}
}

View File

@ -10,18 +10,13 @@
#include <string> #include <string>
#define ANALYSIS_PATH "analysis/" #define ANALYSIS_PATH "analysis/"
#define SYNTHESIS_PATH "synthesis/"
#define STATE_ACQUIRING 0x1111 #define STATE_ACQUIRING 0x1111
#define STATE_SYNTHESISING 0x2222 #define STATE_SYNTHESISING 0x2222
#define STATE_DISPLAY_RESULTS 0x3333 #define STATE_DISPLAY_RESULTS 0x3333
#define STATE_STOP 0xDEADBEEF #define STATE_STOP 0xDEADBEEF
#define COMPARE_RED 1
#define COMPARE_BLUE 2
#define COMPARE_GREEN 3
#define COMPARE_HUE 4
#define COMPARE_BRIGHTNESS 5
class AbstractAnalysis { class AbstractAnalysis {
public: public:
@ -30,16 +25,16 @@ public:
// generic function to set up the camera // generic function to set up the camera
virtual void setup(int camWidth, int camHeight){_cam_w = camWidth; _cam_h = camHeight;} virtual void setup(int camWidth, int camHeight){_cam_w = camWidth; _cam_h = camHeight;}
// this is the main threaded loop for a given analysis // this is the main threaded loop for a given analysis
void do_synthesize(); void do_synthesize();
// ofx // show the results to the screen
virtual void draw() = 0; virtual void draw() = 0;
protected: protected:
virtual void create_dir(); virtual void create_dir();
virtual void saveimage(string filename);
// acquire images - all the children (see - do_synthesize) // acquire images - all the children (see - do_synthesize)
virtual void acquire() = 0; virtual void acquire() = 0;
@ -47,26 +42,8 @@ protected:
// analysis + synthesize images - all the children (see - do_synthesize) // analysis + synthesize images - all the children (see - do_synthesize)
virtual void synthesise() = 0; virtual void synthesise() = 0;
//added tom s 19/2 function runs a call back exactly like acquire. // display the results from disk
virtual void display_results() = 0; virtual void displayresults() = 0;
//returns ofPixels which contain the color differences between the two images. Is overloaded to include comparison with values written in to file names for some analyses
virtual ofPixels calculateListOfZValues(ofImage image1, ofImage image2, int whichComparison);
virtual ofPixels calculateListOfZValues(ofImage image1, ofImage image2, int whichComparison, int colourValue);
//uses the returned pixels from make3DZmap to make a mesh of points whose Z positions are set by the brightness values in ofPixels - ofPixels is being used as a convenient container for a bunch of z coordinates
virtual void setMeshFromPixels(ofPixels somePixels, ofImage currentSecondImage, ofMesh * someMesh);
//HELPER FUNCTIONS
//this is purely for debug/viewing purposes and loads old images from middlesborough test
virtual vector<string> getListOfImageFilePaths(string location, string whichAnalysis);
//splits up the filename and returns the recorded value eg brightness
//EG FILENAME : DIFF_NOISE_7_85.7322.jpg RETURNS : 85.7322
virtual int getRecordedValueFromFileName(string str);
static vector<ofMesh>meshes;
public: public:
string _name; string _name;
@ -76,22 +53,18 @@ public:
protected: protected:
int _cam_w, _cam_h; int _cam_w, _cam_h;
string _whole_file_path; string _whole_file_path_analysis, _whole_file_path_synthesis;
vector<string> _saved_filenames; vector<string> _saved_filenames_analysis;
vector<string> _saved_filenames_synthesis;
int _state; int _state;
//int _run_cnt;
float DELTA_T_SAVE; float DELTA_T_SAVE;
int NUM_PHASE; int NUM_PHASE;
int NUM_RUN; int NUM_RUN;
int NUM_SAVE_PER_RUN; int NUM_SAVE_PER_RUN;
//added Tom S 19/2/12
//each mesh in the vector is a seperate 3D point cloud which is coloured with pixel data and shifted in the z plane according to the specified type of colour difference eg red value or hue
//make this vector were static
//how fast to move from one mesh to the next
float speed;
//the index (inside the vector of meshes) of the current mesh being displayed
float whichMesh;
friend class AnalysisAdaptor; friend class AnalysisAdaptor;
}; };

View File

@ -21,12 +21,15 @@ public:
void start() void start()
{ {
_stopping = false;
_runnable = new RunnableAdapter<AbstractAnalysis>(*_analysis, &AbstractAnalysis::do_synthesize); _runnable = new RunnableAdapter<AbstractAnalysis>(*_analysis, &AbstractAnalysis::do_synthesize);
_worker.start(*_runnable); _worker.start(*_runnable);
} }
void stop() void stop()
{ {
if(_stopping) return;
_stopping = true;
_analysis->_state = STATE_STOP; _analysis->_state = STATE_STOP;
_worker.join(); _worker.join();
} }
@ -35,5 +38,6 @@ protected:
AbstractAnalysis* _analysis; AbstractAnalysis* _analysis;
Thread _worker; // Thread _worker; //
RunnableAdapter<AbstractAnalysis>* _runnable; RunnableAdapter<AbstractAnalysis>* _runnable;
bool _stopping;
}; };

View File

@ -1,8 +1,3 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "CamNoiseAnalysis.h" #include "CamNoiseAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -18,16 +13,50 @@ using Poco::Thread;
void CamNoiseAnalysis::setup(int camWidth, int camHeight) void CamNoiseAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 200;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
create_dir(); NUM_RUN = 5;
int acq_run_time = 20; // 20 seconds of acquiring per run
DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 5 times per second = every 200 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000); _run_cnt = 0;
_synth_save_cnt = 0;
c = 0; c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
//create_dir();
_show_image = false;
_image_shown = false;
// 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);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
} }
@ -35,91 +64,118 @@ void CamNoiseAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<CamNoiseAnalysis> save_callback(*this, &CamNoiseAnalysis::save_cb); TimerCallback<CamNoiseAnalysis> save_callback(*this, &CamNoiseAnalysis::save_cb);
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_run_cnt++;
_RUN_DONE = false;
create_dir();
// RUN ROUTINE // RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) { // for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i;
_run_cnt = i; //cout << "RUN NUM = " << i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE) while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
// }
} }
void CamNoiseAnalysis::synthesise() void CamNoiseAnalysis::synthesise()
{ {
//incrementer to whichMesh //cout << "CamNoiseAnalysis::saving synthesis...\n";
speed=0.2; if(_state == STATE_STOP) return;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
float iterator=1;
//if you want to see what this looks like with real data ignore the new filenames and load teh old ones.
bool debug=true;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. //cout << "CamNoiseAnalysis::synthesis FOR LOOP...\n";
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; //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])){
//clear vector so we don't add to it on successive runs //cout << "LOADED image1!!!" << endl;
meshes.clear(); //if(image5.loadImage(_saved_filenames_analysis[i+1])){
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){ ///////////////////////// 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);
ofImage image1; cvGrayImage1 = cvColorImage1;
ofImage image2; //cvGrayImage2 = cvColorImage2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false) //cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
image1.setUseTexture(false); //cvGrayDiff1.erode();
image2.setUseTexture(false); //cvGrayDiff1.contrastStretch();
//some of the textures are not loading correctly so only make mesh if both the images load //cvGrayDiff1.blur(5);
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){ //cvGrayDiff1.dilate();
meshes.push_back(ofMesh()); //cvGrayDiff1.contrastStretch();
cout<<"setting mesh"<<endl;
int _recorded_hue_value=getRecordedValueFromFileName(_saved_filenames[i]); cvGrayImage1.threshold(255.0*i/_saved_filenames_analysis.size()); //random threshold for the moment
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_HUE,_recorded_hue_value), image2, &meshes[index]); cvGrayImage1.blur(10);
index++; cvGrayImage1.contrastStretch();
cvGrayImage1.blur(10);
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
string file_name;
file_name = ofToString(_synth_save_cnt, 2)+"_CamNoiseAnalysis_"+ofToString(_run_cnt,2)+".jpg";
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
ofSaveImage(cvGrayImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
//ofSaveImage(cvGrayDiff1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
//}
} }
} }
} // _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
void CamNoiseAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<CamNoiseAnalysis> display_results_callback(*this, &CamNoiseAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
}
display_results_timer->stop(); void CamNoiseAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
@ -140,44 +196,48 @@ void CamNoiseAnalysis::draw()
ofColor aColour; ofColor aColour;
int _fade_in_frames = _frame_cnt_max/10; int _fade_in_frames = _frame_cnt_max/10;
float _number_of_grey_levels=10; float _number_of_grey_levels=5;
float _frames_per_level = _frame_cnt_max / _number_of_grey_levels; float _frames_per_level = _frame_cnt_max / _number_of_grey_levels;
ofColor someColor; ofColor someColor;
/*
if (_frame_cnt < _fade_in_frames) { if (_frame_cnt < _fade_in_frames) {
aColour.set(255, 255, 255, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)); aColour.set(255, 255, 255, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColour); ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
//cout << "FADE IN STROBE TIME " << endl; //cout << "FADE IN STROBE TIME " << endl;
} }
*/
if (_frame_cnt >= _fade_in_frames && _frame_cnt < (_frame_cnt_max-_fade_in_frames)){ //if (_frame_cnt >= _fade_in_frames && _frame_cnt < (_frame_cnt_max)){
for(int i=0;i<_number_of_grey_levels;i++){ for(int i=1;i<_number_of_grey_levels;i++){
if (_frame_cnt>= _frames_per_level *( i-1) && +_frame_cnt < _frames_per_level * (i) ) { if ( _frame_cnt >= _frames_per_level*(i-1) && +_frame_cnt < _frames_per_level * (i+1) ) {
//set colour to current grey level //set colour to current grey level
c=255-( 255.0 * ( i /_number_of_grey_levels)); c=255-( 255.0 * ( i /_number_of_grey_levels));
//cout << "c: " << c << endl;
someColor.set(c); someColor.set(c);
} }
ofSetColor(someColor); ofSetColor(someColor);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
} }
} //}
/*
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) { if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) {
aColour.set(0, 0, 0, 255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)); aColour.set(0, 0, 0, ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255));
ofSetColor(aColour); ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
// cout << "FADE OUT STROBE TIME " << endl; // cout << "FADE OUT STROBE TIME " << endl;
} }
*/
ofDisableAlphaBlending(); ofDisableAlphaBlending();
} else { } else {
// _state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -189,36 +249,117 @@ void CamNoiseAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "CamNoiseAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "CamNoiseAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices(); //cout << "STATE_DISPLAY_RESULTS...\n" << endl;
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
if (_show_image)
{
//cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break; break;
}
}
default: default:
break; break;
@ -232,16 +373,20 @@ void CamNoiseAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++; _save_cnt++;
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
if (RefractiveIndex::_vidGrabber.isFrameNew())
{
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
} else {
return;
}
//cout << "CamNoiseAnalysis::saving...\n";
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg"; string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
//if(_save_cnt >= NUM_SAVE_PER_RUN) ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
// _RUN_DONE = true; _saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
} }
void CamNoiseAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,6 +9,9 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class CamNoiseAnalysis : public AbstractAnalysis class CamNoiseAnalysis : public AbstractAnalysis
{ {
public: public:
@ -20,17 +23,41 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
int _run_cnt, _save_cnt, _synth_save_cnt,_anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };

View File

@ -1,35 +1,3 @@
/*
- copyright (c) 2011 Copenhagen Institute of Interaction Design (CIID)
- all rights reserved.
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ColorMultiAnalysis.h" #include "ColorMultiAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -42,135 +10,211 @@ using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
void ColorMultiAnalysis::setup(int camWidth, int camHeight) void ColorMultiAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 100;//150; // the right number is about 300
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;//;
create_dir(); NUM_RUN = 5;
int acq_run_time = 35;
DELTA_T_SAVE = 1*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir();
_frame_cnt = 0; _frame_cnt = 0;
_fade_cnt=0; _run_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000); _synth_save_cnt = 0;
c = 0; c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
//for an ofxOpenCv.h image i would like to use..."
//image3.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h);
_show_image = false;
_image_shown = false;
// 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);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
} }
void ColorMultiAnalysis::acquire() void ColorMultiAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<ColorMultiAnalysis> save_callback(*this, &ColorMultiAnalysis::save_cb); TimerCallback<ColorMultiAnalysis> save_callback(*this, &ColorMultiAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir();
// RUN ROUTINE // RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) { //for(int i = 0; i < NUM_RUN; i++) {
_run_cnt = i; // _run_cnt = i;
cout << "RUN NUM = " << i; //cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE) while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
//}
} }
void ColorMultiAnalysis::synthesise() void ColorMultiAnalysis::synthesise()
{ {
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; //cout << "ColorMultiAnalysis::saving synthesis...\n";
float iterator=1; if(_state == STATE_STOP) return;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; //cout << "ColorMultiAnalysis::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])){
//clear vector so we don't add to it on successive runs //cout << "LOADED image1!!!" << endl;
meshes.clear(); //if(image5.loadImage(_saved_filenames_analysis[i+1])){
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){ ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES //////////////////////////
ofImage image1; cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height);
ofImage image2; //cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height);
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false) //cvGrayImage1 = cvColorImage1;
image1.setUseTexture(false); //cvGrayImage2 = cvColorImage2;
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load //cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
if(image1.loadImage(_saved_filenames[0]) && image2.loadImage(_saved_filenames[i+1])){ //cvGrayDiff1.erode();
cout<<"setting mesh"<<endl; //cvGrayDiff1.contrastStretch();
meshes.push_back(ofMesh()); //cvGrayDiff1.blur(5);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_HUE), image2, &meshes[index]); //cvGrayDiff1.dilate();
index++; //cvGrayDiff1.contrastStretch();
}
cvColorImage1.contrastStretch();
cvColorImage1.blur(5);
cvColorImage1.erode();
cvColorImage1.erode();
cvColorImage1.dilate();
cvColorImage1.blur(5);
cvColorImage1.dilate();
cvColorImage1.dilate();
cvColorImage1.dilate();
cvColorImage1.dilate();
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
string file_name;
file_name = ofToString(_synth_save_cnt, 2)+"_ColorMultiAnalysis_"+ofToString(_run_cnt,2)+".jpg";
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
ofSaveImage(cvColorImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
// }
} }
} }
void ColorMultiAnalysis::display_results(){ // _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
Timer* display_results_timer;
TimerCallback<ColorMultiAnalysis> display_results_callback(*this, &ColorMultiAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=500;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
display_results_timer->stop();
} }
void ColorMultiAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
}
void ColorMultiAnalysis::draw() void ColorMultiAnalysis::draw()
{ {
switch (_state) { switch (_state) {
case STATE_ACQUIRING: case STATE_ACQUIRING:
{ {
ofEnableAlphaBlending();
if (_frame_cnt < _frame_cnt_max) if (_frame_cnt < _frame_cnt_max)
{ {
int _fade_in_frames = _frame_cnt_max/50; int _fade_in_frames = _frame_cnt_max/50;
ofColor aColor;
if (_frame_cnt < _fade_in_frames) { if (_frame_cnt < _fade_in_frames) {
ofColor aColor;
aColor.setHsb(c, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255), ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)); aColor.setHsb(c, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255), ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColor); ofSetColor(aColor);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
cout << "FADING IN..." << endl; //cout << "FADING IN..." << endl;
} }
if (_frame_cnt >= _fade_in_frames && _frame_cnt < _frame_cnt_max-_fade_in_frames){ if (_frame_cnt >= _fade_in_frames && _frame_cnt < _frame_cnt_max-_fade_in_frames){
ofColor aColor;
aColor.setHsb(c, 255, 255); aColor.setHsb(c, 255, 255);
ofSetColor(aColor); ofSetColor(aColor);
@ -180,28 +224,26 @@ void ColorMultiAnalysis::draw()
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
} }
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) { if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt <= _frame_cnt_max) {
ofColor aColor; aColor.set(c, c, c, 255-int(ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255)));
aColor.setHsb(c, 255-ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255), 255-ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255)); //aColor.setHsb(c, 255-ofMap(_fade_cnt- (_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255), 255-(ofMap(_fade_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255)));
ofSetColor(aColor); ofSetColor(aColor);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
//cout << "FADING OUT..." << endl;
_fade_cnt++;
cout << "FADING OUT..." << endl;
} }
} else { } else {
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
_frame_cnt++; _frame_cnt++;
ofDisableAlphaBlending();
break; break;
} }
@ -209,31 +251,116 @@ void ColorMultiAnalysis::draw()
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "ColorMultiAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "ColorMultiAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices(); //cout << "STATE_DISPLAY_RESULTS...\n" << endl;
ofPopMatrix();
whichMesh+=speed;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1; if (_frame_cnt > 2)
whichMesh+=speed; {
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
if (_show_image)
{
//cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_COLOR);
//image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break; break;
} }
@ -248,19 +375,24 @@ void ColorMultiAnalysis::draw()
void ColorMultiAnalysis::save_cb(Timer& timer) void ColorMultiAnalysis::save_cb(Timer& timer)
{ {
// cout << "COLORMULTIANALYSIS::saving...\n";
string file_name = ofToString(_save_cnt,2)+"_"+ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++; _save_cnt++;
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
if (RefractiveIndex::_vidGrabber.isFrameNew())
{
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
} else {
return;
} }
void ColorMultiAnalysis::display_results_cb(Timer& timer){ //cout << "ColorMultiAnalysis::saving...\n";
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}
string file_name = ofToString(_save_cnt,2)+"_"+ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
}

View File

@ -4,6 +4,9 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ColorMultiAnalysis : public AbstractAnalysis class ColorMultiAnalysis : public AbstractAnalysis
{ {
public: public:
@ -15,15 +18,41 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _run_cnt, _save_cnt, _fade_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max; int _run_cnt, _save_cnt, _fade_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };

View File

@ -1,8 +1,3 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ColorSingleAnalysis.h" #include "ColorSingleAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -18,20 +13,54 @@ using Poco::Thread;
void ColorSingleAnalysis::setup(int camWidth, int camHeight) void ColorSingleAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 100;//300;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
create_dir(); NUM_RUN = 5;
int acq_run_time = 25; // 20 seconds of acquiring per run
DELTA_T_SAVE = 1*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir();
_run_cnt = 0;
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000); _synth_save_cnt = 0;
c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
r = 0; r = 0;
g = 0; g = 0;
b = 0; b = 0;
_fade_cnt=0;
fileNameTag = ""; fileNameTag = "";
_show_image = false;
_image_shown = false;
// 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);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
} }
@ -42,94 +71,127 @@ void ColorSingleAnalysis::acquire()
TimerCallback<ColorSingleAnalysis> save_callback(*this, &ColorSingleAnalysis::save_cb); TimerCallback<ColorSingleAnalysis> save_callback(*this, &ColorSingleAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir();
// RUN ROUTINE // RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) { //for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i;
_run_cnt = i; //cout << "RUN NUM = " << i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE) while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
// }
} }
void ColorSingleAnalysis::synthesise() void ColorSingleAnalysis::synthesise()
{ {
// _saved_filenames has all the file names of all the saved images
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; //cout << "ColorSingleAnalysis::saving synthesis...\n";
float iterator=1; if(_state == STATE_STOP) return;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; //cout << "ColorSingleAnalysis::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])){
//clear vector so we don't add to it on successive runs //cout << "LOADED image1!!!" << endl;
meshes.clear(); //if(image5.loadImage(_saved_filenames_analysis[i+1])){
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){ ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES //////////////////////////
ofImage image1; cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height);
ofImage image2; //cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height);
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false) //cvGrayImage1 = cvColorImage1;
image1.setUseTexture(false); //cvGrayImage2 = cvColorImage2;
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
if(i<_saved_filenames.size()/3){ //cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_RED), image2, &meshes[index]); //cvGrayDiff1.erode();
} //cvGrayDiff1.contrastStretch();
if(i>=_saved_filenames.size()/3 && i<2* _saved_filenames.size()/3){ //cvGrayDiff1.blur(5);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_GREEN), image2, &meshes[index]); //cvGrayDiff1.dilate();
} //cvGrayDiff1.contrastStretch();
if(i>= 2* _saved_filenames.size()/3 && i<_saved_filenames.size()){
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BLUE), image2, &meshes[index]); cvColorImage1.contrastStretch();
} cvColorImage1.blur(5);
index++; cvColorImage1.erode();
} cvColorImage1.erode();
cvColorImage1.dilate();
cvColorImage1.blur(5);
cvColorImage1.erode();
cvColorImage1.erode();
cvColorImage1.erode();
cvColorImage1.erode();
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
string file_name;
file_name = ofToString(_synth_save_cnt, 2)+"_ColorSingleAnalysis_"+ofToString(_run_cnt,2)+".jpg";
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
ofSaveImage(cvColorImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
// }
} }
} }
void ColorSingleAnalysis::display_results(){ // _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
Timer* display_results_timer;
TimerCallback<ColorSingleAnalysis> display_results_callback(*this, &ColorSingleAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=500;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
display_results_timer->stop(); }
void ColorSingleAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
void ColorSingleAnalysis::draw() void ColorSingleAnalysis::draw()
{ {
@ -179,8 +241,8 @@ void ColorSingleAnalysis::draw()
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max){ if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max){
ofSetColor(0, 0, 255-ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255)); int fade = ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255);
cout << "255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)"<< 255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255) << endl; ofSetColor(0, 0, 255-fade);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
_fade_cnt++; _fade_cnt++;
@ -188,6 +250,7 @@ void ColorSingleAnalysis::draw()
} }
} else { } else {
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -199,32 +262,119 @@ void ColorSingleAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "ColorSingleAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
// cout << "ColorSingleAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices(); //cout << "STATE_DISPLAY_RESULTS...\n" << endl;
ofPopMatrix();
whichMesh+=speed;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1; if (_frame_cnt > 2)
whichMesh+=speed; {
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
if (_show_image)
{
//cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_COLOR);
//image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break; break;
} }
default: default:
@ -236,22 +386,24 @@ void ColorSingleAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void ColorSingleAnalysis::save_cb(Timer& timer) void ColorSingleAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++;
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
if (RefractiveIndex::_vidGrabber.isFrameNew())
{
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
} else {
return;
}
//cout << "ColorSingleAnalysis::saving...\n"; //cout << "ColorSingleAnalysis::saving...\n";
string file_name =ofToString(_save_cnt,2)+"_"+fileNameTag+"_"+ofToString(_run_cnt,2)+".jpg"; string file_name =ofToString(_save_cnt,2)+"_"+fileNameTag+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++; ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
} }
void ColorSingleAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,6 +9,9 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ColorSingleAnalysis : public AbstractAnalysis class ColorSingleAnalysis : public AbstractAnalysis
{ {
public: public:
@ -20,16 +23,43 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
string fileNameTag; string fileNameTag;
int _run_cnt, _save_cnt, _fade_cnt; float r,g,b;
float r,g,b, _frame_cnt, _frame_cnt_max , _results_cnt, _results_cnt_max;
int _run_cnt, _save_cnt, _fade_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };

View File

@ -1,8 +1,3 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "DiffNoiseAnalysis.h" #include "DiffNoiseAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -18,109 +13,183 @@ using Poco::Thread;
void DiffNoiseAnalysis::setup(int camWidth, int camHeight) void DiffNoiseAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 600; // right number is about 450
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 50;
create_dir(); NUM_RUN = 5;
_fade_cnt=0;
int acq_run_time = 20; // 20 seconds of acquiring per run
DELTA_T_SAVE = 1*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir();
_run_cnt = 0;
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000); _synth_save_cnt = 0;
c = 0; c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
//for an ofxOpenCv.h image i would like to use..."
//image3.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h);
// 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);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
} }
void DiffNoiseAnalysis::acquire() void DiffNoiseAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<DiffNoiseAnalysis> save_callback(*this, &DiffNoiseAnalysis::save_cb); TimerCallback<DiffNoiseAnalysis> save_callback(*this, &DiffNoiseAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir();
// RUN ROUTINE // RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) { //for(int i = 0; i < NUM_RUN; i++) {
_run_cnt = i; // _run_cnt = i;
//cout << "RUN NUM = " << i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE) save_timer->start(save_callback);
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
//}
} }
void DiffNoiseAnalysis::synthesise() void DiffNoiseAnalysis::synthesise()
{ {
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; //cout << "DiffNoiseAnalysis::saving synthesis...\n";
float iterator=1; if(_state == STATE_STOP) return;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; //cout << "DiffNoiseAnalysis::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])){
//clear vector so we don't add to it on successive runs //cout << "LOADED image1!!!" << endl;
meshes.clear(); //if(image5.loadImage(_saved_filenames_analysis[i+1])){
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){ ///////////////////////// 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);
ofImage image1; //cvGrayImage1 = cvColorImage1;
ofImage image2; //cvGrayImage2 = cvColorImage2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false) //cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
image1.setUseTexture(false); //cvGrayDiff1.erode();
image2.setUseTexture(false); //cvGrayDiff1.contrastStretch();
//some of the textures are not loading correctly so only make mesh if both the images load //cvGrayDiff1.blur(5);
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){ //cvGrayDiff1.dilate();
meshes.push_back(ofMesh()); //cvGrayDiff1.contrastStretch();
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]); cvColorImage1.contrastStretch();
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_HUE,_recorded_brightness_value), image2, &meshes[index]); cvColorImage1.invert();
index++; cvColorImage1.blur(5);
cvColorImage1.erode();
cvColorImage1.blur(5);
cvColorImage1.erode();
cvColorImage1.contrastStretch();
//////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
string file_name;
file_name = ofToString(_synth_save_cnt, 2)+"_DiffNoiseAnalysis_"+ofToString(_run_cnt,2)+".jpg";
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
ofSaveImage(cvColorImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
// }
} }
} }
} // _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
void DiffNoiseAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<DiffNoiseAnalysis> display_results_callback(*this, &DiffNoiseAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
display_results_timer->stop(); }
}// this runs at frame rate = 33 ms for 30 FPS void DiffNoiseAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
}
// this runs at frame rate = 33 ms for 30 FPS
void DiffNoiseAnalysis::draw() void DiffNoiseAnalysis::draw()
{ {
switch (_state) { switch (_state) {
@ -149,12 +218,12 @@ void DiffNoiseAnalysis::draw()
} }
} }
cout << "FADING IN..." << endl; //cout << "FADING IN..." << endl;
} }
if (_frame_cnt >= _fade_in_frames && _frame_cnt < (_frame_cnt_max-_fade_in_frames)){ if (_frame_cnt >= _fade_in_frames && _frame_cnt <= (_frame_cnt_max-_fade_in_frames)){
for (int i=1; i < ofGetHeight() ; i=i+rectSize) for (int i=1; i < ofGetHeight() ; i=i+rectSize)
{ {
@ -170,29 +239,27 @@ void DiffNoiseAnalysis::draw()
} }
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) { if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt <= _frame_cnt_max) {
for (int i=1; i < ofGetHeight() ; i=i+rectSize) for (int i=1; i < ofGetHeight() ; i=i+rectSize)
{ {
for (int j=1; j < ofGetWidth(); j=j+rectSize) for (int j=1; j < ofGetWidth(); j=j+rectSize)
{ {
c = ofRandom(0,255); c = ofRandom(0,255);
aColour.set(c, c, c, 255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)); aColour.set(c, c, c, 255-ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255));
ofSetColor(aColour); ofSetColor(aColour);
ofRect(j, i, rectSize, rectSize); ofRect(j, i, rectSize, rectSize);
} }
} }
_fade_cnt++; //_fade_cnt++;
cout << "FADING OUT..." << endl; //cout << "FADING OUT..." << endl;
} }
ofDisableAlphaBlending(); ofDisableAlphaBlending();
} else { } else {
// _state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -205,73 +272,146 @@ void DiffNoiseAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "DiffNoiseAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "DiffNoiseAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis //cout << "STATE_DISPLAY_RESULTS...\n" << endl;
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
if (_show_image)
{
//cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_COLOR);
//image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break; break;
} }
default: default:
break; break;
} }
} }
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void DiffNoiseAnalysis::save_cb(Timer& timer) void DiffNoiseAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++;
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
float rand10 = ofRandom(0,10); if (RefractiveIndex::_vidGrabber.isFrameNew())
{
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
} else {
return;
}
if (rand10 > 5.0) { //cout << "DiffNoiseAnalysis::saving...\n";
cout << "DiffNoiseAnalysis::saving...\n";
cout << "c_last... " << c << endl;
cout<<"rand10... " <<rand10<<endl;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg"; string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
} ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
_save_cnt++; _save_cnt++;
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
} }
void DiffNoiseAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,6 +9,9 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class DiffNoiseAnalysis : public AbstractAnalysis class DiffNoiseAnalysis : public AbstractAnalysis
{ {
public: public:
@ -20,16 +23,40 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _run_cnt, _save_cnt, _fade_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
int _run_cnt, _save_cnt, _fade_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };

View File

@ -1,8 +1,3 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "IResponseAnalysis.h" #include "IResponseAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -15,19 +10,51 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
void IResponseAnalysis::setup(int camWidth, int camHeight) void IResponseAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 100;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
create_dir(); NUM_RUN = 5;
int acq_run_time = 20; // 20 seconds of acquiring per run
DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir();
_synth_save_cnt = 0;
_run_cnt = 0;
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0; c = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
// 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);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
} }
@ -35,94 +62,120 @@ void IResponseAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<IResponseAnalysis> save_callback(*this, &IResponseAnalysis::save_cb); TimerCallback<IResponseAnalysis> save_callback(*this, &IResponseAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir();
// RUN ROUTINE // RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) { //for(int i = 0; i < NUM_RUN; i++) {
//_run_cnt = i;
_run_cnt = i; //cout << "RUN NUM = " << i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE) save_timer->start(save_callback);
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
}
//}
}
void IResponseAnalysis::synthesise() void IResponseAnalysis::synthesise()
{ {
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; //cout << "IResponseAnalysis::saving synthesis...\n";
float iterator=1; if(_state == STATE_STOP) return;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; //cout << "IResponseAnalysis::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);
cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height);
cvGrayImage1 = cvColorImage1;
cvGrayImage2 = cvColorImage2;
cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
cvGrayDiff1.erode();
cvGrayDiff1.contrastStretch();
//cvGrayDiff1.blur(5);
//cvGrayDiff1.dilate();
//cvGrayDiff1.contrastStretch();
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
string file_name;
file_name = ofToString(_synth_save_cnt, 2)+"_IResponseSynthesis_"+ofToString(_run_cnt,2)+".jpg";
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
ofSaveImage(cvGrayDiff1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
} }
//clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS,_recorded_brightness_value), image2, &meshes[index]);
index++;
} }
} }
} // _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
void IResponseAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<IResponseAnalysis> display_results_callback(*this, &IResponseAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
display_results_timer->stop();
} }
void IResponseAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
// cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
}
// this runs at frame rate = 33 ms for 30 FPS // this runs at frame rate = 33 ms for 30 FPS
void IResponseAnalysis::draw() void IResponseAnalysis::draw()
{ {
@ -131,9 +184,18 @@ void IResponseAnalysis::draw()
case STATE_ACQUIRING: case STATE_ACQUIRING:
{ {
/// *** TODO *** /// ofEnableAlphaBlending();
// still need to deal with latency frames here - i.e.: there are frames ofColor aColour;
/// *** TODO *** /// int _fade_in_frames = _frame_cnt_max/10;
//cout<< "_fade_in_frames" << _fade_in_frames<< endl;
if (_frame_cnt < _fade_in_frames) {
aColour.set(255, 255, 255, ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight());
}
if (_frame_cnt < _frame_cnt_max) if (_frame_cnt < _frame_cnt_max)
{ {
@ -141,9 +203,18 @@ void IResponseAnalysis::draw()
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
c = 255.0 * (_frame_cnt_max*_frame_cnt_max - _frame_cnt*_frame_cnt)/(_frame_cnt_max*_frame_cnt_max); c = 255.0 * (_frame_cnt_max*_frame_cnt_max - _frame_cnt*_frame_cnt)/(_frame_cnt_max*_frame_cnt_max);
} else { } else {
_RUN_DONE = true; _RUN_DONE = true;
} }
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) {
aColour.set(0, 0, 0, ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255));
ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight());
// cout << "FADE OUT STROBE TIME " << endl;
}
ofDisableAlphaBlending();
_frame_cnt++; _frame_cnt++;
break; break;
@ -151,37 +222,119 @@ void IResponseAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going...
// cout << "IResponse = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "IResponse STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
//cout << "STATE_DISPLAY_RESULTS...\n" << endl;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
}
_frame_cnt++;
if (_show_image)
{
//cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis // display results of the synthesis
int imageWidth=640; _RUN_DONE = true;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
}
break; break;
}
}
default: default:
break; break;
@ -192,21 +345,21 @@ void IResponseAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void IResponseAnalysis::save_cb(Timer& timer) void IResponseAnalysis::save_cb(Timer& timer)
{ {
//cout << "IResponseAnalysis::saving...\n";
//cout << "c_last... " << c << endl;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++; _save_cnt++;
//if(_save_cnt >= NUM_SAVE_PER_RUN) RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
// _RUN_DONE = true;
} if (RefractiveIndex::_vidGrabber.isFrameNew())
void IResponseAnalysis::display_results_cb(Timer& timer){ {
_results_cnt++; RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
if (_results_cnt>_results_cnt_max) { } else {
_RUN_DONE=true; return;
}
} }
//cout << "IResponseAnalysis::saving...\n";
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
}

View File

@ -9,6 +9,8 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class IResponseAnalysis : public AbstractAnalysis class IResponseAnalysis : public AbstractAnalysis
{ {
public: public:
@ -20,17 +22,40 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _run_cnt, _save_cnt; int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max; float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };

View File

@ -1,8 +1,3 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "RelaxRateAnalysis.h" #include "RelaxRateAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -18,112 +13,162 @@ using Poco::Thread;
void RelaxRateAnalysis::setup(int camWidth, int camHeight) void RelaxRateAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 130; NUM_RUN = 5;
NUM_PHASE = 1;
NUM_RUN = 3;
NUM_SAVE_PER_RUN = 100;
create_dir(); int acq_run_time = 20; // 20 seconds of acquiring per run
DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
// or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir();
_run_cnt = 0;
_level = 0; _level = 0;
_flip = 1; _flip = 1;
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0; c = 0;
_synth_save_cnt = 0;
int anim_time = 5; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
// 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);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
} }
void RelaxRateAnalysis::acquire() void RelaxRateAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<RelaxRateAnalysis> save_callback(*this, &RelaxRateAnalysis::save_cb); TimerCallback<RelaxRateAnalysis> save_callback(*this, &RelaxRateAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_RUN_DONE = false;
create_dir();
// RUN ROUTINE // RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) { //for(int i = 0; i < NUM_RUN; i++) {
//_run_cnt = i;
_run_cnt = i; //cout << "RUN NUM = " << i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE) save_timer->start(save_callback);
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
} }
void RelaxRateAnalysis::synthesise() void RelaxRateAnalysis::synthesise()
{ {
/* //cout << "IResponseAnalysis::saving synthesis...\n";
//incrementer to whichMesh if(_state == STATE_STOP) return;
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. //cout << "IResponseAnalysis::synthesis FOR LOOP...\n";
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; //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])){
//clear vector so we don't add to it on successive runs //cout << "LOADED image1!!!" << endl;
meshes.clear(); if(image5.loadImage(_saved_filenames_analysis[i+1])){
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){ ///////////////////////// 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);
ofImage image1; cvGrayImage1 = cvColorImage1;
ofImage image2; cvGrayImage2 = cvColorImage2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false) cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
image1.setUseTexture(false); cvGrayDiff1.erode();
image2.setUseTexture(false); cvGrayDiff1.contrastStretch();
//some of the textures are not loading correctly so only make mesh if both the images load cvGrayDiff1.blur(5);
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh()); /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
cout<<"setting mesh"<<endl; string file_name;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS,_recorded_brightness_value), image2, &meshes[index]); file_name = ofToString(_synth_save_cnt, 2)+"_RelaxRateAnalysis_"+ofToString(_run_cnt,2)+".jpg";
index++;
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
ofSaveImage(cvGrayDiff1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
} }
} }
*/
} }
void RelaxRateAnalysis::display_results(){
/* // _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
Timer* display_results_timer;
TimerCallback<RelaxRateAnalysis> display_results_callback(*this, &RelaxRateAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
display_results_timer->stop(); }
*/
void RelaxRateAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
//cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
// cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
@ -156,13 +201,12 @@ void RelaxRateAnalysis::draw()
} }
} else { } else {
cout << "RELAXRATE RUN COMPLETED" << endl;
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
_frame_cnt++; _frame_cnt++;
cout << "_frame_cnt:" << _frame_cnt << endl; //cout << "_frame_cnt:" << _frame_cnt << endl;
break; break;
} }
@ -170,67 +214,146 @@ void RelaxRateAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
//cout << "RelaxRateAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
//cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = MIDDLE OF ANIMATION...\n";
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
cout << "_anim_cnt = " << _anim_cnt << endl;
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = FADE OUT OF ANIMATION...\n";
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
//cout << "c_anim = " << c_anim << endl;
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
/*
// display results of the synthesis
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
*/
_frame_cnt++;
if (_show_image)
{
//cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break; break;
}
}
default: default:
break; break;
} }
} }
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void RelaxRateAnalysis::save_cb(Timer& timer) void RelaxRateAnalysis::save_cb(Timer& timer)
{ {
//cout << "RelaxRateAnalysis::saving...\n";
//cout << "c_last... " << c << endl;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++; _save_cnt++;
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
//if(_save_cnt >= NUM_SAVE_PER_RUN) if (RefractiveIndex::_vidGrabber.isFrameNew())
// _RUN_DONE = true; {
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
} else {
return;
}
//cout << "RelaxRateAnalysis::saving...\n";
} string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
void RelaxRateAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}
ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
}

View File

@ -1,7 +1,3 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#pragma once #pragma once
@ -9,6 +5,7 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class RelaxRateAnalysis : public AbstractAnalysis class RelaxRateAnalysis : public AbstractAnalysis
{ {
@ -21,17 +18,42 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
float _flip, _level; float _flip, _level;
int _run_cnt, _save_cnt; int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max; float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };

View File

@ -1,35 +1,3 @@
/*
- copyright (c) 2011 Copenhagen Institute of Interaction Design (CIID)
- all rights reserved.
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ShadowScapesAnalysis.h" #include "ShadowScapesAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -37,6 +5,8 @@
#include "Poco/Thread.h" #include "Poco/Thread.h"
#include "RefractiveIndex.h" #include "RefractiveIndex.h"
#include "ofxOpenCv.h"
using Poco::Timer; using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
@ -46,121 +16,214 @@ using Poco::Thread;
void ShadowScapesAnalysis::setup(int camWidth, int camHeight) void ShadowScapesAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 100; NUM_RUN = 5;
NUM_PHASE = 1;
NUM_RUN = 1; int acq_run_time = 15; // 10 seconds of acquiring per run
NUM_SAVE_PER_RUN = 100;
int screenSpan;
if (_dir == V) screenSpan = ofGetHeight();
if (_dir == H) screenSpan = ofGetWidth();
if (_dir == D) screenSpan = ofGetHeight();
_step = (screenSpan/acq_run_time)/(ofGetFrameRate());
// pixel per frame = (pixels / sec) / (frame / sec)
// 40 pixels per second should give us a 20 second scan at 800 pixels wide
DELTA_T_SAVE = 3*(10*acq_run_time/2); // for 20 seconds, we want this to be around 100 files
// or 5 times per second = every 200 ms
//create_dir(); // this makes both synth and analysis folder structures
create_dir();
_speed = 900.0; // 900.0 is the correct number
_scanLineWidth = 100.0; _scanLineWidth = 100.0;
_run_cnt = 0; _run_cnt = 0;
_save_cnt = 0; _save_cnt = 0;
_synth_save_cnt = 0;
int anim_time = 10; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
// 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);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
} }
void ShadowScapesAnalysis::acquire() void ShadowScapesAnalysis::acquire()
{ {
int w;
if (_dir == V) w = ofGetHeight();
if (_dir == H) w = ofGetWidth();
if (_dir == D) w = ofGetHeight();
_step = ((w/_speed) * 1000.0) / 500.0;
_line = 0;
// RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) {
Timer save_timer(0, DELTA_T_SAVE); Timer save_timer(0, DELTA_T_SAVE);
TimerCallback<ShadowScapesAnalysis> save_callback(*this, &ShadowScapesAnalysis::save_cb); TimerCallback<ShadowScapesAnalysis> save_callback(*this, &ShadowScapesAnalysis::save_cb);
_run_cnt++;
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_line = 0;
_RUN_DONE = false; _RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
create_dir();
//cout << "RUN NUM = " << i;
save_timer.start(save_callback); save_timer.start(save_callback);
while(!_RUN_DONE) while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer.stop(); save_timer.stop();
}
//}
} }
void ShadowScapesAnalysis::synthesise() void ShadowScapesAnalysis::synthesise()
{ {
//incrementer to whichMesh //cout << "ShadowScapesAnalysis::saving synthesis...\n";
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. // cout << "ShadowScapesAnalysis::synthesis FOR LOOP...\n";
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; // 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])){
//clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){ if(image5.loadImage(_saved_filenames_analysis[i+1])){
///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES //////////////////////////
ofImage image1; cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height);
ofImage image2; cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height);
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false) cvGrayImage1 = cvColorImage1;
image1.setUseTexture(false); cvGrayImage2 = cvColorImage2;
image2.setUseTexture(false);
string fnamei = _saved_filenames[i]; cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
string fnameii = _saved_filenames[i + 1]; cvGrayDiff1.erode();
cvGrayDiff1.contrastStretch();
cvGrayDiff1.blur(5);
cvGrayDiff1.dilate();
cvGrayDiff1.contrastStretch();
bool l1 = image1.loadImage(_saved_filenames[i]); /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
bool l2 = image2.loadImage(_saved_filenames[i+1]); string file_name;
if(_dir == H) {
//some of the textures are not loading correctly so only make mesh if both the images load file_name = ofToString(_synth_save_cnt, 2)+"_H_ShadowScapesSynthesis_"+ofToString(_run_cnt,2)+".jpg";
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS), image2, &meshes[index]);
index++;
} }
image1.clear(); if(_dir == V) {
image2.clear(); file_name = ofToString(_synth_save_cnt, 2)+"_V_ShadowScapesSynthesis_"+ofToString(_run_cnt,2)+".jpg";
} }
if(_dir == D) {
file_name = ofToString(_synth_save_cnt, 2)+"_D_ShadowScapesSynthesis_"+ofToString(_run_cnt,2)+".jpg";
} }
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
void ShadowScapesAnalysis::display_results(){ ofSaveImage(cvGrayDiff1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
Timer* display_results_timer; }
}
}
TimerCallback<ShadowScapesAnalysis> display_results_callback(*this, &ShadowScapesAnalysis::display_results_cb); // _saved_filenames_synthesis has processed all the files in the analysis images folder
// display results of the synthesis while(!_RUN_DONE && _state != STATE_STOP)
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
}
display_results_timer->stop();
void ShadowScapesAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
// cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
// THE OLD SHIT ///
/*
for(float i=1;i<_saved_filenames_synthesis.size()-1;i++){
// cout << "_saved_filenames_analysis[i]" << _saved_filenames_analysis[i] << endl;
if(_state == STATE_STOP) return;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image1.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image1.loadImage(_saved_filenames_analysis[i])){
image1.loadImage(_saved_filenames_analysis[i]);
cout << "loaded filenames[i] - " << _saved_filenames_analysis[i] << endl;
image2.setFromPixels(image1.getPixels(),image1.width,image1.height, OF_IMAGE_COLOR);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
} }
}
*/
}
// the animation draw - and the output draw // the animation draw - and the output draw
void ShadowScapesAnalysis::draw() void ShadowScapesAnalysis::draw()
{ {
@ -231,24 +294,22 @@ void ShadowScapesAnalysis::draw()
} }
if(_dir == V && int(_line) >= (ofGetHeight()+4*_scanLineWidth)){ if(_dir == V && int(_line) >= (ofGetHeight()+4*_scanLineWidth)){
cout << "VERTICAL IS DONE - _line >= (ofGetHeight()+4*_scanLineWidth) is TRUE" << endl; //cout << "VERTICAL IS DONE - _line >= (ofGetHeight()+4*_scanLineWidth) is TRUE" << endl;
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
if(_dir == H && int(_line) >= (ofGetWidth()+4*_scanLineWidth)) { if(_dir == H && int(_line) >= (ofGetWidth()+4*_scanLineWidth)) {
//cout << "HORIZONTAL IS DONE - _line >= (ofGetWidth()+4*_scanLineWidth)) is TRUE" << endl; //cout << "HORIZONTAL IS DONE - _line >= (ofGetWidth()+4*_scanLineWidth)) is TRUE" << endl;
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
if(_dir == D && int(_line) >= (1.5*ofGetHeight()+4*_scanLineWidth)) { if(_dir == D && int(_line) >= (1.5*ofGetHeight()+4*_scanLineWidth)) {
//cout << "DIAGONAL IS DONE - _line >= (1.5*ofGetHeight()+4*_scanLineWidth)) is TRUE" << endl; //cout << "DIAGONAL IS DONE - _line >= (1.5*ofGetHeight()+4*_scanLineWidth)) is TRUE" << endl;
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -257,35 +318,141 @@ void ShadowScapesAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // cout << "ShadowScapesAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
// cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis //cout << "STATE_DISPLAY_RESULTS...\n" << endl;
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix(); if (_frame_cnt > 2)
whichMesh+=speed; {
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl; _image_shown = true;
if(whichMesh>meshes.size() -1 || whichMesh<0){ _frame_cnt=0;
speed*=-1; }
whichMesh+=speed;
_frame_cnt++;
if (_show_image)
{
// cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
//cvGrayDiff1.draw(0,0, ofGetWidth(), ofGetHeight());
// THE OLD SHIT
/*
ofEnableAlphaBlending();
ofSetColor(255, 255, 255, 200);
image3.setFromPixels(image1.getPixels(),image1.width,image1.height, OF_IMAGE_COLOR);
image4.setFromPixels(image2.getPixels(),image2.width,image2.height, OF_IMAGE_COLOR);
cvColorImage1.setFromPixels(image3.getPixels(), image3.width, image3.height);
cvColorImage2.setFromPixels(image4.getPixels(), image4.width, image4.height);
cvGrayImage1 = cvColorImage1;
cvGrayImage2 = cvColorImage2;
cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
cvGrayDiff1.contrastStretch();
cvGrayDiff1.dilate();
cvGrayDiff1.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
*/
//image2.draw(0,0, ofGetWidth(), ofGetHeight());
} }
// display results of the synthesis
_RUN_DONE = true;
break; break;
} }
@ -299,15 +466,19 @@ void ShadowScapesAnalysis::draw()
void ShadowScapesAnalysis::save_cb(Timer& timer) void ShadowScapesAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++;
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
if (RefractiveIndex::_vidGrabber.isFrameNew()) if (RefractiveIndex::_vidGrabber.isFrameNew())
{ {
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
} else {
return;
} }
//cout << "ShadowScapesAnalysis::saving analysis...\n";
cout << "ShadowScapesAnalysis::saving...\n";
string file_name; string file_name;
if(_dir == H) { if(_dir == H) {
@ -322,15 +493,8 @@ void ShadowScapesAnalysis::save_cb(Timer& timer)
file_name = ofToString(_save_cnt, 2)+"_D_"+ofToString(_line, 2)+"_"+ofToString(_run_cnt,2)+".jpg"; file_name = ofToString(_save_cnt, 2)+"_D_"+ofToString(_line, 2)+"_"+ofToString(_run_cnt,2)+".jpg";
} }
saveimage(file_name); ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_save_cnt++; _saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
} }
void ShadowScapesAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -1,34 +1,4 @@
/* /* */
- copyright (c) 2011 Copenhagen Institute of Interaction Design (CIID)
- all rights reserved.
+ redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ > redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ > redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ SUCH DAMAGE.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#pragma once #pragma once
@ -36,6 +6,8 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
enum shadow_type { enum shadow_type {
H, V, D, H, V, D,
}; };
@ -52,11 +24,11 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
@ -66,7 +38,32 @@ protected:
float _scanLineWidth; // pix per second float _scanLineWidth; // pix per second
float _step; float _step;
shadow_type _dir; shadow_type _dir;
int _run_cnt, _save_cnt; int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max; float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };

View File

@ -1,8 +1,3 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ShapeFromShadingAnalysis.h" #include "ShapeFromShadingAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -15,43 +10,65 @@ using Poco::Timer;
using Poco::TimerCallback; using Poco::TimerCallback;
using Poco::Thread; using Poco::Thread;
void ShapeFromShadingAnalysis::setup(int camWidth, int camHeight) void ShapeFromShadingAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 300; NUM_RUN = 5;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
create_dir(); int acq_run_time = 20; // 20 seconds of acquiring per run
_level = 0; DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 200 files
_flip = 1; // or 10 times per second = every 100 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
//create_dir();
_synth_save_cnt = 0;
_run_cnt = 0;
_frame_cnt = 0; _frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
c = 0; c = 0;
int anim_time = 5; // 10 seconds
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
// 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);
// cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
// cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
} }
void ShapeFromShadingAnalysis::acquire() void ShapeFromShadingAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<ShapeFromShadingAnalysis> save_callback(*this, &ShapeFromShadingAnalysis::save_cb); TimerCallback<ShapeFromShadingAnalysis> save_callback(*this, &ShapeFromShadingAnalysis::save_cb);
// RUN ROUTINE _run_cnt++;
for(int i = 0; i < NUM_RUN; i++) { _frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0, _synth_save_cnt = 0;
_run_cnt = i;
cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback);
_RUN_DONE = false; _RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0; create_dir();
_animation_cnt1 = 0; _animation_cnt1 = 0;
_animation_cnt2 = 0; _animation_cnt2 = 0;
_animation_cnt3 = 0; _animation_cnt3 = 0;
@ -70,76 +87,108 @@ void ShapeFromShadingAnalysis::acquire()
_animation_cnt16 = 0; _animation_cnt16 = 0;
_animation_reset = false; // coundn't get this to work - so using seperate counters - shitty! _animation_reset = false; // coundn't get this to work - so using seperate counters - shitty!
while(!_RUN_DONE) // RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i;
//cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback);
while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
}
// }
}
void ShapeFromShadingAnalysis::synthesise() void ShapeFromShadingAnalysis::synthesise()
{ {
//incrementer to whichMesh // cout << "ShapeFromShadingAnalysis::saving synthesis...\n";
speed=0.2; if(_state == STATE_STOP) return;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. // cout << "ShapeFromShadingAnalysis::synthesis FOR LOOP...\n";
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; // 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);
cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height);
cvGrayImage1 = cvColorImage1;
cvGrayImage2 = cvColorImage2;
cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
cvGrayDiff1.erode();
cvGrayDiff1.contrastStretch();
//cvGrayDiff1.blur(5);
//cvGrayDiff1.dilate();
//cvGrayDiff1.contrastStretch();
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
string file_name;
file_name = ofToString(_synth_save_cnt, 2)+"_ShapeFromShadingSynthesis_"+ofToString(_run_cnt,2)+".jpg";
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
ofSaveImage(cvGrayDiff1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
} }
//clear vector so we don't add to it on successive runs
meshes.clear();
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofImage image1;
ofImage image2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false)
image1.setUseTexture(false);
image2.setUseTexture(false);
//some of the textures are not loading correctly so only make mesh if both the images load
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){
meshes.push_back(ofMesh());
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]);
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS,_recorded_brightness_value), image2, &meshes[index]);
index++;
} }
} }
} // _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
void ShapeFromShadingAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<ShapeFromShadingAnalysis> display_results_callback(*this, &ShapeFromShadingAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
display_results_timer->stop(); }
void ShapeFromShadingAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
// cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
// this runs at frame rate = 33 ms for 30 FPS // this runs at frame rate = 33 ms for 30 FPS
@ -154,23 +203,10 @@ void ShapeFromShadingAnalysis::draw()
ofEnableAlphaBlending(); ofEnableAlphaBlending();
int _quarter_frame_cnt_max = _frame_cnt_max/4; int _quarter_frame_cnt_max = _frame_cnt_max/4;
int _half_frame_cnt_max = _frame_cnt_max/2; int _half_frame_cnt_max = _frame_cnt_max/2;
int _threequarters_frame_cnt_max = 3*_frame_cnt_max/4; int _threequarters_frame_cnt_max = 3*_frame_cnt_max/4;
//TODO: put in CROSS FADES, ETC§E
/*
if (_animation_reset == true)
{
_animation_cnt1 = 0;
_animation_cnt2 = 0;
_animation_cnt3 = 0;
_animation_cnt4 = 0;
}
*/
if(_frame_cnt < _quarter_frame_cnt_max) { if(_frame_cnt < _quarter_frame_cnt_max) {
quad = "QUAD1"; quad = "QUAD1";
@ -311,10 +347,6 @@ void ShapeFromShadingAnalysis::draw()
_animation_cnt12++; _animation_cnt12++;
} }
} }
@ -366,6 +398,10 @@ void ShapeFromShadingAnalysis::draw()
} }
ofDisableAlphaBlending(); ofDisableAlphaBlending();
} else {
//_state = STATE_SYNTHESISING;
_frame_cnt = 0;
_RUN_DONE = true;
} }
_frame_cnt++; _frame_cnt++;
@ -377,33 +413,117 @@ void ShapeFromShadingAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
// cout << "RelaxRateAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
//cout << "ShadowScapesAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
//cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
//cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis // cout << "STATE_DISPLAY_RESULTS...\n" << endl;
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
if (_show_image)
{
// cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break; break;
} }
@ -417,23 +537,22 @@ void ShapeFromShadingAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void ShapeFromShadingAnalysis::save_cb(Timer& timer) void ShapeFromShadingAnalysis::save_cb(Timer& timer)
{ {
_save_cnt++;
RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
if (RefractiveIndex::_vidGrabber.isFrameNew())
{
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
} else {
return;
}
//cout << "ShapeFromShadingAnalysis::saving...\n"; //cout << "ShapeFromShadingAnalysis::saving...\n";
string file_name = ofToString(_save_cnt,2)+"_"+ quad +"_"+ofToString(_run_cnt,2)+".jpg"; string file_name = ofToString(_save_cnt,2)+"_"+ quad +"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name); ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
_save_cnt++;
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
} }
void ShapeFromShadingAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}

View File

@ -9,6 +9,8 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ShapeFromShadingAnalysis : public AbstractAnalysis class ShapeFromShadingAnalysis : public AbstractAnalysis
{ {
@ -21,18 +23,16 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
string quad; string quad;
bool _RUN_DONE; bool _RUN_DONE;
float _flip, _level; float _flip, _level;
int _run_cnt, _save_cnt;
int _animation_cnt1; int _animation_cnt1;
int _animation_cnt2; int _animation_cnt2;
int _animation_cnt3; int _animation_cnt3;
@ -50,6 +50,32 @@ protected:
int _animation_cnt15; int _animation_cnt15;
int _animation_cnt16; int _animation_cnt16;
int _animation_reset; // this reset part didn't get working - so using 16 different counters! yay! int _animation_reset; // this reset part didn't get working - so using 16 different counters! yay!
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };

View File

@ -1,8 +1,3 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "StrobeAnalysis.h" #include "StrobeAnalysis.h"
#include "ofMain.h" #include "ofMain.h"
@ -17,20 +12,49 @@ using Poco::Thread;
void StrobeAnalysis::setup(int camWidth, int camHeight) void StrobeAnalysis::setup(int camWidth, int camHeight)
{ {
DELTA_T_SAVE = 100; NUM_RUN = 5;
NUM_RUN = 1;
_strobe_cnt = 0; int acq_run_time = 25; // 20 seconds of acquiring per run
_strobe_cnt_max = 20; // 40 x 500 ms = 20000 ms = 20 seconds run time
_strobe_interval = 1000; //every 0.5seconds = 15 frames DELTA_T_SAVE = 2*(10*acq_run_time/2); // for 20 seconds, we want this to be around 100 files
_frame_cnt_max = _strobe_cnt_max * _strobe_interval * ofGetFrameRate()/1000; // or 5 times per second = every 200 ms
_frame_cnt_max = acq_run_time*ofGetFrameRate(); // e.g.: 30 frames per second * 20 seconds = 600 frames
_strobe_interval = 1750; //every 1 seconds, or every thirty frames 30 frames
// The British Health and Safety Executive recommend that a net flash rate for a bank of strobe lights does not exceed 5 flashes per second, at which only 5% of photosensitive epileptics are at risk. It also recommends that no strobing effect continue for more than 30 seconds, due to the potential for discomfort and disorientation. // The British Health and Safety Executive recommend that a net flash rate for a bank of strobe lights does not exceed 5 flashes per second, at which only 5% of photosensitive epileptics are at risk. It also recommends that no strobing effect continue for more than 30 seconds, due to the potential for discomfort and disorientation.
//or 20 times, every one second... //create_dir();
_save_cnt_max = _strobe_cnt_max*_strobe_interval/DELTA_T_SAVE; _synth_save_cnt = 0;
_run_cnt = 0;
int anim_time = 5; // 5 seconds for the animation
_anim_cnt_max = anim_time*ofGetFrameRate(); // e.g.: 30 frames per second = 150 frames
_show_image = false;
_image_shown = false;
// 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);
// cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
// cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
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);
create_dir();
} }
@ -38,94 +62,124 @@ void StrobeAnalysis::acquire()
{ {
Timer* save_timer; Timer* save_timer;
TimerCallback<StrobeAnalysis> save_callback(*this, &StrobeAnalysis::save_cb); TimerCallback<StrobeAnalysis> save_callback(*this, &StrobeAnalysis::save_cb);
_frame_cnt = 0; _save_cnt = 0; _anim_cnt = 0; _strobe_cnt = 0, _synth_save_cnt = 0;
_run_cnt++;
_RUN_DONE = false;
create_dir();
// RUN ROUTINE // RUN ROUTINE
for(int i = 0; i < NUM_RUN; i++) { //for(int i = 0; i < NUM_RUN; i++) {
_run_cnt = i; //_run_cnt = i;
_save_cnt = 0;
_frame_cnt = 0;
cout << "RUN NUM = " << i; //cout << "RUN NUM = " << i;
save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files save_timer = new Timer(0, DELTA_T_SAVE); // timing interval for saving files
save_timer->start(save_callback); save_timer->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE) while(!_RUN_DONE && _state != STATE_STOP)
Thread::sleep(3); Thread::sleep(3);
save_timer->stop(); save_timer->stop();
}
//}
} }
void StrobeAnalysis::synthesise() void StrobeAnalysis::synthesise()
{ {
//incrementer to whichMesh // cout << "StrobeAnalysis::saving synthesis...\n";
speed=0.2; if(_state == STATE_STOP) return;
//whichMesh is the index in the vector of meshes
whichMesh=0;
int index=0; for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
//hack to limit number of meshes. // cout << "StrobeAnalysis::synthesis FOR LOOP...\n";
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100; // 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])){
//clear vector so we don't add to it on successive runs // cout << "LOADED image1!!!" << endl;
meshes.clear(); //if(image5.loadImage(_saved_filenames_analysis[i+1])){
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){ ///////////////////////// 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);
ofImage image1; cvGrayImage1 = cvColorImage1;
ofImage image2; //cvGrayImage2 = cvColorImage2;
//there is a known issue with using loadImage inside classes in other directories. the fix is to call setUseTExture(false) //cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1);
image1.setUseTexture(false); //cvGrayDiff1.erode();
image2.setUseTexture(false); //cvGrayDiff1.contrastStretch();
//some of the textures are not loading correctly so only make mesh if both the images load //cvGrayDiff1.blur(5);
if(image1.loadImage(_saved_filenames[i]) && image2.loadImage(_saved_filenames[i+1])){ //cvGrayDiff1.dilate();
meshes.push_back(ofMesh()); //cvGrayDiff1.contrastStretch();
cout<<"setting mesh"<<endl;
int _recorded_brightness_value=getRecordedValueFromFileName(_saved_filenames[i]); cvGrayImage1.contrastStretch();
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BRIGHTNESS,_recorded_brightness_value), image2, &meshes[index]); cvGrayImage1.blur(10);
index++; cvGrayImage1.dilate();
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
string file_name;
file_name = ofToString(_synth_save_cnt, 2)+"_StrobeAnalysis_"+ofToString(_run_cnt,2)+".jpg";
//image4.setFromPixels(cvColorImage1.getPixelsRef(),image3.width, image3.height, OF_IMAGE_COLOR);
ofSaveImage(cvGrayImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
//ofSaveImage(cvGrayDiff1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name);
_synth_save_cnt++;
//}
} }
} }
} // _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
void StrobeAnalysis::display_results(){
Timer* display_results_timer;
TimerCallback<StrobeAnalysis> display_results_callback(*this, &StrobeAnalysis::display_results_cb);
// display results of the synthesis
display_results_timer = new Timer(0, 20); // timing interval for saving files
display_results_timer->start(display_results_callback);
_RUN_DONE = false;
_results_cnt=0;
_results_cnt_max=300;
while(!_RUN_DONE)
Thread::sleep(3); Thread::sleep(3);
}
display_results_timer->stop(); void StrobeAnalysis::displayresults()
{
for(float i=1;i<_saved_filenames_synthesis.size();i++){
if(_state == STATE_STOP) return;
// cout << "_saved_filenames_analysis[i] - " << _saved_filenames_synthesis[i] << endl;
while(!_image_shown){
Thread::sleep(2);
//cout << "!_image_shown" << endl;
}
if(!image3.loadImage(_saved_filenames_synthesis[i])){
//couldn't load image
// cout << "didn't load image" << endl;
}
if(image3.loadImage(_saved_filenames_synthesis[i])){
image3.loadImage(_saved_filenames_synthesis[i]);
//cout << "_show_image = true;" << endl;
_show_image = true;
_image_shown = false;
}
}
} }
// this runs at frame rate = 33 ms for 30 FPS // this runs at frame rate = 33 ms for 30 FPS
void StrobeAnalysis::draw() void StrobeAnalysis::draw()
{ {
@ -139,7 +193,7 @@ void StrobeAnalysis::draw()
ofEnableAlphaBlending(); ofEnableAlphaBlending();
ofColor aColour; ofColor aColour;
int _fade_in_frames = _frame_cnt_max/10; int _fade_in_frames = _frame_cnt_max/10;
cout<< "_fade_in_frames" << _fade_in_frames<< endl; // cout<< "_fade_in_frames" << _fade_in_frames<< endl;
if (_frame_cnt < _fade_in_frames) { if (_frame_cnt < _fade_in_frames) {
@ -157,13 +211,13 @@ void StrobeAnalysis::draw()
//cout << "frame_cnt % 15: " << _frame_cnt%15 << endl; //cout << "frame_cnt % 15: " << _frame_cnt%15 << endl;
//cout << "MAIN STROBE TIME " << endl; //cout << "MAIN STROBE TIME " << endl;
if (_frame_cnt%int(ofGetFrameRate()*_strobe_interval/1000) < (ofGetFrameRate()*_strobe_interval/1000)/2) if (int(_frame_cnt)%int(ofGetFrameRate()*_strobe_interval/1000) < (ofGetFrameRate()*_strobe_interval/1000)/2)
{ {
ofSetColor(255, 255, 255); ofSetColor(255, 255, 255);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
_strobe_cnt++; _strobe_cnt++;
_strobe_on = 1; _strobe_on = 1;
} else if (_frame_cnt%int(ofGetFrameRate()*_strobe_interval/1000) >= (ofGetFrameRate()*_strobe_interval/1000)/2) } else if (int(_frame_cnt)%int(ofGetFrameRate()*_strobe_interval/1000) >= (ofGetFrameRate()*_strobe_interval/1000)/2)
{ {
ofSetColor(0, 0, 0); ofSetColor(0, 0, 0);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
@ -173,7 +227,7 @@ void StrobeAnalysis::draw()
} }
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) { if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max) {
aColour.set(255, 255, 255, 255-ofMap(_frame_cnt, 0, _fade_in_frames, 0, 255)); aColour.set(255, 255, 255, 255-ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255));
ofSetColor(aColour); ofSetColor(aColour);
ofRect(0, 0, ofGetWidth(), ofGetHeight()); ofRect(0, 0, ofGetWidth(), ofGetHeight());
// cout << "FADE OUT STROBE TIME " << endl; // cout << "FADE OUT STROBE TIME " << endl;
@ -181,6 +235,7 @@ void StrobeAnalysis::draw()
ofDisableAlphaBlending(); ofDisableAlphaBlending();
} else { } else {
//_state = STATE_SYNTHESISING;
_RUN_DONE = true; _RUN_DONE = true;
} }
@ -192,33 +247,117 @@ void StrobeAnalysis::draw()
case STATE_SYNTHESISING: case STATE_SYNTHESISING:
{ {
// display animation of something while the synthesis in on-going... // display animation of something while the synthesis in on-going...
// cout << "StrobeAnalysis = STATE_SYNTHESISING...\n";
// display animation of something while the synthesis in on-going...
ofEnableAlphaBlending();
ofSetRectMode(OF_RECTMODE_CENTER);
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
if(_anim_cnt < _anim_cnt_max){
ofColor aColour;
int rectSizeW = ofGetWidth()/4;
int rectSizeH = ofGetHeight()/4;
int _fade_in_frames = _anim_cnt_max/2;
int c_anim = 10;
int fade;
//ofRotate(ofMap(_anim_cnt/2.0, 0, _anim_cnt_max, 0, 360));
if (_anim_cnt < _fade_in_frames) {
// cout << "StrobeAnalysis STATE_SYNTHESIZING = FADING IN ANIMATION...\n";
fade = ofMap(_anim_cnt, 0, _fade_in_frames, 0, 255);
for (int i=0; i <= 15; i++){
c_anim = 0+17*i;
aColour.set(c_anim, c_anim, c_anim, fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt >= _fade_in_frames && _anim_cnt <= (_anim_cnt_max-_fade_in_frames)){
for (int i=0; i <= 15; i++){
c_anim = 255;
aColour.set(c_anim, c_anim, c_anim, 255);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
if (_anim_cnt > (_anim_cnt_max-_fade_in_frames) && _anim_cnt <= _anim_cnt_max) {
// cout << "_anim_cnt = " << _anim_cnt-(_anim_cnt_max-_fade_in_frames) << endl;
fade = ofMap(_anim_cnt-(_anim_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255);
// cout << "fade down = " << fade << endl;
for (int i=0; i <= 15; i++){
c_anim = (17*i);
aColour.set(c_anim, c_anim, c_anim, 255-fade);
ofSetColor(aColour);
ofRect(0, 0, rectSizeW+10*i, rectSizeH+10*i);
ofRect(0, 0, rectSizeW-10*i, rectSizeH-10*i);
}
}
_anim_cnt++;
} else {
_RUN_DONE = true;
//_state = STATE_DISPLAY_RESULTS;
_anim_cnt=0;
}
ofPopMatrix();
ofSetRectMode(OF_RECTMODE_CORNER);
ofDisableAlphaBlending();
break; break;
} }
case STATE_DISPLAY_RESULTS: case STATE_DISPLAY_RESULTS:
{ {
// display results of the synthesis
int imageWidth=640;
int imageHeight =480;
ofPushMatrix();
ofTranslate(ofGetWidth()/2, ofGetHeight()/2);
ofRotateY(_results_cnt*0.3);
//ofRotateX(90);
//ofRotateZ(whichMesh);
ofTranslate(-ofGetWidth()/2, -ofGetHeight()/2),-400;
ofTranslate((ofGetWidth()/2)-(imageWidth/2),0,0 );
meshes[whichMesh].drawVertices(); // cout << "STATE_DISPLAY_RESULTS...\n" << endl;
ofPopMatrix();
whichMesh+=speed;
cout<<whichMesh<<" size of meshes "<<meshes.size()<<endl;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
} }
_frame_cnt++;
if (_show_image)
{
// cout << "_show_image...\n" << endl;
ofEnableAlphaBlending();
ofSetColor(255, 255, 255);
image2.setFromPixels(image3.getPixels(),image3.width,image3.height, OF_IMAGE_GRAYSCALE);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break; break;
} }
@ -231,27 +370,22 @@ void StrobeAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE // this runs at save_cb timer rate = DELTA_T_SAVE
void StrobeAnalysis::save_cb(Timer& timer) void StrobeAnalysis::save_cb(Timer& timer)
{ {
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(_strobe_on) +"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++; _save_cnt++;
cout << "_save_cnt" << _save_cnt << endl; RefractiveIndex::_vidGrabber.grabFrame(); // get a new frame from the camera
cout << "_save_cnt_max" << _save_cnt_max << endl;
//TODO: something fucked here with my calc of _save_cnt_max - new structure should fix it?
//if(_save_cnt >= _save_cnt_max-10) {
// _RUN_DONE = true;
//}
if (RefractiveIndex::_vidGrabber.isFrameNew())
{
RefractiveIndex::_pixels = RefractiveIndex::_vidGrabber.getPixelsRef(); //get ofPixels from the camera
} else {
return;
} }
void StrobeAnalysis::display_results_cb(Timer& timer){ //cout << "StrobeAnalysis::saving...\n";
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(_strobe_on) +"_"+ofToString(_run_cnt,2)+".jpg";
ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+file_name, OF_IMAGE_QUALITY_BEST);
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+file_name);
}

View File

@ -9,6 +9,8 @@
#include "Poco/Timer.h" #include "Poco/Timer.h"
#include "ofxOpenCv.h"
class StrobeAnalysis : public AbstractAnalysis class StrobeAnalysis : public AbstractAnalysis
{ {
@ -21,21 +23,46 @@ public:
void setup(int camWidth, int camHeight); void setup(int camWidth, int camHeight);
void acquire(); void acquire();
void synthesise(); void synthesise();
void display_results(); void displayresults();
void draw(); void draw();
void save_cb(Poco::Timer& timer); void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected: protected:
bool _RUN_DONE; bool _RUN_DONE;
int _strobe_cnt, _run_cnt, _strobe_cnt_max; int _strobe_cnt, _strobe_cnt_max;
int _save_cnt;
int _frame_cnt, _frame_cnt_max, _save_cnt_max ;
float _results_cnt, _results_cnt_max;
int _strobe_interval; int _strobe_interval;
bool _strobe_on; bool _strobe_on;
int _run_cnt, _save_cnt,_synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
bool _show_image, _image_shown;
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;
ofxCvContourFinder cvContourFinder1;
}; };