Quantcast
Channel: openFrameworks - Latest posts
Viewing all articles
Browse latest Browse all 40524

How Interact with ContourFinder(Contour detected -OfxOpencv),blobs VS Some kind of objects,like a simple circle or a system particle for instance?

$
0
0

benMcChesney,Thanks I'll try.

@lilive,The last chance I promisse! :sweat:
I still getting confuse.

I joined opencvExample and your example of repulsion to give me an ideia how to change the movie mouse for just blobs contourFinder.

class ofApp : public ofBaseApp{

public:
	void setup();
	void update();
	void draw();
	
	void keyPressed(int key);
	void keyReleased(int key);
	void mouseMoved(int x, int y );
	void mouseDragged(int x, int y, int button);
	void mousePressed(int x, int y, int button);
	void mouseReleased(int x, int y, int button);
	void windowResized(int w, int h);
	void dragEvent(ofDragInfo dragInfo);
	void gotMessage(ofMessage msg);		

    #ifdef _USE_LIVE_VIDEO
	  ofVideoGrabber 		vidGrabber;
	#else
	  ofVideoPlayer 		vidPlayer;
	#endif

    ofxCvColorImage			colorImg;

    ofxCvGrayscaleImage 	grayImage;
	ofxCvGrayscaleImage 	grayBg;
	ofxCvGrayscaleImage 	grayDiff;

    ofxCvContourFinder 	contourFinder;

	int 				threshold;
	bool				bLearnBakground;

    ofVec2f p;

};


include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){

#ifdef _USE_LIVE_VIDEO
    vidGrabber.setVerbose(true);
    vidGrabber.initGrabber(320,240);
#else
    vidPlayer.loadMovie("fingers.mov");
    vidPlayer.play();
#endif

colorImg.allocate(320,240);
grayImage.allocate(320,240);
grayBg.allocate(320,240);
grayDiff.allocate(320,240);

bLearnBakground = true;
threshold = 80;

**p.set( ofGetWidth() * 0.5f, ofGetHeight() * 0.5f );**

}

//--------------------------------------------------------------

void ofApp::update(){

ofBackground(100,100,100);

bool bNewFrame = false;

#ifdef _USE_LIVE_VIDEO
   vidGrabber.update();
   bNewFrame = vidGrabber.isFrameNew();
#else
    vidPlayer.update();
    bNewFrame = vidPlayer.isFrameNew();
#endif

if (bNewFrame){

	#ifdef _USE_LIVE_VIDEO
        colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
    #else
        colorImg.setFromPixels(vidPlayer.getPixels(), 320,240);
    #endif

    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);

	// 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(grayDiff, 20, (340*240)/3, 10, true);	// find holes
    
    
    **ofVec2f c( ofGetMouseX(), ofGetMouseY() ); // Point of repulsion**
    **ofVec2f v = p - c; // Vector between c and p**
    **float d = v.length(); // Distance between c and p**
    **float f = 10000 / d / d; // Intensity of the repulsion (quick search on the web, found http://www.physicsclassroom.com/class/estatics/Lesson-3/Coulomb-s-Law)**
    **v = v.getNormalized() * f; // Force vector**
    **p += v; // Move the point with this vector**
    
    **// Just to bound the movement**
    **p.x = ofClamp( p.x, 50, ofGetWidth() - 50 );**
    **p.y = ofClamp( p.y, 50, ofGetHeight() - 50 );**
}

}

//--------------------------------------------------------------
void ofApp::draw(){

// draw the incoming, the grayscale, the bg and the thresholded difference
ofSetHexColor(0xffffff);
colorImg.draw(20,20);
grayImage.draw(360,20);
grayBg.draw(20,280);
grayDiff.draw(360,280);

// then draw the contours:

ofFill();
ofSetHexColor(0x333333);
ofRect(360,540,320,240);
ofSetHexColor(0xffffff);

// we could draw the whole contour finder
//contourFinder.draw(360,540);

// or, instead we can draw each blob individually from the blobs vector,
// this is how to get access to them:
for (int i = 0; i < contourFinder.nBlobs; i++){
    contourFinder.blobs[i].draw(360,540);
	
	// draw over the centroid if the blob is a hole
	ofSetColor(255);
	if(contourFinder.blobs[i].hole){
		ofDrawBitmapString("hole",
			contourFinder.blobs[i].boundingRect.getCenter().x + 360,
			contourFinder.blobs[i].boundingRect.getCenter().y + 540);
	}
}

// finally, a report:
ofSetHexColor(0xffffff);
stringstream reportStr;
reportStr << "bg subtraction and blob detection" << endl
		  << "press ' ' to capture bg" << endl
		  << "threshold " << threshold << " (press: +/-)" << endl
		  << "num blobs found " << contourFinder.nBlobs << ", fps: " << ofGetFrameRate();
ofDrawBitmapString(reportStr.str(), 20, 600);


**ofFill();**
**ofCircle( p.x, p.y, 30 );**

}


How Can I tell my app use the blobs centroid just to repel the circle?


Viewing all articles
Browse latest Browse all 40524

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>