mercredi 12 juillet 2017

Drawing Trail of Tracked Blob OpenCv

I'm trying to draw the trail of a motion tracked blob in OpenCV but having some problems.

I'm using contourFinder to find a blob, setting a threshold and using erode to limit light sources so that when I shine a torch / laser it becomes the only detected blob.

I've used centroid to draw a blue circle where the centre of the blob is, and using centroid I can therefore get the x, y co-ordinates of the blob.

However, I can't get it to draw a line between the previous points with the current points when moving the tracked object and i'm not sure if i'm missing something or doing something wrong in my code? Specifically the ofDrawLine call.

I'm trying to achieve the same effect of drawing on screen that you would with mouse dragged like ofDrawLine(x, y, prevX, prevY);

This is my ofApp.cpp file

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

    bLearnBackground = false;

    vidGrabber.setVerbose(true);
    vidGrabber.initGrabber(320,240);

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

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

void ofApp::update(){
    vidGrabber.update();
    //do we have a new frame?
    if (vidGrabber.isFrameNew()){
        colorImg.setFromPixels(vidGrabber.getPixels());
        colorImg.mirror(false, true); // mirrors the camera feed so
        grayImage = colorImg; // convert our color image to a grayscale image
        grayBlur = grayImage;
        grayBlur.blurGaussian(21); // smooth edges of image with Gaussian blur
    if (bLearnBackground == true) {
        grayBg = grayBlur; // update the background image
        bLearnBackground = false;
    }

    // Find difference of the frame and background
    grayDiff.absDiff(grayBg, grayBlur);
    // Thresholding for obtaining binary image
    grayDiff.threshold(200);
    grayDiff.erode();

    // findContours(ofxCvGrayscaleImage &input, int minArea, int maxArea, int nConsidered, bool bFindHoles, bool bUseApproximation=true)
    contourFinder.findContours(grayDiff, 5, (340*240)/4, 2, false, true);
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    ofSetHexColor(0xffffff);
    colorImg.draw(0, 0, 320, 240);                  // draws original feed
    grayDiff.draw(0, 240, 320, 240);                // draws greyscale feed
    ofDrawRectangle(320, 0, 320, 240);              // draws recatngle feed on threshold
    contourFinder.draw(320, 0, 320, 240);           // draws only contours rectangles on white background
    ofColor red(255, 255, 255);
    ofColor blue(0, 0, 255);

    // for all the blobs found, draw a bounding rectangle around them
    for(int i = 0; i < contourFinder.nBlobs; i++) {

   //------------ Draw center of blob as blue circle ------------------------ //

        ofPoint centroid = contourFinder.blobs.at(i).centroid;
        centroid.x += 320; centroid.y += 240;  // sets bounds for centroid drawing, same as rectangle
        ofSetColor(blue);
        ofDrawCircle(centroid, 10);

    //------------ Draw trail of moving blobs  ------------------------ //

        ofDrawLine(centroid.x, centroid.y, (centroid.x - 1), (centroid.y - 1));

    // or use ofDrawLine(<#const ofPoint &p1#>, <#const ofPoint &p2#>); ?

    }
}

Aucun commentaire:

Enregistrer un commentaire