测试像素是否在of ofOpenCV的blob内

时间:2011-03-19 08:03:11

标签: openframeworks

我正在应用躲避球的概念,并且需要测试球的像素是否在blob捕获中(这是玩家的图像)。我陷入困境,已经没有了如何实现它的想法。

我设法做了一些有斑点的进展,但我不确定如何测试它?

请帮忙。

我是一个处于绝望状态的新手。

这是我的一些代码。

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(){

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


}

2 个答案:

答案 0 :(得分:2)

c ++中的经典代码如下; (pno是blob编号,x和y是要检查的点)

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

答案 1 :(得分:0)

编写简单的UI可以帮助测试这样的算法。在屏幕上绘制斑点,然后移动鼠标以测试您的算法:如果您的算法说鼠标指针下的像素应该在blob内,请使用ofDrawBitmapString在屏幕上打印一条消息。