From 357573f81a6f7dcb442b77873eea165f981672e5 Mon Sep 17 00:00:00 2001 From: Jamie Allen Date: Wed, 9 May 2012 21:11:16 +0200 Subject: [PATCH] added the meshcode to all analyses (just for consistency... see extended description 1) Live/interactive version of one or two of the algorithms. I'd vote for IResponse, RelaxRate and ColorMulti as the ones to do live on the laptop. David is looking at this... 2) The frame skipping issue remains. I looked at this while travelling today, but couldn't figure it out. We're still saving out meshes at about half the rate of them being created. 3) The following three analyses do exactly the same thing currently, and it would be good if they had something approaching a real analysis happening. Right now it's just a simple z-map in black and white with some noise on top and lighting... kind of pretty but bit boring currently: ShapeFromShadingAnalysis CamNoiseAnalysis ShadowScapesAnalysis --- src/CamNoiseAnalysis.cpp | 629 ++++++++++++++++++++++++++++--- src/CamNoiseAnalysis.h | 15 +- src/ShadowScapesAnalysis.cpp | 581 ++++++++++++++++++++++++++-- src/ShadowScapesAnalysis.h | 14 +- src/ShapeFromShadingAnalysis.cpp | 125 +++--- 5 files changed, 1210 insertions(+), 154 deletions(-) diff --git a/src/CamNoiseAnalysis.cpp b/src/CamNoiseAnalysis.cpp index fab9cc7..cfff8a0 100755 --- a/src/CamNoiseAnalysis.cpp +++ b/src/CamNoiseAnalysis.cpp @@ -21,12 +21,58 @@ void CamNoiseAnalysis::setup(int camWidth, int camHeight) cout << "NUM_RUN CamNoiseAnalysis " << NUM_RUN << endl; //NUM_RUN = 5; + //flag for main sketch meshIsComplete=false; _gotFirstImage=false; + _mesh_size_multiplier = 15; + vertexSubsampling = 1; + chooseColour = 5; + multiplier = 4.0; + + ofSetLineWidth(5.0f); + glPointSize(5.0f); + + //blendMode = OF_BLENDMODE_ADD; + //blendMode = OF_BLENDMODE_MULTIPLY; + //blendMode = OF_BLENDMODE_SUBTRACT; + blendMode = OF_BLENDMODE_ALPHA; + //blendMode = OF_BLENDMODE_SCREEN; + + //meshMode = OF_PRIMITIVE_TRIANGLES; + //meshMode = OF_PRIMITIVE_TRIANGLE_STRIP; + //meshMode = OF_PRIMITIVE_TRIANGLE_FAN; + meshMode = OF_PRIMITIVE_LINES; + //meshMode = OF_PRIMITIVE_LINE_STRIP; + //meshMode = OF_PRIMITIVE_POINTS; + + ///setup light + ofEnableLighting(); + GLfloat light_ambient[] = {0.5, 0.5, 0.5, 0.5 }; + GLfloat light_diffuse[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_specular[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; + + glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); + glLightfv(GL_LIGHT0, GL_POSITION, light_position); + + glEnable(GL_LIGHT0); + + GLfloat light_ambient1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_diffuse1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_specular1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_position1[] = { -1.0, 1.0, 1.0, 0.0 }; + + glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient1); + glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1); + glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular1); + glLightfv(GL_LIGHT1, GL_POSITION, light_position1); + + glEnable(GL_LIGHT1); + - _mesh_size_multiplier=4; - int acq_run_time; // 10 seconds of acquiring per run acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_camnoise", ACQUIRE_TIME); cout << "ACQUIRE_TIME CamNoiseAnalysis " << acq_run_time << endl; @@ -88,7 +134,6 @@ void CamNoiseAnalysis::setup(int camWidth, int camHeight) cvGrayImage2.clear(); cvGrayDiff2.clear(); - cvConvertorImage.clear(); cvColorImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); @@ -97,9 +142,6 @@ void CamNoiseAnalysis::setup(int camWidth, int camHeight) cvColorImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); - - cvConvertorImage.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); - } @@ -150,63 +192,118 @@ void CamNoiseAnalysis::synthesise() } 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.convertToGrayscalePlanarImage(cvGrayImage1, 1); - // cvColorImage2.convertToGrayscalePlanarImage(cvGrayImage2, 1); - //added by tom we weren't actually setting cvgrayimage1 anywhere - // cvGrayImage1.setFromPixels(cvColorImage1.getPixels(),cvColorImage1.width,cvColorImage1.height); - //cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1); - //cvGrayImage1=cvColorImage1; - - cvGrayImage1.erode(); - cvGrayImage1.erode(); - cvGrayImage1.erode(); - cvGrayImage1.blur(); - cvGrayImage1.contrastStretch(); - /* cvColorImage1.erode(); - cvColorImage1.erode(); - cvColorImage1.erode(); - cvColorImage1.blur(); - cvColorImage1.contrastStretch();*/ - - /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// - string file_name; - - file_name = ofToString(_synth_save_cnt, 2)+"_CamNoiseAnalysis_"+ofToString(_run_cnt,2)+".jpg"; - - - //<---- THE OLD WAY OF SAVING - works on OSX but generates BLACK FRAMES on WINDOWS ----> - // ofSaveImage(cvGrayImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST); - - - //<---- NEW SAVING - seems to fix WINDOWS saving out BLACK FRAMES PROBLEM ----> - //ofImage image; - //image.allocate(cvGrayImage1.width, cvGrayImage1.height, OF_IMAGE_GRAYSCALE); - - //*** This needs to be here for OSX of we get a BAD ACCESS ERROR. DOES IT BREAK WINDOWS? ***// - //image.setUseTexture(false); - - //image.setFromPixels(cvGrayImage1.getPixels(), cvGrayImage1.width, cvGrayImage1.height, OF_IMAGE_GRAYSCALE); - //image.saveImage(_whole_file_path_synthesis+"/"+file_name); - - //_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name); - - // <--- REALLY NEW SAVING METHOD --- 26 feb 2012 --- consolidated the save function into Abstract Analysis> /// - cvConvertorImage.setFromGrayscalePlanarImages(cvGrayImage1,cvGrayImage1,cvGrayImage1); - //cvConvertorImage.setFromGrayscalePlanarImages(cvColorImage1,cvColorImage1,cvColorImage1); - saveImageSynthesis(file_name, &cvConvertorImage, OF_IMAGE_GRAYSCALE); - _synth_save_cnt++; - - - //} + // cout << "LOADED image1!!!" << endl; + if(image5.loadImage(_saved_filenames_analysis[i+1])){ + + ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// + + cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height); + cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height); + + cvColorImage1.convertToGrayscalePlanarImage(cvGrayImage1, 1); + cvColorImage2.convertToGrayscalePlanarImage(cvGrayImage2, 1); + + //cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1); + //cvGrayDiff1.erode(); + + cvGrayImage1.dilate(); + cvGrayImage1.blur(5); + cvGrayImage1.contrastStretch(); + + /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// + cvColorImage1.setFromGrayscalePlanarImages(cvGrayImage1, cvGrayImage1, cvGrayImage1); + //cvColorImage2.setFromGrayscalePlanarImages(cvGrayImage2, cvGrayImage2, cvGrayImage2); + + //cvPyrMeanShiftFiltering(cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), 1, 1); + //cvPyrMeanShiftFiltering(cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), 1, 1); + + //cvFloatImage1 = cvColorImage1; + //cvGrayImage1 = cvColorImage1; + + + //cvSmooth( cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), CV_GAUSSIAN, 5, 5); + //cvSmooth( cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), CV_GAUSSIAN, 5, 5); + + //cvCanny(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 100, 100, 3); + //cvLaplace(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0); + + //cvGrayImage1 = cvCreateImage(cvSize(image1.width, image1.height),IPL_DEPTH_16S,1); + //cvSobel(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0, 1, 3); + + // convert the CV image + image1.setFromPixels(cvColorImage1.getPixelsRef()); + image5.setFromPixels(cvColorImage2.getPixelsRef()); + + ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// + if(!_gotFirstImage){ + cout<<"background image is"<< _saved_filenames_analysis[i]<0){ + imagePixels1[i]-=imagePixels2[i]; + } + else{ + imagePixels1[i]=0; + } + } + */ + + //DIFFERENCING THE BACKGROUND + /* + for(int i=0;i0){ + imagePixels1[i]-=backgroundPixels[i]; + } + else{ + imagePixels1[i]=0; + } + } + */ + + //update the images with their new background subtracted selves + image1.setFromPixels(imagePixels1); + + //flag the main app that we aren't read yet + meshIsComplete=false; + + //make a mesh - this mesh will be drawn in the main app + setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); + + //setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); + + /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// + //string file_name; + + //with jpgs this was refusing to save out + meshFileName = _whole_file_path_synthesis+"/"+ofToString(_synth_save_cnt, 2)+"_CamNoiseAnalysis_"+ofToString(_run_cnt,2)+".png"; + _saved_filenames_synthesis.push_back(meshFileName); + + //file_name = ofToString(_synth_save_cnt, 2)+"_ColorMultiAnalysis_"+ofToString(_run_cnt,2)+".jpg"; + + //flag that we are finished + meshIsComplete=true; + _synth_save_cnt++; + + } + } else { + cout<<"couldn't load image from "<<_saved_filenames_analysis[i]< sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh){ + int x=0; + int y=0; + + //get rid of all previous vectors and colours + mesh.clear(); + mesh.setMode(meshMode); + + ofColor meshColour=ofColor(255,255,255); + + //the average z position of the matrix - used later to centre the mesh on the z axis when drawing + float zPlaneAverage=0; + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==2){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==3){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==4){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==5){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + +} + + +vector CamNoiseAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){ + + ofPixels imagePixels1 = image1.getPixelsRef(); + //ofPixels imagePixels2 = image2.getPixelsRef(); + ofPixels backgroundPixels = backgroundImag.getPixelsRef(); + vector differences; + + ofPixels difference; + + //this unsigned char should be unnecessary - I would have thought - can't you just address the pixel locations in ofPixels directly? + unsigned char * thesePixels = new unsigned char[imagePixels1.getWidth()*imagePixels1.getHeight()*3]; + + for(int i=0;i=imagePixels1.getWidth()){ + x=0; + y++; + } + } + } + + //difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3); + return differences; +} diff --git a/src/CamNoiseAnalysis.h b/src/CamNoiseAnalysis.h index b2831e5..f1a1002 100755 --- a/src/CamNoiseAnalysis.h +++ b/src/CamNoiseAnalysis.h @@ -61,10 +61,21 @@ protected: ofxCvContourFinder cvContourFinder1; - //this is the temporary container to allow us to convert and save out greyscale images - ofxCvColorImage cvConvertorImage; + + //mesh making function + void setMeshFromPixels(vector sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh); + + //depth map function + vector _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage); + + int vertexSubsampling; + int chooseColour; + ofPrimitiveMode meshMode; + ofBlendMode blendMode; + float multiplier; bool _gotFirstImage; ofImage _background; + }; diff --git a/src/ShadowScapesAnalysis.cpp b/src/ShadowScapesAnalysis.cpp index 8154070..34e4b30 100755 --- a/src/ShadowScapesAnalysis.cpp +++ b/src/ShadowScapesAnalysis.cpp @@ -28,10 +28,52 @@ void ShadowScapesAnalysis::setup(int camWidth, int camHeight) //flag for main sketch meshIsComplete=false; _gotFirstImage=false; + _mesh_size_multiplier = 15; + vertexSubsampling = 1; + chooseColour = 5; + multiplier = 4.0; - _mesh_size_multiplier=4; + ofSetLineWidth(5.0f); + glPointSize(5.0f); - + //blendMode = OF_BLENDMODE_ADD; + //blendMode = OF_BLENDMODE_MULTIPLY; + //blendMode = OF_BLENDMODE_SUBTRACT; + blendMode = OF_BLENDMODE_ALPHA; + //blendMode = OF_BLENDMODE_SCREEN; + + //meshMode = OF_PRIMITIVE_TRIANGLES; + //meshMode = OF_PRIMITIVE_TRIANGLE_STRIP; + //meshMode = OF_PRIMITIVE_TRIANGLE_FAN; + meshMode = OF_PRIMITIVE_LINES; + //meshMode = OF_PRIMITIVE_LINE_STRIP; + //meshMode = OF_PRIMITIVE_POINTS; + + ///setup light + ofEnableLighting(); + GLfloat light_ambient[] = {0.5, 0.5, 0.5, 0.5 }; + GLfloat light_diffuse[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_specular[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; + + glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); + glLightfv(GL_LIGHT0, GL_POSITION, light_position); + + glEnable(GL_LIGHT0); + + GLfloat light_ambient1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_diffuse1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_specular1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_position1[] = { -1.0, 1.0, 1.0, 0.0 }; + + glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient1); + glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1); + glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular1); + glLightfv(GL_LIGHT1, GL_POSITION, light_position1); + + glEnable(GL_LIGHT1); int acq_run_time; // 10 seconds of acquiring per run acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_shadowscapes", ACQUIRE_TIME); @@ -95,8 +137,7 @@ void ShadowScapesAnalysis::setup(int camWidth, int camHeight) cvColorImage2.clear(); cvGrayImage2.clear(); cvGrayDiff2.clear(); - - cvConvertorImage.clear(); + cvColorImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayImage1.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); @@ -106,7 +147,6 @@ void ShadowScapesAnalysis::setup(int camWidth, int camHeight) cvGrayImage2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); cvGrayDiff2.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); - cvConvertorImage.allocate(RefractiveIndex::_vid_w,RefractiveIndex::_vid_h); } @@ -150,65 +190,116 @@ void ShadowScapesAnalysis::synthesise() } if(image1.loadImage(_saved_filenames_analysis[i])){ - + // cout << "LOADED image1!!!" << endl; if(image5.loadImage(_saved_filenames_analysis[i+1])){ ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// - + cvColorImage1.setFromPixels(image1.getPixels(), image1.width, image1.height); cvColorImage2.setFromPixels(image5.getPixels(), image5.width, image5.height); - //cvGrayImage1 = cvColorImage1; - //cvGrayImage2 = cvColorImage2; - cvColorImage1.convertToGrayscalePlanarImage(cvGrayImage1, 1); cvColorImage2.convertToGrayscalePlanarImage(cvGrayImage2, 1); - - cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1); - cvGrayDiff1.erode(); - cvGrayDiff1.contrastStretch(); - cvGrayDiff1.blur(5); - cvGrayDiff1.dilate(); + + //cvGrayDiff1.absDiff(cvGrayImage2, cvGrayImage1); + //cvGrayDiff1.erode(); + + cvGrayImage1.dilate(); + cvGrayImage1.blur(5); + cvGrayImage1.contrastStretch(); /////////////////////////////////// SAVE TO DISK IN THE SYNTHESIS FOLDER //////////////////////////////// - string file_name; + cvColorImage1.setFromGrayscalePlanarImages(cvGrayImage1, cvGrayImage1, cvGrayImage1); + //cvColorImage2.setFromGrayscalePlanarImages(cvGrayImage2, cvGrayImage2, cvGrayImage2); - if(_dir == H) { - file_name = ofToString(_synth_save_cnt, 2)+"_H_ShadowScapesSynthesis_"+ofToString(_run_cnt,2)+".jpg"; + //cvPyrMeanShiftFiltering(cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), 1, 1); + //cvPyrMeanShiftFiltering(cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), 1, 1); + + //cvFloatImage1 = cvColorImage1; + //cvGrayImage1 = cvColorImage1; + + + //cvSmooth( cvColorImage1.getCvImage(), cvColorImage1.getCvImage(), CV_GAUSSIAN, 5, 5); + //cvSmooth( cvColorImage2.getCvImage(), cvColorImage2.getCvImage(), CV_GAUSSIAN, 5, 5); + + //cvCanny(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 100, 100, 3); + //cvLaplace(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0); + + //cvGrayImage1 = cvCreateImage(cvSize(image1.width, image1.height),IPL_DEPTH_16S,1); + //cvSobel(cvGrayImage1.getCvImage(), cvGrayImage1.getCvImage(), 0, 1, 3); + + // convert the CV image + image1.setFromPixels(cvColorImage1.getPixelsRef()); + image5.setFromPixels(cvColorImage2.getPixelsRef()); + + ///////////////////////// PROCESS THE SAVED CAMERA IMAGES OF SHIT TO THE IMAGES ////////////////////////// + if(!_gotFirstImage){ + cout<<"background image is"<< _saved_filenames_analysis[i]<0){ + imagePixels1[i]-=imagePixels2[i]; + } + else{ + imagePixels1[i]=0; + } + } + */ + //DIFFERENCING THE BACKGROUND + /* + for(int i=0;i0){ + imagePixels1[i]-=backgroundPixels[i]; + } + else{ + imagePixels1[i]=0; + } + } + */ - //<---- THE OLD WAY OF SAVING - works on OSX but generates BLACK FRAMES on WINDOWS ----> - // ofSaveImage(cvGrayImage1.getPixelsRef(),_whole_file_path_synthesis+"/"+file_name, OF_IMAGE_QUALITY_BEST); + //update the images with their new background subtracted selves + image1.setFromPixels(imagePixels1); + //flag the main app that we aren't read yet + meshIsComplete=false; - //<---- NEW SAVING - seems to fix WINDOWS saving out BLACK FRAMES PROBLEM ----> - //ofImage image; - //image.allocate(cvGrayDiff1.width, cvGrayDiff1.height, OF_IMAGE_GRAYSCALE); + //make a mesh - this mesh will be drawn in the main app + setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); - //*** This needs to be here for OSX of we get a BAD ACCESS ERROR. DOES IT BREAK WINDOWS? ***// - //image.setUseTexture(false); + //setMeshFromPixels(_returnDepthsAtEachPixel(image1, image1, _background), image1, image1, aMesh); - //image.setFromPixels(cvGrayDiff1.getPixels(), cvGrayDiff1.width, cvGrayDiff1.height, OF_IMAGE_GRAYSCALE); - //image.saveImage(_whole_file_path_synthesis+"/"+file_name); - - //_saved_filenames_synthesis.push_back(_whole_file_path_synthesis+"/"+file_name); - - // <--- REALLY NEW SAVING METHOD --- 26 feb 2012 --- consolidated the save function into Abstract Analysis> /// - cvConvertorImage.setFromGrayscalePlanarImages(cvGrayDiff1,cvGrayDiff1,cvGrayDiff1); + /////////////////////////////////// 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)+"_ShadowScapeAnalysis_"+ofToString(_run_cnt,2)+".png"; + _saved_filenames_synthesis.push_back(meshFileName); - saveImageSynthesis(file_name, &cvConvertorImage, OF_IMAGE_GRAYSCALE); + //file_name = ofToString(_synth_save_cnt, 2)+"_ColorMultiAnalysis_"+ofToString(_run_cnt,2)+".jpg"; + + //flag that we are finished + meshIsComplete=true; _synth_save_cnt++; + } + } else { + cout<<"couldn't load image from "<<_saved_filenames_analysis[i]< sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh){ + int x=0; + int y=0; + + //get rid of all previous vectors and colours + mesh.clear(); + mesh.setMode(meshMode); + + ofColor meshColour=ofColor(255,255,255); + + //the average z position of the matrix - used later to centre the mesh on the z axis when drawing + float zPlaneAverage=0; + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==2){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==3){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==4){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + + if(chooseColour==5){ + + for(int i=0;i=currentSecondImage.getWidth()-1){ + x=0; + y=y+vertexSubsampling; + //something is going badly wrong with my maths for me to need this HELP TODO fix this - why am I running over the end of the vector? + if(y>=currentSecondImage.getHeight()-1){ + break; + } + } + } + } + +} + + +vector ShadowScapesAnalysis::_returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImag){ + + ofPixels imagePixels1 = image1.getPixelsRef(); + //ofPixels imagePixels2 = image2.getPixelsRef(); + ofPixels backgroundPixels = backgroundImag.getPixelsRef(); + vector differences; + + ofPixels difference; + + //this unsigned char should be unnecessary - I would have thought - can't you just address the pixel locations in ofPixels directly? + unsigned char * thesePixels = new unsigned char[imagePixels1.getWidth()*imagePixels1.getHeight()*3]; + + for(int i=0;i=imagePixels1.getWidth()){ + x=0; + y++; + } + } + } + + //difference.setFromPixels(thesePixels,imagePixels1.getWidth(),imagePixels1.getHeight(), 3); + return differences; +} + diff --git a/src/ShadowScapesAnalysis.h b/src/ShadowScapesAnalysis.h index c1c77c4..c5b0a73 100755 --- a/src/ShadowScapesAnalysis.h +++ b/src/ShadowScapesAnalysis.h @@ -66,10 +66,20 @@ protected: ofxCvContourFinder cvContourFinder1; - //this is the temporary container to allow us to convert and save out greyscale images - ofxCvColorImage cvConvertorImage; + //mesh making function + void setMeshFromPixels(vector sPixels, ofImage currentFirstImage, ofImage currentSecondImage, ofMesh & mesh); + + //depth map function + vector _returnDepthsAtEachPixel(ofImage &image1, ofImage &image2, ofImage &backgroundImage); + + int vertexSubsampling; + int chooseColour; + ofPrimitiveMode meshMode; + ofBlendMode blendMode; + float multiplier; bool _gotFirstImage; ofImage _background; + }; \ No newline at end of file diff --git a/src/ShapeFromShadingAnalysis.cpp b/src/ShapeFromShadingAnalysis.cpp index 56c1716..70c1cc8 100755 --- a/src/ShapeFromShadingAnalysis.cpp +++ b/src/ShapeFromShadingAnalysis.cpp @@ -24,24 +24,53 @@ void ShapeFromShadingAnalysis::setup(int camWidth, int camHeight) //flag for main sketch meshIsComplete=false; _gotFirstImage=false; - _mesh_size_multiplier = 10; + _mesh_size_multiplier = 15; vertexSubsampling = 1; chooseColour = 5; multiplier = 4.0; + ofSetLineWidth(5.0f); + glPointSize(5.0f); + //blendMode = OF_BLENDMODE_ADD; //blendMode = OF_BLENDMODE_MULTIPLY; //blendMode = OF_BLENDMODE_SUBTRACT; - //blendMode = OF_BLENDMODE_ALPHA; + blendMode = OF_BLENDMODE_ALPHA; //blendMode = OF_BLENDMODE_SCREEN; - meshMode = OF_PRIMITIVE_TRIANGLES; + //meshMode = OF_PRIMITIVE_TRIANGLES; //meshMode = OF_PRIMITIVE_TRIANGLE_STRIP; //meshMode = OF_PRIMITIVE_TRIANGLE_FAN; - //meshMode = OF_PRIMITIVE_LINES; + meshMode = OF_PRIMITIVE_LINES; //meshMode = OF_PRIMITIVE_LINE_STRIP; //meshMode = OF_PRIMITIVE_POINTS; + ///setup light + ofEnableLighting(); + GLfloat light_ambient[] = {0.5, 0.5, 0.5, 0.5 }; + GLfloat light_diffuse[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_specular[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; + + glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); + glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); + glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); + glLightfv(GL_LIGHT0, GL_POSITION, light_position); + + glEnable(GL_LIGHT0); + + GLfloat light_ambient1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_diffuse1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_specular1[] = { 0.5, 0.5, 0.5, 0.5 }; + GLfloat light_position1[] = { -1.0, 1.0, 1.0, 0.0 }; + + glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient1); + glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse1); + glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular1); + glLightfv(GL_LIGHT1, GL_POSITION, light_position1); + + glEnable(GL_LIGHT1); + int acq_run_time; // 10 seconds of acquiring per run acq_run_time = RefractiveIndex::XML.getValue("config:analysis_time:acquiretime_shapefromshading", ACQUIRE_TIME); cout << "ACQUIRE_TIME ShapeFromShadingAnalysis " << acq_run_time << endl; @@ -204,8 +233,6 @@ void ShapeFromShadingAnalysis::synthesise() fileNameQuad = "QUAD4"; cout<<"FOUND QUAD4"< sPixels, ofImage currentSecondImageBW.g = currentSecondImageColor.getBrightness()+randomJitter2; currentSecondImageBW.b = currentSecondImageColor.getBrightness()+randomJitter2; - mesh.addColor( currentSecondImageBW.clamp()); + mesh.addColor( currentSecondImageBW); mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x),_mesh_size_multiplier*((y)+1),- sPixels[ (currentSecondImage.getWidth()*(y+randomJitter+1))+(x+randomJitter) ] )); currentSecondImageColor = currentSecondImage.getColor(x, y); @@ -1061,7 +1088,7 @@ void ShapeFromShadingAnalysis::setMeshFromPixels(vector sPixels, ofImage currentSecondImageBW.g = currentSecondImageColor.getBrightness()+randomJitter2; currentSecondImageBW.b = currentSecondImageColor.getBrightness()+randomJitter2; - mesh.addColor(currentSecondImageBW.clamp()); + mesh.addColor(currentSecondImageBW); mesh.addVertex(ofVec3f(_mesh_size_multiplier*(x),_mesh_size_multiplier*(y),- sPixels[(currentSecondImage.getWidth()*((y+randomJitter)))+(x+randomJitter) ] )); currentSecondImageColor = currentSecondImage.getColor(x+1, y+1); @@ -1069,7 +1096,7 @@ void ShapeFromShadingAnalysis::setMeshFromPixels(vector sPixels, ofImage currentSecondImageBW.g = currentSecondImageColor.getBrightness()+randomJitter2; currentSecondImageBW.b = currentSecondImageColor.getBrightness()+randomJitter2; - mesh.addColor(currentSecondImageBW.clamp()); + mesh.addColor(currentSecondImageBW); mesh.addVertex(ofVec3f(_mesh_size_multiplier*((x)+1),_mesh_size_multiplier*((y)+1),- sPixels[(currentSecondImage.getWidth()*((y+randomJitter)+1))+(x+randomJitter)+1 ] )); currentSecondImageColor = currentSecondImage.getColor(x+1, y+1); @@ -1077,7 +1104,7 @@ void ShapeFromShadingAnalysis::setMeshFromPixels(vector sPixels, ofImage currentSecondImageBW.g = currentSecondImageColor.getBrightness()+randomJitter2; currentSecondImageBW.b = currentSecondImageColor.getBrightness()+randomJitter2; - mesh.addColor( currentSecondImageBW.clamp() ); + mesh.addColor( currentSecondImageBW); mesh.addVertex(ofVec3f(_mesh_size_multiplier*((x)+1),_mesh_size_multiplier*((y)+1),- sPixels[(currentSecondImage.getWidth()*((y+randomJitter)+1))+(x+randomJitter)+1] )); currentSecondImageColor = currentSecondImage.getColor(x, y); @@ -1141,16 +1168,18 @@ vector ShapeFromShadingAnalysis::_returnDepthsAtEachPixel(ofImage &image1 for(int i=0;i