//-------------------------------------------------------------- void testApp::keyPressed(int key){ if ( key == '1' ) { //Select first image to morph morphImageIndex = 1; updateMorph( morphValue, morphImageIndex ); } if ( key == '2' ) { //Select checkerboard image to morph morphImageIndex = 2; updateMorph( morphValue, morphImageIndex ); } }
void MorphGeometry::changed(ConstFieldMaskArg whichField, UInt32 origin, BitVector details) { Inherited::changed(whichField, origin, details); //Do not respond to changes that have a Sync origin if(origin & ChangedOrigin::Sync) { return; } if(whichField & BaseGeometryFieldMask) { if(getBaseGeometry() != NULL) { if(getBaseGeometry()->getTypes() != NULL) { GeoIntegralPropertyUnrecPtr Prop = dynamic_pointer_cast<GeoIntegralProperty>(getBaseGeometry()->getTypes()->clone()); setTypes(Prop); } if(getBaseGeometry()->getLengths() != NULL) { GeoIntegralPropertyUnrecPtr Prop = dynamic_pointer_cast<GeoIntegralProperty>(getBaseGeometry()->getLengths()->clone()); setLengths(Prop); } if(getBaseGeometry()->getIndices() != NULL) { GeoIntegralPropertyUnrecPtr Prop = dynamic_pointer_cast<GeoIntegralProperty>(getBaseGeometry()->getIndices()->clone()); setIndices(Prop); } for(UInt16 i(0) ; i<Geometry::LastIndex ; ++i) { if(getBaseGeometry()->getProperty(i) != NULL) { GeoVectorPropertyUnrecPtr Prop = dynamic_pointer_cast<GeoVectorProperty>(getBaseGeometry()->getProperty(i)->clone()); setProperty(Prop, i); } else { setProperty(NULL, i); } } setMaterial(getBaseGeometry()->getMaterial()); } else { setTypes(NULL); setLengths(NULL); for(UInt16 i(0) ; i<Geometry::LastIndex ; ++i) { setProperty(NULL, i); } setIndices(NULL); setMaterial(NULL); } } if((whichField & InternalWeightsFieldMask) || (whichField & InternalTargetGeometriesFieldMask) || (whichField & BaseGeometryFieldMask) || (whichField & MorphPropertiesFieldMask)) { updateMorph(); } }
//-------------------------------------------------------------- void testApp::setup() { ofImage imageOf1, imageOf2; //Load openFrameworks' images imageOf1.loadImage("crater1.png"); imageOf2.loadImage("crater2.png"); color1.setFromPixels( imageOf1 ); //Convert to ofxCv images color2.setFromPixels( imageOf2 ); float decimate = 0.3; //Decimate images to 30% ofxCvColorImage imageDecimated1; imageDecimated1.allocate( color1.width * decimate, color1.height * decimate ); //High-quality resize imageDecimated1.scaleIntoMe( color1, CV_INTER_AREA ); gray1 = imageDecimated1; ofxCvColorImage imageDecimated2; imageDecimated2.allocate( color2.width * decimate, color2.height * decimate ); //High-quality resize imageDecimated2.scaleIntoMe( color2, CV_INTER_AREA ); gray2 = imageDecimated2; Mat img1( gray1.getCvImage() ); //Create OpenCV images Mat img2( gray2.getCvImage() ); Mat flow; //Image for flow //Computing optical flow calcOpticalFlowFarneback( img1, img2, flow, 0.7, 3, 11, 5, 5, 1.1, 0 ); //Split flow into separate images vector<Mat> flowPlanes; split( flow, flowPlanes ); //Copy float planes to ofxCv images flowX and flowY IplImage iplX( flowPlanes[0] ); flowX = &iplX; IplImage iplY( flowPlanes[1] ); flowY = &iplY; //-------------------------------------------------------------------------- //ATTENTION: Lines flowX = &iplX; and flowY = &iplY; can raise runtime error, //caused by small bug in ofxOpenCV. //So before running the example, fix it, as it described in testApp.h file //-------------------------------------------------------------------------- w = gray1.width; h = gray1.height; //Flow image planeX = flowX; planeY = flowY; //create idX, idy idX.allocate( w, h ); idY.allocate( w, h ); for (int y=0; y<h; y++) { for (int x=0; x<w; x++) { idX.getPixelsAsFloats()[ x + w * y ] = x; idY.getPixelsAsFloats()[ x + w * y ] = y; } } //Load checkerboard image ofImage imageTest; imageTest.loadImage("checkerBoard.png"); colorTest.setFromPixels( imageTest ); //Make morphing at first time morphValue = 0; morphImageIndex = 1; updateMorph( morphValue, morphImageIndex ); }
//-------------------------------------------------------------- void testApp::mouseMoved(int x, int y ){ morphValue = ofMap( x, 0, ofGetWidth(), 0, 1 ); updateMorph( morphValue, morphImageIndex ); }