Openframeworks Colour Tracking

1.2k Views Asked by At

I'm working with OpenCV within Openframeworks in order to track a certain colour. My question may be difficult if you are not familiar with the colour tracking code but I'll try to explain the best I can.

What the code does now is follow a certain colour with a red circle and I am working to create a line that does basically the same thing but each point will be stored so that a squiggly type of drawing application is created. Right now it's a straight line that you can pull.

I'll post more code if necessary. Any advice would really help. Thanks!

tespApp.cpp

void testApp::draw(){

ofSetColor(255,255,255);
//draw coloured cv image
rgb.draw(0,0);
contours.draw(0,480);

// draw line that follows the blobs

    for (int i=0; i<contours.nBlobs; i++) {
        ofSetColor(0);
       ofLine( contours.blobs[i].pos.x, contours.blobs[i].pos.y, contours.blobs[i].lastpos.x, contours.blobs[i].lastpos.y );

   }


}

ofxCvContourFinder.cpp


for( int i = 0; i < MIN(nConsidered, (int)cvSeqBlobs.size()); i++ ) {
    blobs.push_back( ofxCvBlob() );
    float area = cvContourArea( cvSeqBlobs[i], CV_WHOLE_SEQ, bFindHoles ); // oriented=true for holes
    CvRect rect = cvBoundingRect( cvSeqBlobs[i], 0 );
    cvMoments( cvSeqBlobs[i], myMoments );

    blobs[i].area                     = bFindHoles ? fabs(area) : area; // only return positive areas
    blobs[i].length                   = cvArcLength(cvSeqBlobs[i]);
    blobs[i].boundingRect.x           = rect.x;
    blobs[i].boundingRect.y           = rect.y;
    blobs[i].boundingRect.width       = rect.width;
    blobs[i].boundingRect.height      = rect.height;
    blobs[i].centroid.x               = (myMoments->m10 / myMoments->m00);
    blobs[i].centroid.y               = (myMoments->m01 / myMoments->m00);
             blobs[i].pos.x                    =0;
             blobs[i].pos.y                    =0;
             blobs[i].lastpos.x                = blobs[i].pos.x;
             blobs[i].lastpos.y                = blobs[i].pos.y;
             blobs[i].pos.x                    =(myMoments->m10 / myMoments->m00);
             blobs[i].pos.y                    = (myMoments ->m01 / myMoments->m00);
1

There are 1 best solutions below

0
On

A complete example of what you are trying to achieve can be found in here:

https://github.com/kylemcdonald/ofxCv/tree/master/example-contours-color

It uses ofxCv, an add-on that let you integrate openframeworks with openCv in a clean way. Moreover you can use native OpenCV calls, without the need of any kind of wrapper. The internet is full of similar examples that uses pure OpenCV code and with this add ons you can use those snippets as it is, without any kind of problem and it gives some easy way to go from OpenCV data structures to Openframeworks ones and vice-versa. It is great!