Compare commits

..

13 Commits

Author SHA1 Message Date
gauthiier
42d048ca04 ... 2023-09-08 08:20:39 +02:00
dviid
5c5581f16e mid-backed commit - master is dead 2012-02-21 18:06:58 +01:00
dviid
b5cebe9304 mid-commit AbstractAnalysis::saveimage 2012-02-20 14:10:20 +01:00
Tom Schofield
3649211943 merged with tom 2012-02-20 13:00:05 +00:00
Tom Schofield
bc19757de2 merge local master 2012-02-20 12:37:44 +00:00
Tom Schofield
d798394793 final check and commit before merge with master 2012-02-20 12:33:13 +00:00
Tom Schofield
bf8c630add make vector of meshes static since we only ever want to display one at a time 2012-02-20 12:00:18 +00:00
Tom Schofield
935d6414b5 tidy up 2012-02-20 11:33:26 +00:00
Tom Schofield
6b401b8424 added synthesise and display results to all analysis classes 2012-02-20 11:25:55 +00:00
Tom Schofield
3fd26c1eb4 fixed bug in color single 2012-02-19 18:51:25 +00:00
Tom Schofield
c53eb2eb6c corrected file confusion AGAIN 2012-02-19 18:25:37 +00:00
Tom Schofield
1d5d6abbde corrected file confusion 2012-02-19 18:17:45 +00:00
Tom Schofield
71e3a9a1db added image loading and display results stuff- changes all commented in code 2012-02-19 16:42:55 +00:00
29 changed files with 1700 additions and 8191 deletions

6
.gitignore vendored
View File

@ -1,8 +1,2 @@
src/.DS_Store
.DS_Store
Project.xcconfig
bin/
openFrameworks-Info.plist
opencvExample.xcodeproj/
config.refindx

10
README
View File

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

2
README.md Normal file
View File

@ -0,0 +1,2 @@
<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,5 +1,12 @@
/*
////also the new shit////
todo:
(1) Look at warinings about the #define which get over written
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "RefractiveIndex.h"
@ -15,11 +22,11 @@
#include "ofxXmlSettings.h"
#define CAMERA_ID 0
#define CAMERA_ID 1
#define CAMERA_ACQU_WIDTH 640
#define CAMERA_ACQU_HEIGHT 480
#define LOCATION "MANCHESTER"
#define LOCATION "MIDDLESBOROUGH"
#define ISTATE_UNDEF 0xEEEE
#define ISTATE_START 0xAAAA
@ -27,10 +34,8 @@
#define ISTATE_TRANSITION 0xCCCC
#define ISTATE_END 0xDDDD
int _state = ISTATE_UNDEF;
int RefractiveIndex::_mode;
ofPixels RefractiveIndex::_pixels;
ofVideoGrabber RefractiveIndex::_vidGrabber;
int RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, RefractiveIndex::_vid_id;
@ -38,32 +43,20 @@ bool RefractiveIndex::_vid_stream_open;
bool RefractiveIndex::_vid_toggle_on;
string RefractiveIndex::_location;
ofxXmlSettings RefractiveIndex::XML;
ofxXmlSettings XML;
void RefractiveIndex::setup()
{
camDist=1000;
ofBackground(0, 0, 0);
ofSetBackgroundAuto(false);
bool save_config = false;
cout << "Loading configuration..." << endl;
if(XML.loadFile("config.refindx") == false) {
if(!XML.loadFile("../data/config.refindx")) {
ofLog(OF_LOG_ERROR) << "error loading config - using default.";
save_config = true;
} else {
XML.loadFile("config.refindx");
}
// <mode>
string m = XML.getValue("config:mode", "analysing");
_mode = (m == "analysing" ? MODE_ANALYSING : (m == "drawing" ? MODE_DRAWING : MODE_ANALYSING));
// <camera>
_vid_id = XML.getValue("config:camera:id", CAMERA_ID);
cout << "_vid_id: " << _vid_id << endl;
_vid_w = XML.getValue("config:camera:width", CAMERA_ACQU_WIDTH);
_vid_h = XML.getValue("config:camera:height", CAMERA_ACQU_HEIGHT);
@ -78,7 +71,6 @@ void RefractiveIndex::setup()
// display
cout << "> display" << endl;
ofSetFrameRate(fps);
if(fps > 30) {
ofSetVerticalSync(FALSE);
} else {
@ -92,10 +84,8 @@ void RefractiveIndex::setup()
cout << "* cam width = " << _vid_w << endl;
cout << "* cam height = " << _vid_h << endl;
if(_mode == MODE_ANALYSING) {
_vid_stream_open = false;
setup_camera();
}
cout << "RRRRRREADY!" << endl;
@ -103,43 +93,42 @@ void RefractiveIndex::setup()
//getting a warning from the OFlog that the pixels aren't allocated
//void ofPixels::allocate(int w, int h, ofImageType type)
_pixels.allocate(_vid_w, _vid_h, OF_IMAGE_COLOR);
//TODO: whichever one of these is first - it always runs twice ?
_analysisVector.push_back(new ShadowScapesAnalysis(V)); //1
_analysisVector.push_back(new ShadowScapesAnalysis(H)); //2
_analysisVector.push_back(new ShadowScapesAnalysis(D)); //3
//_analysisVector.push_back(new ShadowScapesAnalysis(V));
//_analysisVector.push_back(new ShadowScapesAnalysis(H));
//_analysisVector.push_back(new ShadowScapesAnalysis(D));
_analysisVector.push_back(new RelaxRateAnalysis()); //4
_analysisVector.push_back(new RelaxRateAnalysis());
_analysisVector.push_back(new IResponseAnalysis()); //5
//_analysisVector.push_back(new IResponseAnalysis());
_analysisVector.push_back(new ShapeFromShadingAnalysis()); //6
//_analysisVector.push_back(new ShapeFromShadingAnalysis());
_analysisVector.push_back(new StrobeAnalysis()); //7
//_analysisVector.push_back(new StrobeAnalysis());
_analysisVector.push_back(new CamNoiseAnalysis()); //8
//_analysisVector.push_back(new CamNoiseAnalysis());
_analysisVector.push_back(new ColorSingleAnalysis()); //9
//_analysisVector.push_back(new ColorSingleAnalysis());
_analysisVector.push_back(new ColorMultiAnalysis()); //0
//_analysisVector.push_back(new ColorMultiAnalysis());
_analysisVector.push_back(new DiffNoiseAnalysis()); //Q
//_analysisVector.push_back(new DiffNoiseAnalysis());
//_currentAnalysisIndx = 0;
//_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
//_state = ISTATE_START;
_currentAnalysis = NULL;
_state = ISTATE_UNDEF;
_currentAnalysisIndx = 0;
_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_state = ISTATE_START;
}
void RefractiveIndex::analysis_cb(string & analysis)
{
assert(analysis == _currentAnalysis->_name);
_state = ISTATE_STOP;
}
@ -149,17 +138,6 @@ void RefractiveIndex::start_analysis()
_analysisAdapator = new AnalysisAdaptor(_currentAnalysis);
_currentAnalysis->setup(_vid_w, _vid_h);
_analysisAdapator->start();
//allocate fbo for HD
fbo.allocate(1920,1080);
// fbo.allocate( _currentAnalysis->_mesh_size_multiplier *_vid_w,_currentAnalysis->_mesh_size_multiplier * _vid_h);
//camera.setPosition((fbo.getWidth()/2), fbo.getHeight()/2,_currentAnalysis->_mesh_size_multiplier *500);
camera.setPosition(0, (fbo.getHeight()/2)+150, _currentAnalysis->_mesh_size_multiplier*500);
camera.setFov(65.0);
camera.setOrientation(ofVec3f(-2,0,0));
_meshRotation=0;
}
void RefractiveIndex::stop_analysis()
@ -183,9 +161,7 @@ void RefractiveIndex::state_analysis()
case ISTATE_TRANSITION:
if(_currentAnalysisIndx >= _analysisVector.size()) {
_currentAnalysisIndx = 0;
_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_state = ISTATE_START;
//_state = ISTATE_END;
_state = ISTATE_END;
} else {
_currentAnalysis = _analysisVector.at(_currentAnalysisIndx++);
_state = ISTATE_START;
@ -193,13 +169,9 @@ void RefractiveIndex::state_analysis()
break;
case ISTATE_STOP:
stop_analysis(); // blocking
if(_mode == MODE_DRAWING)
_state = ISTATE_UNDEF;
else
_state = ISTATE_TRANSITION;
break;
case ISTATE_END:
if(_mode == MODE_ANALYSING)
stop_camera();
::exit(1);
break;
@ -213,82 +185,35 @@ void RefractiveIndex::state_analysis()
void RefractiveIndex::update()
{
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()
{
// refractive mauve - this doesn't work... looks weird in various places.
//ofBackground(113, 110, 136);
// black
ofBackground(0, 0, 0);
if(_currentAnalysis){
if(_currentAnalysis)
_currentAnalysis->draw();
if(_currentAnalysis->meshIsComplete){
fbo.begin();
ofClear(0,0,0);
glShadeModel(GL_SMOOTH);
camera.begin();
//this is a hack, I don't know how to colour the fbo with black pixels so I'm drawing a massive black rectangle in the background
/*
ofPushMatrix();
ofTranslate(0, 0,-500);
ofSetColor(0, 0, 0);
ofRect(-fbo.getWidth(), -fbo.getHeight(), fbo.getWidth()*3, fbo.getHeight()*3);
ofPopMatrix();
ofSetColor(255);
*/
//float xDiff= (fbo.getWidth()- 1.55*(_currentAnalysis->_mesh_size_multiplier * _vid_w))/2;
float xDiff= (fbo.getWidth()- 1.33333*(_currentAnalysis->_mesh_size_multiplier * _vid_w))/2;
float yDiff= (fbo.getHeight()-(_currentAnalysis->_mesh_size_multiplier * _vid_h))/2;
ofTranslate(xDiff,yDiff,-_currentAnalysis->zPlaneAverage );
//ofScale(1.33333,1.0,1.0);
//_currentAnalysis->aMesh.drawVertices(); // TODO: tom - why do you have the vertices drawing here?
_currentAnalysis->aMesh.drawFaces();
_currentAnalysis->aMesh.draw();
camera.end();
fbo.end();
ofPixels pixels;
fbo.readToPixels(pixels);
ofSaveImage(pixels,_currentAnalysis->meshFileName, OF_IMAGE_QUALITY_BEST);
//saving jpgs doesn't work - pngs only!
// PNG is fine - better for Final Cut anyway!
ofDisableBlendMode() ;
}
}
}
void RefractiveIndex::setup_camera()
{
stop_camera();
// THIS IS LOADED IN FROM THE XML FILE SETTINGS
_vidGrabber.setDeviceID(_vid_id);
_vidGrabber.listDevices();
if(!_vidGrabber.initGrabber(_vid_w, _vid_h)) {
ofLog(OF_LOG_ERROR) << "RefractiveIndex::setup_camera - could not initialise grabber";
return;
}
_vidGrabber.setVerbose(true);
_vidGrabber.setUseTexture(false);
_vidGrabber.listDevices();
_vidGrabber.setVerbose(true);
_vid_stream_open = true;
cout << "CAMERA SETUP " << endl;
return;
_vidGrabber.setDeviceID(_vid_id);
}
@ -304,167 +229,9 @@ void RefractiveIndex::keyPressed (int key)
{
if( key =='f')
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;
}
/*
TO DO: add a file dialog so we can save images to another hard drive...
e.g.: http://dev.openframeworks.cc/pipermail/of-dev-openframeworks.cc/2011-April/003125.html
>> case 's':
>> doSave ^= true;
>> doLoad = false;
>> if(doSave) {
>> ofFileDialogResult r = ofSystemLoadDialog("Select path to save to", true);
>> if(r.bSuccess) {
>> saveCounter = 0;
>> savePath = r.getPath();
>> ofDirectory::createDirectory(savePath + "/color/");
>> ofDirectory::createDirectory(savePath + "/depth/");
>> printf("SAVE %s %s\n", r.getPath().c_str(), r.getName().c_str());
>> } else {
>> doSave = false;
>> }
>>
>> }
>> break;
>>
>> case 'l':
>> doLoad ^= true;
>> doSave = false;
>> if(doLoad) {
>> ofFileDialogResult r = ofSystemLoadDialog("Select path to load from", true);
>> if(r.bSuccess) {
>> loadCounter = 0;
>> loadPath = r.getPath();
>> ofDirectory dir;
>> loadMaxFiles = MAX(dir.listDir(loadPath + "/color"), dir.listDir(loadPath + "/depth"));
>> printf("LOAD %i %s %s\n", loadMaxFiles, r.getPath().c_str(), r.getName().c_str());
>> } else {
>> doLoad = false;
>> }
>>
>> }
>> break;
*/
}
void RefractiveIndex::exit()
{
if(_currentAnalysis)
_analysisAdapator->stop();
stop_camera();
}

View File

@ -1,3 +1,8 @@
/*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#pragma once
@ -8,13 +13,6 @@
#include "AbstractAnalysis.h"
#include "AnalysisAdaptor.h"
#include "ofxOpenCv.h"
#include "ofxXmlSettings.h"
#include "ofxOpenCv.h"
#define MODE_DRAWING 0xEEFF
#define MODE_ANALYSING 0xFFEE
class RefractiveIndex : public ofBaseApp
{
@ -56,15 +54,10 @@ protected:
AnalysisAdaptor* _analysisAdapator;
vector<AbstractAnalysis*> _analysisVector;
float _meshRotation;
float camDist;
ofEasyCam cam;
public:
// acquisition
static ofPixels _pixels;
static ofVideoGrabber _vidGrabber;
static int _mode;
vector<string> videoSourceList;
static int _vid_w, _vid_h, _vid_id;
static bool _vid_stream_open;
@ -72,9 +65,6 @@ public:
// this should be in xml
static string _location;
static ofxXmlSettings XML; // made this static so we can access RUN_NUM in the analyses
ofCamera camera;
ofPixels keepOnScreen;
ofFbo fbo;
};

View File

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

View File

@ -1,81 +1,54 @@
/* */
/*
- 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 "RefractiveIndex.h"
#include "ofxFileHelper.h"
#include "ofSystemUtils.h"
void AbstractAnalysis::setup(int camWidth, int camHeight) {
meshIsComplete=false;
imageForContourAvailable=false;
_cam_w = camWidth; _cam_h = camHeight;
if(RefractiveIndex::_mode == MODE_DRAWING) {
ofFileDialogResult r = ofSystemLoadDialog("choooose da folda", true);
if(!r.bSuccess) {
ofSystemAlertDialog("OOOOPS.... ERROR...");
return;
}
_whole_file_path_analysis = r.filePath;
_whole_file_path_synthesis = r.filePath + "/darwings";
}
}
vector<ofMesh> AbstractAnalysis::meshes;
// this is the main threaded loop for a given analysis
void AbstractAnalysis::do_synthesize() {
switch(RefractiveIndex::_mode)
{
case MODE_ANALYSING:
{
for(int i = 0; i < NUM_RUN; i++) {
cout << "NUM_RUN: " << i << endl;
_saved_filenames_analysis.clear();
_saved_filenames_synthesis.clear();
_state = STATE_ACQUIRING;
acquire();
if(_state == STATE_STOP) goto exit;
_state = STATE_SYNTHESISING;
synthesise();
if(_state == STATE_STOP) goto exit;
_state = STATE_DISPLAY_RESULTS;
displayresults();
cleanup();
}
}
case MODE_DRAWING:
{
ofxFileHelper fileHelperDrawing;
if(!fileHelperDrawing.doesDirectoryExist(_whole_file_path_synthesis)){
fileHelperDrawing.makeDirectory(_whole_file_path_synthesis);
}
read_dir_create_list(_whole_file_path_analysis);
_state = STATE_SYNTHESISING;
synthesise();
if(_state == STATE_STOP) goto exit;
_state = STATE_DISPLAY_RESULTS;
displayresults();
cleanup();
}
}
exit:
cleanup();
display_results();
ofNotifyEvent(_synthesize_cb, _name);
}
void AbstractAnalysis::create_dir_allocate_images()
void AbstractAnalysis::create_dir()
{
// HERE IS WHERE WE SETUP THE DIRECTORY FOR ALL THE SAVED IMAGES
@ -98,160 +71,266 @@ void AbstractAnalysis::create_dir_allocate_images()
}
}
ofxFileHelper fileHelperAnalysis;
ofxFileHelper fileHelperSynthesis;
_whole_file_path_analysis = ANALYSIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime ;
//cout << "_whole_file_path_analysis = " << _whole_file_path_analysis << endl;
if(!fileHelperAnalysis.doesDirectoryExist(_whole_file_path_analysis)){
if(!fileHelperAnalysis.doesDirectoryExist(ANALYSIS_PATH))
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH);
if(!fileHelperAnalysis.doesDirectoryExist(ANALYSIS_PATH+RefractiveIndex::_location))
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location);
if(!fileHelperAnalysis.doesDirectoryExist(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name))
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name);
if(!fileHelperAnalysis.doesDirectoryExist(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime))
fileHelperAnalysis.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime);
ofxFileHelper fileHelper;
_whole_file_path = ofToDataPath("") + ANALYSIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime ;
cout << "_whole_file_path = " << _whole_file_path << endl;
if(!fileHelper.doesDirectoryExist(_whole_file_path)){
fileHelper.makeDirectory(ANALYSIS_PATH);
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location);
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name);
fileHelper.makeDirectory(ANALYSIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime);
}
_whole_file_path_synthesis = SYNTHESIS_PATH + RefractiveIndex::_location + "/" + _name + "/"+replaceTime;
if(!fileHelperSynthesis.doesDirectoryExist(_whole_file_path_synthesis)){
if(!fileHelperAnalysis.doesDirectoryExist(SYNTHESIS_PATH))
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH);
if(!fileHelperAnalysis.doesDirectoryExist(SYNTHESIS_PATH+RefractiveIndex::_location))
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location);
if(!fileHelperAnalysis.doesDirectoryExist(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name))
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name);
if(!fileHelperAnalysis.doesDirectoryExist(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime))
fileHelperSynthesis.makeDirectory(SYNTHESIS_PATH+RefractiveIndex::_location+"/"+_name+"/"+replaceTime);
}
//////////////////////////////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();
//////////////////////////////ALLOCATE IMAGES //////////////////////////////////////////////////
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];
myColorImage1.clear();
myColorImage1.setUseTexture(false);
myColorImage1.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
//where are we in the image pixel array
int x=0;
int y=0;
myColorImage2.clear();
myColorImage2.setUseTexture(false);
myColorImage2.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
//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);
myGrayImage1.clear();
myGrayImage1.setUseTexture(false);
myGrayImage1.allocate(RefractiveIndex::_vid_w, RefractiveIndex::_vid_h, OF_IMAGE_GRAYSCALE);
//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++;
//////////////////////////////END ALLOCATE IMAGES //////////////////////////////////////////////////
}
}
difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return difference;
}
bool cmp_file(string f0, string f1)
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)
{
int v0 = atoi(f0.substr(0, f0.find("_")).c_str());
int v1 = atoi(f1.substr(0, f1.find("_")).c_str());
return v0 < v1;
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::read_dir_create_list(string folder_path)
void AbstractAnalysis::saveimage(string filename)
{
File dir(folder_path);
if(RefractiveIndex::_pixels.isAllocated()) {
if(dir.exists() && dir.isDirectory()) {
vector<string> list;
dir.list(list);
string fname = _whole_file_path + "/" + filename;
std::sort(list.begin(), list.end(), cmp_file);
cout << "saving - " << fname << endl;
for(int i = 0; i < list.size(); i++) {
string filepath = folder_path + "/" + list[i];
_saved_filenames_analysis.push_back(filepath);
ofSaveImage(RefractiveIndex::_pixels, fname, OF_IMAGE_QUALITY_BEST);
cout << list[i] << endl;
_saved_filenames.push_back(fname);
}
}
}
void AbstractAnalysis::saveImageAnalysis(string filename)
{
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;
ofLog(OF_LOG_ERROR) << "RefractiveIndex::_pixels NOT allocated...";
}
}
#ifdef TARGET_OSX
ofSaveImage(RefractiveIndex::_pixels, _whole_file_path_analysis+"/"+filename, OF_IMAGE_QUALITY_BEST);
#elif defined(TARGET_WIN32)
//<---- NEW SAVING - seems to fix WINDOWS saving out BLACK FRAMES PROBLEM ---->
unsigned char * somePixels;
ofPixels appPix = RefractiveIndex::_pixels;
somePixels = appPix.getPixels();
myColorImage1.setUseTexture(false);
myColorImage1.setFromPixels(somePixels,appPix.getWidth(),appPix.getHeight(), OF_IMAGE_COLOR);
myColorImage1.saveImage(ofToDataPath("")+ _whole_file_path_analysis+"/"+filename);
myColorImage1.clear();
#endif
_saved_filenames_analysis.push_back(_whole_file_path_analysis+"/"+filename);
}
void AbstractAnalysis::saveImageSynthesis(string filename, ofxCvImage* newPixels, ofImageType newType)
{
#ifdef TARGET_OSX
ofSaveImage(newPixels->getPixelsRef(), _whole_file_path_synthesis+"/"+filename, OF_IMAGE_QUALITY_BEST);
#elif defined(TARGET_WIN32)
if (newType == OF_IMAGE_COLOR){
myColorImage2.setUseTexture(false);
myColorImage2.setFromPixels(newPixels->getPixels(), newPixels->getWidth(), newPixels->getHeight(), OF_IMAGE_COLOR);
myColorImage2.saveImage(_whole_file_path_synthesis+"/"+filename);
}
if (newType == OF_IMAGE_GRAYSCALE){
myGrayImage1.setUseTexture(false);
// THIS IS HOW YOU HAVE TO SAVE OUT THE GREYSCALE IMAGES on WINDOWS FOR SOME REASON --> i.e.: as an OF_IMAGE_COLOR
// But they don't save properly - they're spatially translated and generally f'd up
myGrayImage1.setFromPixels(newPixels->getPixels(), newPixels->getWidth(), newPixels->getHeight(), OF_IMAGE_COLOR);
myGrayImage1.setImageType(OF_IMAGE_COLOR);
myGrayImage1.saveImage(_whole_file_path_synthesis+"/"+filename);
//myGrayImage1.clear();
}
#endif
_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+filename);
}

View File

@ -7,17 +7,21 @@
#include "ofMain.h"
#include "ofEvents.h"
#include "ofxOpenCv.h"
#include <string>
#define ANALYSIS_PATH "analysis/"
#define SYNTHESIS_PATH "synthesis/"
#define STATE_ACQUIRING 0x1111
#define STATE_SYNTHESISING 0x2222
#define STATE_DISPLAY_RESULTS 0x3333
#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 {
public:
@ -25,22 +29,17 @@ public:
virtual ~AbstractAnalysis(){;}
// generic function to set up the camera
virtual void setup(int camWidth, int camHeight);
virtual void setup(int camWidth, int camHeight){_cam_w = camWidth; _cam_h = camHeight;}
// this is the main threaded loop for a given analysis
void do_synthesize();
// show the results to the screen
// ofx
virtual void draw() = 0;
protected:
virtual void create_dir_allocate_images();
virtual void read_dir_create_list(string folder_path);
virtual void saveImageAnalysis(string filename);
virtual void saveImageSynthesis(string filename, ofxCvImage* newPixels, ofImageType newType);
virtual void create_dir();
virtual void saveimage(string filename);
// acquire images - all the children (see - do_synthesize)
virtual void acquire() = 0;
@ -48,55 +47,51 @@ protected:
// analysis + synthesize images - all the children (see - do_synthesize)
virtual void synthesise() = 0;
// display the results from disk
virtual void displayresults() = 0;
//added tom s 19/2 function runs a call back exactly like acquire.
virtual void display_results() = 0;
// display the results from disk
virtual void cleanup() {;}
//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:
string _name;
string _draw_directory;
// event
ofEvent<string> _synthesize_cb;
bool meshIsComplete;
bool imageForContourAvailable;
ofMesh aMesh;
ofLight light;
ofLight lightStatic;
string meshFileName;
//difference between our image size and the size of the fbo
float widthScaleFactor;
float heightScaleFactor;
ofImage contourImage;
ofPixels meshPix;
ofPixels publicColorImage;
float zPlaneAverage;
//added Tom 1/5/12 defines the stretch we make to the mesh to make it fit HD proportions
float _mesh_size_multiplier;
protected:
int _cam_w, _cam_h;
string _whole_file_path_analysis, _whole_file_path_synthesis;
vector<string> _saved_filenames_analysis;
vector<string> _saved_filenames_synthesis;
string _whole_file_path;
vector<string> _saved_filenames;
int _state;
ofImage myColorImage1;
ofImage myColorImage2;
ofImage myGrayImage1;
//int _run_cnt;
float DELTA_T_SAVE;
int NUM_PHASE;
int NUM_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;
};

View File

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

File diff suppressed because it is too large Load Diff

View File

@ -9,9 +9,6 @@
#include "Poco/Timer.h"
#include "ofxOpenCv.h"
class CamNoiseAnalysis : public AbstractAnalysis
{
public:
@ -23,67 +20,17 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
bool _RUN_DONE;
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;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//depth map function
vector<float> _returnDepthsAtEachPixel(ofImage &image1, vector<float> averageDepths, ofImage &backgroundImage);
//returns a vector of floats for each distinct light level
void _returnAveragePixelValues();
//this vector of vectors will be returned
vector<vector<float> > averagePixelValuesForAllLevels;
vector<float> _listOfLightLevels;
int vertexSubsampling;
int chooseColour;
ofPrimitiveMode meshMode;
ofBlendMode blendMode;
float multiplier;
bool _gotFirstImage;
ofImage _background;
int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
};

View File

@ -1,3 +1,35 @@
/*
- 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 "ofMain.h"
@ -9,323 +41,136 @@ using Poco::Timer;
using Poco::TimerCallback;
using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
void ColorMultiAnalysis::setup(int camWidth, int camHeight)
{
AbstractAnalysis::setup(camWidth, camHeight);
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_colormulti", NUMBER_RUNS);
cout << "NUM_RUN ColorMultiAnalysis " << NUM_RUN << endl;
//NUM_RUN = 5;
//flag for main sketch
meshIsComplete=false;
_gotFirstImage=false;
_mesh_size_multiplier=8;
vertexSubsampling = 1;
chooseColour=1;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_colormulti", ACQUIRE_TIME);
cout << "ACQUIRE_TIME ColorMultiAnalysis " << acq_run_time << endl;
//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_allocate_images();
DELTA_T_SAVE = 100;//150; // the right number is about 300
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;//;
create_dir();
_frame_cnt = 0;
_run_cnt = 0;
_synth_save_cnt = 0;
_fade_cnt=0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
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;
image1.clear();
image2.clear();
image3.clear();
image4.clear();
image5.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
// images used for re-loading and saving out the synthesized files ///
image3.setUseTexture(false);
image4.setUseTexture(false);
image5.setUseTexture(false);
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image3.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image4.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image5.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
// clear() apparently fixes the "OF_WARNING: in allocate, reallocating a ofxCvImage"
// that we're getting in OSX/Windows and is maybe crashing Windows
// http://forum.openframeworks.cc/index.php?topic=1867.0
cvColorImage1.clear();
cvGrayImage1.clear();
cvGrayDiff1.clear();
cvColorImage2.clear();
cvGrayImage2.clear();
cvGrayDiff2.clear();
cvConvertorImage.clear();
cvColorImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayDiff1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvColorImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvConvertorImage.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
}
void ColorMultiAnalysis::acquire()
{
Timer* save_timer;
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_allocate_images();
// 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->start(save_callback);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP)
while(!_RUN_DONE)
Thread::sleep(3);
save_timer->stop();
}
//}
}
void ColorMultiAnalysis::synthesise()
{
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
//cout << "ColorMultiAnalysis::saving synthesis...\n";
if(_state == STATE_STOP) return;
int index=0;
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
//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;
//hack to limit number of meshes.
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
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);
//cvColorImage1.blur(1);
//cvColorImage1.erode();
//cvColorImage1.dilate();
//cvColorImage1.dilate();
//cvColorImage1.dilate();
//cvColorImage1.dilate();
//cvFloatImage1 = cvColorImage1;
//cvGrayImage1 = cvColorImage1;
cvSmooth( cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), CV_GAUSSIAN, 15, 15);
cvXorS( cvColorImage1.getCvImage(), cvScalarAll(2), cvColorImage1.getCvImage(), 0 );
//cvCanny(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 100, 100, 3);
//cvLaplace(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0);
//cvGrayImage1 = cvCreateImage(cvSize(image1.width, image1.height),IPL_DEPTH_16S,1);
//cvSobel(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0, 1, 3);
// convert the CV image
image1.setFromPixels(cvColorImage1.getPixelsRef());
///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES //////////////////////////
if(!_gotFirstImage){
cout<<"background image is"<< _saved_filenames_analysis[i]<<endl;
_background=image1;
_gotFirstImage=true;
}
//clear vector so we don't add to it on successive runs
meshes.clear();
//subtract background begin///////////////
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofPixels imagePixels1 = image1.getPixelsRef();
//ofPixels imagePixels2 = image5.getPixelsRef();
ofPixels backgroundPixels = _background.getPixelsRef();
ofImage image1;
ofImage image2;
for(int i=0;i<imagePixels1.size();i++){
//unsigned char val=imagePixels1[i];
// cout<<(int)backgroundPixels[i]<< " thesePixels[i] "<<(int)imagePixels1[i]<<endl;
if(imagePixels1[i]-backgroundPixels[i]>0){
imagePixels1[i]-=backgroundPixels[i];
//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[0]) && image2.loadImage(_saved_filenames[i+1])){
cout<<"setting mesh"<<endl;
meshes.push_back(ofMesh());
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_HUE), image2, &meshes[index]);
index++;
}
else{
imagePixels1[i]=0;
}
}
//update the images with their new background subtracted selves
image1.setFromPixels(imagePixels1);
void ColorMultiAnalysis::display_results(){
//flag the main app that we aren't read yet
meshIsComplete=false;
Timer* display_results_timer;
//make a mesh - this mesh will be drawn in the main app
setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh);
TimerCallback<ColorMultiAnalysis> display_results_callback(*this, &ColorMultiAnalysis::display_results_cb);
// display results of the synthesis
//setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh);
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;
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
//string file_name;
//with jpgs this was refusing to save out
meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_ColorMultiAnalysis_"+ofToString(_run_cnt,2)+".png";
_saved_filenames_synthesis.push_back(meshFileName);
//file_name = ofToString(_synth_save_cnt, 2)+"_ColorMultiAnalysis_"+ofToString(_run_cnt,2)+".jpg";
//flag that we are finished
meshIsComplete=true;
_synth_save_cnt++;
// }
}
}
// TOM'S fix of why the last file gets overwritten again and again - but seems to prevent the files being written to the screen.
/*
_RUN_DONE = true;
*/
meshIsComplete=false;
_synth_save_cnt=0;
// _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
while(!_RUN_DONE)
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()
{
ofEnableSmoothing();
ofEnableLighting();
ofEnableSeparateSpecularLight();
light.enable();
light.setPosition(200,200,-150);
lightStatic.enable();
glEnable(GL_DEPTH_TEST);
ofSetLineWidth(2.0f);
//glPointSize(4.0f);
ofEnableBlendMode ( OF_BLENDMODE_ADD );
//ofEnableBlendMode ( OF_BLENDMODE_MULTIPLY );
//ofEnableBlendMode ( OF_BLENDMODE_SUBTRACT );
//ofEnableBlendMode ( OF_BLENDMODE_ALPHA );
//ofEnableBlendMode ( OF_BLENDMODE_SCREEN );
switch (_state) {
case STATE_ACQUIRING:
{
ofEnableAlphaBlending();
if (_frame_cnt < _frame_cnt_max)
{
int _fade_in_frames = _frame_cnt_max/50;
ofColor aColor;
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));
ofSetColor(aColor);
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){
ofColor aColor;
aColor.setHsb(c, 255, 255);
ofSetColor(aColor);
@ -335,26 +180,28 @@ void ColorMultiAnalysis::draw()
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) {
aColor.set(c, c, c, 255-int(ofMap(_frame_cnt-(_frame_cnt_max-_fade_in_frames), 0, _fade_in_frames, 0, 255)));
ofColor aColor;
//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)));
aColor.setHsb(c, 255-ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255), 255-ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255));
ofSetColor(aColor);
ofRect(0, 0, ofGetWidth(), ofGetHeight());
//cout << "FADING OUT..." << endl;
_fade_cnt++;
cout << "FADING OUT..." << endl;
}
} else {
//_state = STATE_SYNTHESISING;
_RUN_DONE = true;
}
_frame_cnt++;
ofDisableAlphaBlending();
break;
}
@ -362,115 +209,31 @@ void ColorMultiAnalysis::draw()
{
// 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;
}
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 );
//cout << "STATE_DISPLAY_RESULTS...\n" << endl;
meshes[whichMesh].drawVertices();
ofPopMatrix();
whichMesh+=speed;
if (_frame_cnt > 2)
{
_image_shown = true;
_frame_cnt=0;
if(whichMesh>meshes.size() -1 || whichMesh<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_COLOR);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break;
}
@ -484,157 +247,20 @@ void ColorMultiAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE
void ColorMultiAnalysis::save_cb(Timer& timer)
{
_save_cnt++;
// cout << "COLORMULTIANALYSIS::saving...\n";
string file_name = ofToString(_save_cnt,2)+"_"+ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveImageAnalysis(file_name);
saveimage(file_name);
_save_cnt++;
}
void ColorMultiAnalysis::setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh){
int x=0;
int y=0;
//get rid of all previous vectors and colours
mesh.clear();
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
//mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
//mesh.setMode(OF_PRIMITIVE_TRIANGLE_FAN);
//mesh.setMode(OF_PRIMITIVE_LINES);
//mesh.setMode(OF_PRIMITIVE_LINE_STRIP);
//mesh.setMode(OF_PRIMITIVE_LINE_LOOP);
//mesh.setMode(OF_PRIMITIVE_POINTS);
/*
OF_PRIMITIVE_TRIANGLES,
OF_PRIMITIVE_TRIANGLE_STRIP,
OF_PRIMITIVE_TRIANGLE_FAN,
OF_PRIMITIVE_LINES,
OF_PRIMITIVE_LINE_STRIP,
OF_PRIMITIVE_LINE_LOOP,
OF_PRIMITIVE_POINTS
*/
ofColor meshColour=ofColor(255,255,255);
//the average z position of the matrix - used later to centre the mesh on the z axis when drawing
float zPlaneAverage=0;
for(int i=0;i<sPixels.size();i++){
zPlaneAverage+=sPixels[i];
}
if (sPixels.size()!=0) {
zPlaneAverage/=sPixels.size();
//cout<<zPlaneAverage<<" zPlaneAverage "<<endl;
}
else{
cout<<"DEPTH FLOAT ARRAY IS EMPTY";
}
if(chooseColour==1){
for(int i=0;i<sPixels.size();i++){
mesh.addColor( currentSecondImage.getColor(x, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*(y+1),- sPixels[ (currentSecondImage.getWidth()*(y+1))+x ] ));
mesh.addColor( currentSecondImage.getColor(x, y));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1 ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1] ));
mesh.addColor( currentSecondImage.getColor(x, y));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x +1 ]));
x=x+vertexSubsampling;
if(x>=currentSecondImage.getWidth()-1){
x=0;
y=y+vertexSubsampling;
//something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector?
if(y>=currentSecondImage.getHeight()-1){
break;
}
}
}
void ColorMultiAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}
vector<float> ColorMultiAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){
ofPixels imagePixels1 = image1.getPixelsRef();
//ofPixels imagePixels2 = image2.getPixelsRef();
ofPixels backgroundPixels = backgroundImag.getPixelsRef();
vector<float> differences;
ofPixels difference;
//this unsigned char should be unnecessary - I would have thought - can't you just address the pixel locations in ofPixels directly?
unsigned char * thesePixels = new unsigned char[imagePixels1.getWidth()*imagePixels1.getHeight()*3];
for(int i=0;i<imagePixels1.size();i++){
thesePixels[i]=0;
}
int x=0;
int y=0;
int chooseComparison=1;
//comparison here to find out how close each color is to pure RED / GREEN / BLUE
if(chooseComparison==1){
//for each pixel...
//float _maxPossibleDistanceToCentre=ofDist(0,0,imagePixels1.getWidth()/2, imagePixels1.getHeight()/2);
for(int i=0;i<imagePixels1.size();i+=3){
ofColor imageColor1 = imagePixels1.getColor(x, y);
//ofColor colourImage2 = imagePixels2.getColor(x, y);
//float _distanceToCentre=ofDist(imagePixels1.getWidth()/2, imagePixels1.getHeight()/2, x, y);
//float _presumedBrightness=ofMap(sqrt(_maxPossibleDistanceToCentre)-sqrt(_distanceToCentre), 0, sqrt(_maxPossibleDistanceToCentre), 0, 255);
//int thisDiff=abs(imageColor1.getHue());
//int thisDiff=abs(imageColor1.getBrightness());
//int thisDiff=abs(imageColor1.getBrightness()-_presumedBrightness);
int thisDiff=-abs(imageColor1.getHue());
//int thisDiff=abs(imageColor1.getLightness());
//cout<<thisDiff<< " thisDiff "<<endl;
//red hue: 0
//green hue: 120
//blue hue: 240
float multiplier=15.0*((thisDiff)/255.0);
differences.push_back(multiplier* thisDiff);
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
if(x>=imagePixels1.getWidth()){
x=0;
y++;
}
}
}
//difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return differences;
}

View File

@ -4,9 +4,6 @@
#include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ColorMultiAnalysis : public AbstractAnalysis
{
public:
@ -18,58 +15,15 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
bool _RUN_DONE;
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;
//this is the temporary container to allow us to convert and save out greyscale images
ofxCvColorImage cvConvertorImage;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//depth map function
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
int vertexSubsampling;
int chooseColour;
bool _gotFirstImage;
ofImage _background;
int _run_cnt, _save_cnt, _fade_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
};

View File

@ -1,343 +1,143 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "ColorSingleAnalysis.h"
#include "ofMain.h"
#include "Poco/Timer.h"
#include "Poco/Thread.h"
#include <string>
#include <iostream>
#include "RefractiveIndex.h"
using Poco::Timer;
using Poco::TimerCallback;
using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
void ColorSingleAnalysis::setup(int camWidth, int camHeight)
{
AbstractAnalysis::setup(camWidth, camHeight);
DELTA_T_SAVE = 100;//300;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_colorsingle", NUMBER_RUNS);
cout << "NUM_RUN ColorSingleAnalysis " << NUM_RUN << endl;
//NUM_RUN = 5;
//flag for main sketch
meshIsComplete=false;
_gotFirstImage=false;
_mesh_size_multiplier=8;
vertexSubsampling = 1;
chooseColour=1;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_colorsingle", ACQUIRE_TIME);
cout << "ACQUIRE_TIME ColorSingleAnalysis " << acq_run_time << endl;
//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_allocate_images();
_run_cnt = 0;
create_dir();
_frame_cnt = 0;
_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
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
r = 0;
g = 0;
b = 0;
_fade_cnt=0;
fileNameTag = "";
fileNameColor = "";
_show_image = false;
_image_shown = false;
image1.clear();
image2.clear();
image3.clear();
image4.clear();
image5.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
// images used for re-loading and saving out the synthesized files ///
image3.setUseTexture(false);
image4.setUseTexture(false);
image5.setUseTexture(false);
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image3.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image4.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image5.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
// clear() apparently fixes the "OF_WARNING: in allocate, reallocating a ofxCvImage"
// that we're getting in OSX/Windows and is maybe crashing Windows
// http://forum.openframeworks.cc/index.php?topic=1867.0
cvColorImage1.clear();
cvGrayImage1.clear();
cvGrayDiff1.clear();
cvColorImage2.clear();
cvGrayImage2.clear();
cvGrayDiff2.clear();
cvConvertorImage.clear();
cvColorImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayDiff1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvColorImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvConvertorImage.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
}
void ColorSingleAnalysis::acquire()
{
Timer* save_timer;
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_allocate_images();
// RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) {
// _run_cnt = i;
//cout << "RUN NUM = " << i;
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);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP)
while(!_RUN_DONE)
Thread::sleep(3);
save_timer->stop();
}
}
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;
//cout << "ColorSingleAnalysis::saving synthesis...\n";
if(_state == STATE_STOP) return;
int index=0;
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
//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;
//hack to limit number of meshes.
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
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 //////////////////////////
cout << "_saved_filenames_analysis[i].find(RED): " << _saved_filenames_analysis[i].find("RED") << endl;
if(_saved_filenames_analysis[i].find("RED")<_saved_filenames_analysis[i].length())
{
fileNameColor = "RED";
cout<<"FOUND RED"<<endl;
} else if (_saved_filenames_analysis[i].find("GREEN")<_saved_filenames_analysis[i].length())
{
fileNameColor = "GREEN";
cout<<"FOUND GREEN"<<endl;
} else if(_saved_filenames_analysis[i].find("BLUE")<_saved_filenames_analysis[i].length())
{
fileNameColor = "BLUE";
cout<<"FOUND BLUE"<<endl;
} else if(_saved_filenames_analysis[i].find("FADING")<_saved_filenames_analysis[i].length())
{
fileNameColor = "FADING";
cout<<"FOUND FADING"<<endl;
}
//clear vector so we don't add to it on successive runs
meshes.clear();
cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height);
//cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height);
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
cvColorImage1.blur(1);
ofImage image1;
ofImage image2;
//cvSmooth( cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), CV_GAUSSIAN, 9, 9 );
//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());
cvColorImage1.erode();
cvSmooth( cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), CV_GAUSSIAN, 3, 3 );
//cvColorImage1.erode();
//cvColorImage1.dilate();
//cvColorImage1.dilate();
//cvColorImage1.dilate();
//cvColorImage1.dilate();
//cvFloatImage1 = cvColorImage1;
//cvGrayImage1 = cvColorImage1;
//cvXorS( cvColorImage1.getCvImage(), cvScalarAll(255), cvColorImage1.getCvImage(), 0 );
//cvCanny(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 100, 100, 3);
//cvLaplace(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0);
//cvGrayImage1 = cvCreateImage(cvSize(image1.width, image1.height),IPL_DEPTH_16S,1);
//cvSobel(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0, 1, 3);
// convert the CV image
image1.setFromPixels(cvColorImage1.getPixelsRef());
///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES //////////////////////////
if(!_gotFirstImage){
cout<<"background image is"<< _saved_filenames_analysis[i]<<endl;
_background=image1;
_gotFirstImage=true;
if(i<_saved_filenames.size()/3){
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_RED), image2, &meshes[index]);
}
//subtract background begin///////////////
ofPixels imagePixels1 = image1.getPixelsRef();
ofPixels imagePixels2 = image5.getPixelsRef();
ofPixels backgroundPixels = _background.getPixelsRef();
for(int i=0;i<imagePixels1.size();i++){
//unsigned char val=imagePixels1[i];
// cout<<(int)backgroundPixels[i]<< " thesePixels[i] "<<(int)imagePixels1[i]<<endl;
if(imagePixels1[i]-backgroundPixels[i]>0){
imagePixels1[i]-=backgroundPixels[i];
if(i>=_saved_filenames.size()/3 && i<2* _saved_filenames.size()/3){
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_GREEN), image2, &meshes[index]);
}
if(i>= 2* _saved_filenames.size()/3 && i<_saved_filenames.size()){
setMeshFromPixels( calculateListOfZValues(image1,image2, COMPARE_BLUE), image2, &meshes[index]);
}
index++;
}
else{
imagePixels1[i]=0;
}
}
//update the images with their new background subtracted selves
image1.setFromPixels(imagePixels1);
void ColorSingleAnalysis::display_results(){
//flag the main app that we aren't read yet
meshIsComplete=false;
Timer* display_results_timer;
//make a mesh - this mesh will be drawn in the main app
setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh);
TimerCallback<ColorSingleAnalysis> display_results_callback(*this, &ColorSingleAnalysis::display_results_cb);
// display results of the synthesis
//setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh);
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;
/////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER ////////////////////////////////
//string file_name;
//with jpgs this was refusing to save out
meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_ColorSingleAnalysis_"+ofToString(_run_cnt,2)+".png";
_saved_filenames_synthesis.push_back(meshFileName);
//file_name = ofToString(_synth_save_cnt, 2)+"_ColorSingleAnalysis_"+ofToString(_run_cnt,2)+".jpg";
//flag that we are finished
meshIsComplete=true;
_synth_save_cnt++;
// }
}
}
// TOM'S fix of why the last file gets overwritten again and again - but seems to prevent the files being written to the screen.
/*
_RUN_DONE = true;
*/
meshIsComplete=false;
_synth_save_cnt=0;
// _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
while(!_RUN_DONE)
Thread::sleep(3);
}
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;
}
}
display_results_timer->stop();
}
void ColorSingleAnalysis::draw()
{
ofEnableSmoothing();
ofEnableLighting();
ofEnableSeparateSpecularLight();
light.enable();
light.setPosition(200,200,-150);
lightStatic.enable();
glEnable(GL_DEPTH_TEST);
ofSetLineWidth(1.0f);
glPointSize(1.0f);
ofEnableBlendMode ( OF_BLENDMODE_ADD );
//ofEnableBlendMode ( OF_BLENDMODE_MULTIPLY );
//ofEnableBlendMode ( OF_BLENDMODE_SUBTRACT );
//ofEnableBlendMode ( OF_BLENDMODE_ALPHA );
//ofEnableBlendMode ( OF_BLENDMODE_SCREEN );
switch (_state) {
case STATE_ACQUIRING:
{
if (_frame_cnt < _frame_cnt_max)
{
@ -379,8 +179,8 @@ void ColorSingleAnalysis::draw()
if (_frame_cnt >= (_frame_cnt_max-_fade_in_frames) && _frame_cnt < _frame_cnt_max){
int fade = ofMap(_fade_cnt, 0, _fade_in_frames, 0, 255);
ofSetColor(0, 0, 255-fade);
ofSetColor(0, 0, 255-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;
ofRect(0, 0, ofGetWidth(), ofGetHeight());
_fade_cnt++;
@ -388,7 +188,6 @@ void ColorSingleAnalysis::draw()
}
} else {
//_state = STATE_SYNTHESISING;
_RUN_DONE = true;
}
@ -400,118 +199,32 @@ void ColorSingleAnalysis::draw()
case STATE_SYNTHESISING:
{
// 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;
}
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_COLOR);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
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;
if(whichMesh>meshes.size() -1 || whichMesh<0){
speed*=-1;
whichMesh+=speed;
}
break;
}
default:
@ -523,284 +236,22 @@ void ColorSingleAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE
void ColorSingleAnalysis::save_cb(Timer& timer)
{
_save_cnt++;
//cout << "ColorSingleAnalysis::saving...\n";
string file_name =ofToString(_save_cnt,2)+"_"+fileNameTag+"_"+ofToString(_run_cnt,2)+".jpg";
saveimage(file_name);
_save_cnt++;
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
saveImageAnalysis(file_name);
}
void ColorSingleAnalysis::setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh){
int x=0;
int y=0;
//get rid of all previous vectors and colours
mesh.clear();
//mesh.setMode(OF_PRIMITIVE_TRIANGLES);
//mesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
//mesh.setMode(OF_PRIMITIVE_TRIANGLE_FAN);
//mesh.setMode(OF_PRIMITIVE_LINES);
//mesh.setMode(OF_PRIMITIVE_LINE_STRIP);
//mesh.setMode(OF_PRIMITIVE_LINE_LOOP);
mesh.setMode(OF_PRIMITIVE_POINTS);
/*
OF_PRIMITIVE_TRIANGLES,
OF_PRIMITIVE_TRIANGLE_STRIP,
OF_PRIMITIVE_TRIANGLE_FAN,
OF_PRIMITIVE_LINES,
OF_PRIMITIVE_LINE_STRIP,
OF_PRIMITIVE_LINE_LOOP,
OF_PRIMITIVE_POINTS
*/
ofColor meshColour=ofColor(255,255,255, 255);
//the average z position of the matrix - used later to centre the mesh on the z axis when drawing
float zPlaneAverage=0;
for(int i=0;i<sPixels.size();i++){
zPlaneAverage+=sPixels[i];
}
if (sPixels.size()!=0) {
zPlaneAverage/=sPixels.size();
//cout<<zPlaneAverage<<" zPlaneAverage "<<endl;
}
else{
cout<<"DEPTH FLOAT ARRAY IS EMPTY";
}
if(chooseColour==1){
for(int i=0;i<sPixels.size();i++){
mesh.addColor( currentSecondImage.getColor(x, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*(y+1),- sPixels[ (currentSecondImage.getWidth()*(y+1))+x ] ));
mesh.addColor( currentSecondImage.getColor(x, y));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1 ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1] ));
mesh.addColor( currentSecondImage.getColor(x, y));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x +1 ]));
x=x+vertexSubsampling;
if(x>=currentSecondImage.getWidth()-1){
x=0;
y=y+vertexSubsampling;
//something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector?
if(y>=currentSecondImage.getHeight()-1){
break;
}
}
void ColorSingleAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}
if(chooseColour==2){
for(int i=0;i<sPixels.size();i++){
ofColor currentSecondImageColor = currentSecondImage.getColor(x, y+1);
mesh.addColor( currentSecondImageColor.getBrightness());
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*(y+1),- sPixels[ (currentSecondImage.getWidth()*(y+1))+x ] ));
currentSecondImageColor = currentSecondImage.getColor(x, y);
mesh.addColor( currentSecondImageColor.getBrightness());
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
currentSecondImageColor = currentSecondImage.getColor(x+1, y+1);
mesh.addColor( currentSecondImageColor.getBrightness());
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1 ] ));
currentSecondImageColor = currentSecondImage.getColor(x+1, y+1);
mesh.addColor( currentSecondImageColor.getBrightness());
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1] ));
currentSecondImageColor = currentSecondImage.getColor(x, y);
mesh.addColor( currentSecondImageColor.getBrightness());
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
currentSecondImageColor = currentSecondImage.getColor(x+1, y);
mesh.addColor( currentSecondImageColor.getBrightness());
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x +1 ]));
x=x+vertexSubsampling;
if(x>=currentSecondImage.getWidth()-1){
x=0;
y=y+vertexSubsampling;
//something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector?
if(y>=currentSecondImage.getHeight()-1){
break;
}
}
}
}
if(chooseColour==3){
ofColor currentSecondImageColor;
for(int i=0;i<sPixels.size();i++){
if(fileNameColor=="RED")
{
currentSecondImageColor.r=255;
currentSecondImageColor.g=0;
currentSecondImageColor.b=0;
currentSecondImageColor.a=255;
} else if(fileNameColor=="GREEN")
{
currentSecondImageColor.r=0;
currentSecondImageColor.g=255;
currentSecondImageColor.b=0;
currentSecondImageColor.a=255;
} else if(fileNameColor=="BLUE")
{
currentSecondImageColor.r=0;
currentSecondImageColor.g=0;
currentSecondImageColor.b=255;
currentSecondImageColor.a=255;
} else if(fileNameColor=="FADING")
{
currentSecondImageColor.r= 255;
currentSecondImageColor.g= 255;
currentSecondImageColor.b= 255;
currentSecondImageColor.a= 255;
}
mesh.addColor( currentSecondImageColor);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*(y+1),- sPixels[ (currentSecondImage.getWidth()*(y+1))+x ] ));
mesh.addColor( currentSecondImageColor);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImageColor);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1 ] ));
mesh.addColor( currentSecondImageColor);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1] ));
mesh.addColor( currentSecondImageColor);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImageColor);
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x +1 ]));
x=x+vertexSubsampling;
//x++;
if(x>=currentSecondImage.getWidth()-1){
x=0;
y=y+vertexSubsampling;
//y++;
//something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector?
if(y>=currentSecondImage.getHeight()-1){
break;
}
}
}
}
}
vector<float> ColorSingleAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){
ofPixels imagePixels1 = image1.getPixelsRef();
//ofPixels imagePixels2 = image2.getPixelsRef();
ofPixels backgroundPixels = backgroundImag.getPixelsRef();
vector<float> differences;
ofPixels difference;
//this unsigned char should be unnecessary - I would have thought - can't you just address the pixel locations in ofPixels directly?
unsigned char * thesePixels = new unsigned char[imagePixels1.getWidth()*imagePixels1.getHeight()*3];
for(int i=0;i<imagePixels1.size();i++){
thesePixels[i]=0;
}
int x=0;
int y=0;
int chooseComparison=1;
//comparison here to find out how close each color is to pure RED / GREEN / BLUE
if(chooseComparison==1){
//for each pixel...
for(int i=0;i<imagePixels1.size();i+=3){
ofColor imageColor1 = imagePixels1.getColor(x, y);
//ofColor colourImage2 = imagePixels2.getColor(x, y);
//int thisDiff=abs(imageColor1.getHue());
//int thisDiff=abs(imageColor1.getBrightness());
//int thisDiff=abs(imageColor1.getBrightness()-_presumedBrightness);
int thisDiff;
if(fileNameColor=="RED")
{
//thisDiff=abs(imageColor1.r);
thisDiff=abs(255-imageColor1.r);
} else if(fileNameColor=="GREEN")
{
//thisDiff=abs(imageColor1.g);
thisDiff=abs(255-imageColor1.g);
} else if(fileNameColor=="BLUE")
{
//thisDiff=abs(imageColor1.b);
thisDiff=abs(255-imageColor1.b);
} else if(fileNameColor=="FADING") {
//thisDiff=imageColor1.getBrightness();
thisDiff=255-imageColor1.getBrightness();
}
//cout<<thisDiff<< " thisDiff "<<endl;
//red hue: 0
//green hue: 120
//blue hue: 240
float multiplier=8.0;
differences.push_back(multiplier* thisDiff);
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
if(x>=imagePixels1.getWidth()){
x=0;
y++;
}
}
}
//difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return differences;
}

View File

@ -9,9 +9,6 @@
#include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ColorSingleAnalysis : public AbstractAnalysis
{
public:
@ -23,62 +20,16 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
bool _RUN_DONE;
string fileNameTag;
string fileNameColor;
float r,g,b;
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;
ofxCvFloatImage cvFloatImage1;
ofxCvContourFinder cvContourFinder1;
//this is the temporary container to allow us to convert and save out greyscale images
ofxCvColorImage cvConvertorImage;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//depth map function
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
int vertexSubsampling;
int chooseColour;
bool _gotFirstImage;
ofImage _background;
int _run_cnt, _save_cnt, _fade_cnt;
float r,g,b, _frame_cnt, _frame_cnt_max , _results_cnt, _results_cnt_max;
};

File diff suppressed because it is too large Load Diff

View File

@ -9,9 +9,6 @@
#include "Poco/Timer.h"
#include "ofxOpenCv.h"
class DiffNoiseAnalysis : public AbstractAnalysis
{
public:
@ -23,60 +20,16 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
bool _RUN_DONE;
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;
//this is the temporary container to allow us to convert and save out greyscale images
ofxCvColorImage cvConvertorImage;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//depth map function
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
int vertexSubsampling;
int chooseColour;
ofPrimitiveMode meshMode;
ofBlendMode blendMode;
float multiplier;
bool _gotFirstImage;
ofImage _background;
int _run_cnt, _save_cnt, _fade_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
};

View File

@ -1,3 +1,8 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#include "IResponseAnalysis.h"
#include "ofMain.h"
@ -10,237 +15,114 @@ using Poco::Timer;
using Poco::TimerCallback;
using Poco::Thread;
#define NUMBER_RUNS 1
#define ACQUIRE_TIME 20
void IResponseAnalysis::setup(int camWidth, int camHeight)
{
AbstractAnalysis::setup(camWidth, camHeight);
DELTA_T_SAVE = 100;
NUM_PHASE = 1;
NUM_RUN = 1;
NUM_SAVE_PER_RUN = 100;
NUM_RUN = RefractiveIndex::XML.getValue("config:analysis_NUM_RUN:NUM_RUN_iresponse", NUMBER_RUNS);
cout << "NUM_RUN IResponseAnalysis " << NUM_RUN << endl;
//NUM_RUN = 5;
//flag for main sketch
meshIsComplete=false;
_gotFirstImage=false;
_mesh_size_multiplier=4;
int acq_run_time; // 10 seconds of acquiring per run
acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_iresponse", ACQUIRE_TIME);
cout << "ACQUIRE_TIME IResponseAnalysis " << acq_run_time << endl;
//flag for main sketch
meshIsComplete=false;
_gotFirstImage=false;
_mesh_size_multiplier=8;
//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_allocate_images();
_synth_save_cnt = 0;
_run_cnt = 0;
create_dir();
_frame_cnt = 0;
_frame_cnt_max = ofGetFrameRate() * ((DELTA_T_SAVE * NUM_SAVE_PER_RUN) / 1000);
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;
image1.clear();
image2.clear();
image3.clear();
image4.clear();
image5.clear();
// images use for drawing the synthesized files to the screen ///
image1.setUseTexture(false); // the non texture image that is needed to first load the image
image2.setUseTexture(true); // the image that needs to get written to the screen which takes the content of image1
// images used for re-loading and saving out the synthesized files ///
image3.setUseTexture(false);
image4.setUseTexture(false);
image5.setUseTexture(false);
image1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image3.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image4.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
image5.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h, OF_IMAGE_COLOR);
//cout << "RefractiveIndex::_vid_w " << RefractiveIndex::_vid_w << endl;
//cout << "RefractiveIndex::_vid_h " << RefractiveIndex::_vid_h << endl;
// clear() apparently fixes the "OF_WARNING: in allocate, reallocating a ofxCvImage"
// that we're getting in OSX/Windows and is maybe crashing Windows
// http://forum.openframeworks.cc/index.php?topic=1867.0
cvColorImage1.clear();
cvGrayImage1.clear();
cvGrayDiff1.clear();
cvColorImage2.clear();
cvGrayImage2.clear();
cvGrayDiff2.clear();
cvColorImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayDiff1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvColorImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h);
}
void IResponseAnalysis::acquire()
{
Timer* save_timer;
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_allocate_images();
// RUN ROUTINE
//for(int i = 0; i < NUM_RUN; i++) {
//_run_cnt = i;
//cout << "RUN NUM = " << i;
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);
_RUN_DONE = false;
_frame_cnt = 0; _save_cnt = 0;
while(!_RUN_DONE && _state != STATE_STOP)
while(!_RUN_DONE)
Thread::sleep(3);
save_timer->stop();
//}
}
}
void IResponseAnalysis::synthesise()
{
cout<<"SYNTHESISING IRESPONSE";
//incrementer to whichMesh
speed=0.2;
//whichMesh is the index in the vector of meshes
whichMesh=0;
//cout << "IResponseAnalysis::saving synthesis...\n";
if(_state == STATE_STOP) return;
int index=0;
float iterator=1;
bool debug=false;
if(debug){
_saved_filenames.clear();
_saved_filenames=getListOfImageFilePaths("MIDDLESBOROUGH", _name);
for(float i=1;i<_saved_filenames_analysis.size()-1;i++){
//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;
//hack to limit number of meshes.
if(_saved_filenames.size()>100){
iterator= _saved_filenames.size() /100;
}
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 //////////////////////////
if(!_gotFirstImage){
cout<<"background image is"<< _saved_filenames_analysis[i]<<endl;
_background=image1;
_gotFirstImage=true;
}
//clear vector so we don't add to it on successive runs
meshes.clear();
//subtract background begin///////////////
for(float i=0;i<_saved_filenames.size()-1;i+=iterator){
ofPixels imagePixels1 = image1.getPixelsRef();
//ofPixels imagePixels2 = image5.getPixelsRef();
ofPixels backgroundPixels = _background.getPixelsRef();
//background subtraction//
/*
for(int i=0;i<imagePixels1.size();i++){
//unsigned char val=imagePixels1[i];
// cout<<(int)backgroundPixels[i]<< " thesePixels[i] "<<(int)imagePixels1[i]<<endl;
if(imagePixels1[i]-backgroundPixels[i]>0){
imagePixels1[i]-=backgroundPixels[i];
}
else{
imagePixels1[i]=0;
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++;
}
}
*/
//update the images with their new background subtracted selves
image1.setFromPixels(imagePixels1);
//flag the main app that we aren't read yet
meshIsComplete=false;
//make a mesh - this mesh will be drawn in the main app
setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh);
//meshPix=make3DZmap(image1, image5, _background);
//with jpgs this was refusing to save out
meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_IResponseSynthesis_"+ofToString(_run_cnt,2)+".png";
_saved_filenames_synthesis.push_back(meshFileName);
//flag that we are finished
meshIsComplete=true;
_synth_save_cnt++;
}
//}
}
// TOM'S fix of why the last file gets overwritten again and again - but seems to prevent the files being written to the screen.
/*
_RUN_DONE = true;
*/
void IResponseAnalysis::display_results(){
meshIsComplete=false;
_synth_save_cnt=0;
Timer* display_results_timer;
// _saved_filenames_synthesis has processed all the files in the analysis images folder
while(!_RUN_DONE && _state != STATE_STOP)
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);
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
void IResponseAnalysis::draw()
{
@ -249,18 +131,9 @@ void IResponseAnalysis::draw()
case STATE_ACQUIRING:
{
ofEnableAlphaBlending();
ofColor aColour;
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());
}
/// *** TODO *** ///
// still need to deal with latency frames here - i.e.: there are frames
/// *** TODO *** ///
if (_frame_cnt < _frame_cnt_max)
{
@ -268,18 +141,9 @@ void IResponseAnalysis::draw()
ofRect(0, 0, ofGetWidth(), ofGetHeight());
c = 255.0 * (_frame_cnt_max*_frame_cnt_max - _frame_cnt*_frame_cnt)/(_frame_cnt_max*_frame_cnt_max);
} else {
_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++;
break;
@ -287,120 +151,38 @@ void IResponseAnalysis::draw()
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...
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;
}
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_COLOR);
image2.draw(0,0, ofGetWidth(), ofGetHeight());
ofDisableAlphaBlending();
}
// display results of the synthesis
_RUN_DONE = true;
break;
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;
}
break;
}
default:
break;
}
@ -410,131 +192,21 @@ void IResponseAnalysis::draw()
// this runs at save_cb timer rate = DELTA_T_SAVE
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++;
string file_name = ofToString(_save_cnt,2)+"_"+ ofToString(c,2)+"_"+ofToString(_run_cnt,2)+".jpg";
saveImageAnalysis(file_name);
//if(_save_cnt >= NUM_SAVE_PER_RUN)
// _RUN_DONE = true;
}
void IResponseAnalysis::setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh){
int x=0;
int y=0;
//get rid of all previous vectors and colours
mesh.clear();
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
ofColor meshColour=ofColor(255,0,0);
int chooseColour=1 ;
//the average z position of the matrix - used later to centre the mesh on the z axis when drawing
float zPlaneAverage=0;
for(int i=0;i<sPixels.size();i++){
zPlaneAverage+=sPixels[i];
}
if (sPixels.size()!=0) {
zPlaneAverage/=sPixels.size();
//cout<<zPlaneAverage<<" zPlaneAverage "<<endl;
}
else{
cout<<"DEPTH FLOAT ARRAY IS EMPTY";
}
if(chooseColour==1){
for(int i=0;i<sPixels.size();i++){
mesh.addColor( currentSecondImage.getColor(x, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*(y+1),- sPixels[ (currentSecondImage.getWidth()*(y+1))+x ] ));
mesh.addColor( currentSecondImage.getColor(x, y));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1 ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y+1));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*(y+1),- sPixels[(currentSecondImage.getWidth()*(y+1))+x+1] ));
mesh.addColor( currentSecondImage.getColor(x, y));
mesh.addVertex(ofVec3f(_mesh_size_multiplier*x,_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x ] ));
mesh.addColor( currentSecondImage.getColor(x+1, y) );
mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x+1),_mesh_size_multiplier*y,- sPixels[(currentSecondImage.getWidth()*(y))+x +1 ]));
x++;
if(x>=currentSecondImage.getWidth()-1){
x=0;
y++;
//something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector?
if(y>=currentSecondImage.getHeight()-1){
break;
void IResponseAnalysis::display_results_cb(Timer& timer){
_results_cnt++;
if (_results_cnt>_results_cnt_max) {
_RUN_DONE=true;
}
}
}
}
}
vector<float> IResponseAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){
ofPixels imagePixels1 = image1.getPixelsRef();
//ofPixels imagePixels2 = image2.getPixelsRef();
ofPixels backgroundPixels = backgroundImag.getPixelsRef();
vector<float> differences;
ofPixels difference;
//this unsigned char should be unnecessary - I would have thought - can't you just address the pixel locations in ofPixels directly?
unsigned char * thesePixels = new unsigned char[imagePixels1.getWidth()*imagePixels1.getHeight()*3];
for(int i=0;i<imagePixels1.size();i++){
thesePixels[i]=0;
}
int x=0;
int y=0;
int chooseComparison=1;
//the current version compares how bright this pixel is with how bright it would be following inverse square fall off from centre
if(chooseComparison==1){
//for each pixel...
float _maxPossibleDistanceToCentre=ofDist(0,0,imagePixels1.getWidth()/2, imagePixels1.getHeight()/2);
for(int i=0;i<imagePixels1.size();i+=3){
ofColor imageColor1 = imagePixels1.getColor(x, y);
//ofColor colourImage2 = imagePixels2.getColor(x, y);
float _distanceToCentre=ofDist(imagePixels1.getWidth()/2, imagePixels1.getHeight()/2, x, y);
float _presumedBrightness=ofMap(sqrt(_maxPossibleDistanceToCentre)-sqrt(_distanceToCentre), 0, sqrt(_maxPossibleDistanceToCentre), 0, 255);
//float _presumedBrightness=255;
int thisDiff=abs(imageColor1.getBrightness()-_presumedBrightness);
//cout<<thisDiff<< " thisDiff "<<endl;
float multiplier=4.0;
differences.push_back(multiplier * thisDiff);
thesePixels[i]=thisDiff;
thesePixels[i+1]=thisDiff;
thesePixels[i+2]=thisDiff;
x++;
if(x>=imagePixels1.getWidth()){
x=0;
y++;
}
}
}
difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3);
return differences;
}

View File

@ -9,8 +9,6 @@
#include "Poco/Timer.h"
#include "ofxOpenCv.h"
class IResponseAnalysis : public AbstractAnalysis
{
public:
@ -22,48 +20,17 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
bool _RUN_DONE;
int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_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;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//returns ofPixels but these pixels actually hold depth data.
//ofPixels make3DZmap(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
bool _gotFirstImage;
ofImage _background;
};

File diff suppressed because it is too large Load Diff

View File

@ -1,3 +1,7 @@
/*
~ author: dviid
~ contact: dviid@labs.ciid.dk
*/
#pragma once
@ -5,11 +9,6 @@
#include "Poco/Timer.h"
#include "rfiCvContourFinder.h"
#include "ofxOpenCv.h"
class RelaxRateAnalysis : public AbstractAnalysis
{
@ -22,69 +21,17 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void cleanup();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
void clearcfindervect();
void clearcfindervectdisplay();
bool _RUN_DONE;
float _flip, _level;
int _run_cnt, _save_cnt, _synth_save_cnt, _anim_cnt;
float c, _frame_cnt, _frame_cnt_max, _anim_cnt_max;
int _treshold;
int _maxblobs;
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;
vector<rfiCvContourFinder*> cvContourFinderVect;
vector<rfiCvContourFinder*> cvContourFinderVectDisplay;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//depth map function
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
int vertexSubsampling_x;
int vertexSubsampling_y;
int chooseColour;
ofPrimitiveMode meshMode;
ofBlendMode blendMode;
float multiplier;
bool _gotFirstImage;
ofImage _background;
int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
};

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,34 @@
/* */
/*
- 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
@ -6,8 +36,6 @@
#include "Poco/Timer.h"
#include "ofxOpenCv.h"
enum shadow_type {
H, V, D,
};
@ -24,11 +52,11 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
@ -38,48 +66,7 @@ protected:
float _scanLineWidth; // pix per second
float _step;
shadow_type _dir;
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;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//depth map function
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
int vertexSubsampling;
int chooseColour;
ofPrimitiveMode meshMode;
ofBlendMode blendMode;
float multiplier;
bool _gotFirstImage;
ofImage _background;
int _run_cnt, _save_cnt;
float c, _frame_cnt, _frame_cnt_max, _results_cnt, _results_cnt_max;
};

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,6 @@
#include "Poco/Timer.h"
#include "ofxOpenCv.h"
class ShapeFromShadingAnalysis : public AbstractAnalysis
{
@ -23,18 +21,18 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
string quad;
string fileNameQuad;
bool _RUN_DONE;
float _flip, _level;
int _run_cnt, _save_cnt;
int _animation_cnt1;
int _animation_cnt2;
int _animation_cnt3;
@ -52,51 +50,6 @@ protected:
int _animation_cnt15;
int _animation_cnt16;
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;
//this is the temporary container to allow us to convert and save out greyscale images
ofxCvColorImage cvConvertorImage;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//depth map function
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
int vertexSubsampling;
int chooseColour;
ofPrimitiveMode meshMode;
ofBlendMode blendMode;
float multiplier;
bool _gotFirstImage;
ofImage _background;
};

File diff suppressed because it is too large Load Diff

View File

@ -9,8 +9,6 @@
#include "Poco/Timer.h"
#include "ofxOpenCv.h"
class StrobeAnalysis : public AbstractAnalysis
{
@ -23,62 +21,21 @@ public:
void setup(int camWidth, int camHeight);
void acquire();
void synthesise();
void displayresults();
void display_results();
void draw();
void save_cb(Poco::Timer& timer);
void display_results_cb(Poco::Timer& timer);
protected:
bool _RUN_DONE;
int _strobe_cnt, _strobe_cnt_max;
int _strobe_cnt, _run_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;
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;
//mesh making function
void setMeshFromPixels(vector<float> sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh);
//depth map function
vector<float> _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage);
int vertexSubsampling;
int chooseColour;
ofPrimitiveMode meshMode;
ofBlendMode blendMode;
float multiplier;
bool _gotFirstImage;
ofImage _background;
};

View File

@ -1,211 +0,0 @@
#include "rfiCvContourFinder.h"
//--------------------------------------------------------------------------------
static bool sort_carea_compare( const CvSeq* a, const CvSeq* b) {
// use opencv to calc size, then sort based on size
float areaa = fabs(cvContourArea(a, CV_WHOLE_SEQ));
float areab = fabs(cvContourArea(b, CV_WHOLE_SEQ));
//return 0;
return (areaa > areab);
}
//--------------------------------------------------------------------------------
rfiCvContourFinder::rfiCvContourFinder() {
_width = 0;
_height = 0;
myMoments = (CvMoments*)malloc( sizeof(CvMoments) );
reset();
}
//--------------------------------------------------------------------------------
rfiCvContourFinder::~rfiCvContourFinder() {
free( myMoments );
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::reset() {
cvSeqBlobs.clear();
blobs.clear();
nBlobs = 0;
}
//--------------------------------------------------------------------------------
int rfiCvContourFinder::findContours( ofxCvGrayscaleImage& input,
int minArea,
int maxArea,
int nConsidered,
bool bFindHoles,
bool bUseApproximation) {
// get width/height disregarding ROI
IplImage* ipltemp = input.getCvImage();
_width = ipltemp->width;
_height = ipltemp->height;
reset();
// opencv will clober the image it detects contours on, so we want to
// copy it into a copy before we detect contours. That copy is allocated
// if necessary (necessary = (a) not allocated or (b) wrong size)
// so be careful if you pass in different sized images to "findContours"
// there is a performance penalty, but we think there is not a memory leak
// to worry about better to create mutiple contour finders for different
// sizes, ie, if you are finding contours in a 640x480 image but also a
// 320x240 image better to make two rfiCvContourFinder objects then to use
// one, because you will get penalized less.
if( inputCopy.getWidth() == 0 ) {
inputCopy.setUseTexture(false);
inputCopy.allocate( _width, _height );
} else if( inputCopy.getWidth() != _width || inputCopy.getHeight() != _height ) {
// reallocate to new size
inputCopy.clear();
inputCopy.setUseTexture(false);
inputCopy.allocate( _width, _height );
}
inputCopy.setROI( input.getROI() );
inputCopy = input;
CvSeq* contour_list = NULL;
contour_storage = cvCreateMemStorage( 1000 );
storage = cvCreateMemStorage( 1000 );
CvContourRetrievalMode retrieve_mode
= (bFindHoles) ? CV_RETR_LIST : CV_RETR_EXTERNAL;
cvFindContours( inputCopy.getCvImage(), contour_storage, &contour_list,
sizeof(CvContour), retrieve_mode, bUseApproximation ? CV_CHAIN_APPROX_SIMPLE : CV_CHAIN_APPROX_NONE );
CvSeq* contour_ptr = contour_list;
// put the contours from the linked list, into an array for sorting
while( (contour_ptr != NULL) ) {
float area = fabs( cvContourArea(contour_ptr, CV_WHOLE_SEQ) );
if( (area > minArea) && (area < maxArea) ) {
cvSeqBlobs.push_back(contour_ptr);
}
contour_ptr = contour_ptr->h_next;
}
// sort the pointers based on size
if( cvSeqBlobs.size() > 1 ) {
sort( cvSeqBlobs.begin(), cvSeqBlobs.end(), sort_carea_compare );
}
// now, we have cvSeqBlobs.size() contours, sorted by size in the array
// cvSeqBlobs let's get the data out and into our structures that we like
for( int i = 0; i < MIN(nConsidered, (int)cvSeqBlobs.size()); i++ ) {
blobs.push_back( ofxCvBlob() );
float area = cvContourArea( cvSeqBlobs[i], CV_WHOLE_SEQ );
CvRect rect = cvBoundingRect( cvSeqBlobs[i], 0 );
cvMoments( cvSeqBlobs[i], myMoments );
blobs[i].area = fabs(area);
blobs[i].hole = area < 0 ? true : false;
blobs[i].length = cvArcLength(cvSeqBlobs[i]);
blobs[i].boundingRect.x = rect.x;
blobs[i].boundingRect.y = rect.y;
blobs[i].boundingRect.width = rect.width;
blobs[i].boundingRect.height = rect.height;
blobs[i].centroid.x = (myMoments->m10 / myMoments->m00);
blobs[i].centroid.y = (myMoments->m01 / myMoments->m00);
// get the points for the blob:
CvPoint pt;
CvSeqReader reader;
cvStartReadSeq( cvSeqBlobs[i], &reader, 0 );
for( int j=0; j < cvSeqBlobs[i]->total; j++ ) {
CV_READ_SEQ_ELEM( pt, reader );
blobs[i].pts.push_back( ofPoint((float)pt.x, (float)pt.y) );
}
blobs[i].nPts = blobs[i].pts.size();
}
nBlobs = blobs.size();
// Free the storage memory.
// Warning: do this inside this function otherwise a strange memory leak
if( contour_storage != NULL ) { cvReleaseMemStorage(&contour_storage); }
if( storage != NULL ) { cvReleaseMemStorage(&storage); }
return nBlobs;
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::draw( float x, float y, float w, float h ) {
float scalex = 0.0f;
float scaley = 0.0f;
if( _width != 0 ) { scalex = w/_width; } else { scalex = 1.0f; }
if( _height != 0 ) { scaley = h/_height; } else { scaley = 1.0f; }
if(bAnchorIsPct){
x -= anchor.x * w;
y -= anchor.y * h;
}else{
x -= anchor.x;
y -= anchor.y;
}
ofPushStyle();
glPushMatrix();
glTranslatef( x, y, 0.0 );
glScalef( scalex, scaley, 0.0 );
ofSetHexColor(0xFFFFFF);
for( int i=0; i<(int)blobs.size(); i++ ) {
ofNoFill();
ofBeginShape();
for( int j=0; j<blobs[i].nPts; j++ ) {
ofVertex( blobs[i].pts[j].x, blobs[i].pts[j].y );
}
ofEndShape();
}
glPopMatrix();
ofPopStyle();
}
//----------------------------------------------------------
void rfiCvContourFinder::draw(const ofPoint & point){
draw(point.x, point.y);
}
//----------------------------------------------------------
void rfiCvContourFinder::draw(const ofRectangle & rect){
draw(rect.x, rect.y, rect.width, rect.height);
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::setAnchorPercent( float xPct, float yPct ){
anchor.x = xPct;
anchor.y = yPct;
bAnchorIsPct = true;
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::setAnchorPoint( int x, int y ){
anchor.x = x;
anchor.y = y;
bAnchorIsPct = false;
}
//--------------------------------------------------------------------------------
void rfiCvContourFinder::resetAnchor(){
anchor.set(0,0);
bAnchorIsPct = false;
}

View File

@ -1,73 +0,0 @@
/*
* rfiCvContourFinder.h
*
* Finds white blobs in binary images and identifies
* centroid, bounding box, area, length and polygonal contour
* The result is placed in a vector of ofxCvBlob objects.
*
*/
#ifndef RFI_CV_CONTOUR_FINDER
#define RFI_CV_CONTOUR_FINDER
#include "ofxCvConstants.h"
#include "ofxCvBlob.h"
#include "ofxCvGrayscaleImage.h"
#include <algorithm>
class rfiCvContourFinder : public ofBaseDraws {
public:
vector<ofxCvBlob> blobs;
int nBlobs; // DEPRECATED: use blobs.size() instead
rfiCvContourFinder();
virtual ~rfiCvContourFinder();
virtual float getWidth() { return _width; }; //set after first findContours call
virtual float getHeight() { return _height; }; //set after first findContours call
virtual int findContours( ofxCvGrayscaleImage& input,
int minArea, int maxArea,
int nConsidered, bool bFindHoles,
bool bUseApproximation = true);
// approximation = don't do points for all points
// of the contour, if the contour runs
// along a straight line, for example...
virtual void draw() { draw(0,0, _width, _height); };
virtual void draw( float x, float y ) { draw(x,y, _width, _height); };
virtual void draw( float x, float y, float w, float h );
virtual void draw(const ofPoint & point);
virtual void draw(const ofRectangle & rect);
virtual void setAnchorPercent(float xPct, float yPct);
virtual void setAnchorPoint(int x, int y);
virtual void resetAnchor();
//virtual ofxCvBlob getBlob(int num);
protected:
int _width;
int _height;
ofxCvGrayscaleImage inputCopy;
CvMemStorage* contour_storage;
CvMemStorage* storage;
CvMoments* myMoments;
vector<CvSeq*> cvSeqBlobs; //these will become blobs
ofPoint anchor;
bool bAnchorIsPct;
virtual void reset();
};
#endif