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

Using ofxCvContourFinder's findContours

$
0
0

Here is a start.

declare some stuff in ofApp.h

ofxCvColorImage colorImage;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage thresholdImage;
ofxCvContourFinder 	contourFinder;

ofApp.cpp

//--------------------------------------------------------------
void ofApp::setup(){
    
    ofImage image;
    image.loadImage("appelbaum1.png"); // load in your image
    image.setImageType(OF_IMAGE_COLOR); // get rid of alpha channel from png
    
    ofSetWindowShape(image.getWidth(), image.getHeight());
    
    colorImage.setFromPixels(image.getPixelsRef()); // put the pixels into the image format openCV understands

    grayImage = colorImage;  // convert the image to grayscale using the handy overloaded operator
    thresholdImage = grayImage; // make a copy for thresholding
    
    thresholdImage.threshold(100); // turn any pixels above 100 white, and below 100 black

    
    int totalArea = thresholdImage.width*thresholdImage.height;
    int minArea = totalArea * 0.001;
    int maxArea = totalArea * 0.75;
    int nConsidered = 10;

    contourFinder.findContours(thresholdImage, minArea, maxArea, nConsidered, true);
}
     

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

}

//--------------------------------------------------------------
void ofApp::draw(){
    ofSetColor(ofColor::white);
    thresholdImage.draw(0, 0);  
    for (int i = 0; i < contourFinder.nBlobs; i++){
        ofxCvBlob& blob = contourFinder.blobs[i];
        blob.draw(0, 0);
    }
    
    ofSetColor(ofColor::red);
    ofDrawBitmapString("nBlobs="+ofToString(contourFinder.nBlobs), 10, 15);
}

Viewing all articles
Browse latest Browse all 40524

Trending Articles