开发者

Test whether pixel is inside the blobs for ofxOpenCV

开发者 https://www.devze.com 2023-02-17 15:49 出处:网络
I am doing an application of the concept of the dodgeball, and need to test if the pixel of the ball is in the blobs capture (which is the image of the player). I am stuck and have run out of ideas of

I am doing an application of the concept of the dodgeball, and need to test if the pixel of the ball is in the blobs capture (which is the image of the player). I am stuck and have run out of ideas of how to implement it.

I managed to do a little progress which have the blobs but I'm not sure how to test it?

Please help.

I am a newbie who in a desperate condition.

This is some of my code.

void testApp::setup(){


    #ifdef _USE_LIVE_VIDEO
        vidGrabber.setVerbose(true);
        vidGrabber.initGrabber(widthS,heightS);
    #else
        vidPlayer.loadMovie("fingers.mov");
        vidPlayer.play();
    #endif
    widthS = 320;
    heightS = 240;
    colorImg.allocate(widthS,heightS);
    grayImage.allocate(widthS,heightS);
    grayBg.allocate(widthS,heightS);
    grayDiff.allocate(widthS,heightS); ////<---what I want

    bLearnBakground = true;
    threshold = 80;

    //////////circle//////////////
    counter = 0;
    radius = 0;
    circlePosX = 100;
    circlePosY=200;


}


void testApp::update(){

ofBackgro开发者_如何学编程und(100,100,100);

    bool bNewFrame = false;

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

    if (bNewFrame){

        if (bLearnBakground == true){
            grayBg = grayImage;     // the = sign copys the pixels from grayImage into grayBg (operator overloading)
            bLearnBakground = false;
        }

        #ifdef _USE_LIVE_VIDEO
            colorImg.setFromPixels(vidGrabber.getPixels(),widthS,heightS);
        #else
            colorImg.setFromPixels(vidPlayer.getPixels(),widthS,heightS);
        #endif

        grayImage = colorImg;

        grayDiff.absDiff(grayBg, grayImage);
        grayDiff.threshold(threshold);

        contourFinder.findContours(grayDiff, 20, (340*240)/3, 10, true);    // find holes
    }



    ////////////circle////////////////////
    counter = counter + 0.05f;
    if(radius>=50){
        circlePosX = ofRandom(10,300);
        circlePosY = ofRandom(10,230);
    }
    radius = 5 + 3*(counter);

}


void testApp::draw(){

    // draw the incoming, the grayscale, the bg and the thresholded difference
    ofSetColor(0xffffff); //white colour
    grayDiff.draw(10,10);// draw start from point (0,0);
    // we could draw the whole contour finder

    // or, instead we can draw each blob individually,
    // this is how to get access to them:
    for (int i = 0; i < contourFinder.nBlobs; i++){
        contourFinder.blobs[i].draw(10,10);

    }

    ///////////////circle//////////////////////////
    //let's draw a circle:
    ofSetColor(0,0,255);
    char buffer[255];
    float a = radius;
    sprintf(buffer,"radius = %i",a);
    ofDrawBitmapString(buffer, 120, 300);

    if(radius>=50)
     {
        ofSetColor(255,255,255);
        counter = 0;
     }
     else{

        ofSetColor(255,0,0);

     }
     ofFill();
     ofCircle(circlePosX,circlePosY,radius);


}


The classic code in c++ is below; (pno is blob number, x and y is the point to check)

bool testApp::pointInPolygon(int pno,int x, int y) {

int      i, j=blobTracker.blobs[pno].pts.size()-1 ;
bool  oddNodes= false;

for (i=0; i<blobTracker.blobs[pno].pts.size(); i++) {
    if (blobTracker.blobs[pno].pts[i].y<y && blobTracker.blobs[pno].pts[j].y>=y
        ||  blobTracker.blobs[pno].pts[j].y<y && blobTracker.blobs[pno].pts[i].y>=y) {
        if (blobTracker.blobs[pno].pts[i].x+(y-blobTracker.blobs[pno].pts[i].y)/(blobTracker.blobs[pno].pts[j].y-blobTracker.blobs[pno].pts[i].y)*(blobTracker.blobs[pno].pts[j].x-blobTracker.blobs[pno].pts[i].x)<x) {
            oddNodes=!oddNodes; }}
    j=i; }

return oddNodes; }


Coding up a simple UI can help for testing algorithms like this. Draw the blob on the screen and then move the mouse around to test your algorithm: if your algorithm says that pixel under the mouse pointer should be inside a blob, print a message saying so on the screen using ofDrawBitmapString.

0

精彩评论

暂无评论...
验证码 换一张
取 消