2011-03-19 72 views
0

我正在做一個躲避球的概念的應用程序,並且需要測試球的像素是否在blob捕獲(這是玩家的圖像)。我被卡住了,並已經用完了如何實現它的想法。測試像素是否在blob中forxOpenCV

我設法做了一些進步,有斑點但我不知道如何測試它?

請幫忙。

我是一個處於絕望狀態的新手。

這是我的一些代碼。

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


} 

回答

0

編寫一個簡單的UI可以幫助測試像這樣的算法。在屏幕上繪製斑點,然後移動鼠標來測試你的算法:如果你的算法說鼠標指針下面的像素應該在斑點內,用ofDrawBitmapString在屏幕上打印一條消息。

2

C++中的經典代碼如下; (pno是斑點號,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; } 
相關問題