video replace when blobs over

Hello,

As I’m not familiar with code writing, can somebody please help me with this code? thanks a lot. I was using the example ofxFBOMovieClip and OpenCV

I want a movie clip replace the previous one when blobs move over it and start playing, but with my code, the clip would appear when blob over it but stop at the first frame (still frame) without playing, so how can I fix the code?

here is my code :

  
  
void testApp::setup(){  
  
	ofSetFrameRate(30);  
      
    fis.loadAndCreateSequence("horses");  
    fis.loadAndCreateSequence("frogs");  
          
    vid.posx = 350;  
    vid.posy = 350;  
      
    // create as many movieclips as you want and pass in a reference to the ofxFBOImageSequenceLoader & desired frame rate  
    mc.init(&fis, 30.0f);  
    mc.gotoAndPlay("horses");  
      
    vidGrabber.setVerbose(true);  
    vidGrabber.initGrabber(320,240);  
      
    vw = 320;  
    vh = 240;  
  
    colorImg.allocate(vw,vh);  
	grayImage.allocate(vw,vh);  
	grayBg.allocate(vw,vh);  
	grayDiff.allocate(vw,vh);  
    grayDifflarge.allocate(ofGetWidth(), ofGetHeight());  
      
	bLearnBakground = true;  
	threshold = 45;  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
	ofBackground(100,100,100);  
  
    bool bNewFrame = false;  
  
    vidGrabber.grabFrame();  
    bNewFrame = vidGrabber.isFrameNew();  
  
	if (bNewFrame){  
  
		  
        colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);  
	      
        grayImage = colorImg;  
          
		if (bLearnBakground == true){  
			grayBg = grayImage;		// the = sign copys the pixels from grayImage into grayBg (operator overloading)  
			bLearnBakground = false;  
		}  
  
		// take the abs value of the difference between background and incoming and then threshold:  
		grayDiff.absDiff(grayBg, grayImage);  
		grayDiff.threshold(threshold);  
          
        grayDifflarge.scaleIntoMe(grayDiff);  
  
		// find contours which are between the size of 20 pixels and 1/3 the w*h pixels.  
		// also, find holes is set to true so we will get interior contours as well....  
		contourFinder.findContours(grayDifflarge, 20, (ofGetWidth()*ofGetHeight())/3, 8, true);	// find holes  
	}  
      
    for(int i=0; i<contourFinder.blobs.size(); i++){  
          
        vector<ofPoint> contour_points;  
        contour_points = contourFinder.blobs[i].pts;  
                              
        bool isInside;  
        isInside = ofInsidePoly(vid.posx, vid.posy, contour_points);  
          
        if(isInside){  
              
            mc.gotoAndPlay("frogs");  
   
        }  
          
/*        if(!isInside){  
            mc.gotoAndPlay("horses");  
        }  
*/  
    }   
}  
  
  

Thanks.

Hey, the problem is you keep calling mc.gotoAndPlay(“frogs”) in the update function. This keeps resetting the playhead to the first frame from the ‘frogs’ animation.

I would add a variable to keep track of the animation states in the setup function:
bool isFrogsPlaying; // in the .h
isFrogsPlaying = false; // in setup in .cpp

And then in your update, replace the whole contourFinder.blobs for loop (line 63) with something like this:

  
  
// check all blobs to see if were inside at least one  
bool isInside = false;  
for(int i=0; i<contourFinder.blobs.size(); i++){    
      
    vector<ofPoint> contour_points;    
    contour_points = contourFinder.blobs[i].pts;    
  
    if(ofInsidePoly(vid.posx, vid.posy, contour_points) isInside = true;  
     
}     
     
// only play frogs if it's not already playing & we were inside a blob  
// otherwise play the horses if it's not already playing  
if(isInside && !isFrogsPlaying){    
   mc.gotoAndPlay("frogs");  
   isFrogsPlaying = true;  
} else if(!isInside && isFrogsPlaying) {  
   mc.gotoAndPlay("horses");  
   isFrogsPlaying = false;  
}