2014-03-31 25 views
0

我跟踪两个对象,并且当它们在彼此100内时想保存一帧。 进出口使用测量距离:当对象靠近时保存一个帧到文件opencv

bool bSavePic = false; 

void example{ 
    int x1,y1,x2,y2; 
int distance; //distance between two objects 

if(initialMarkers.size()>1){ 
x1 = initialMarkers.at(0).getXPos(); 
y1 = initialMarkers.at(0).getYPos(); 
x2 = initialMarkers.at(1).getXPos(); 
y2 = initialMarkers.at(1).getYPos(); 

distance = (int)sqrt((double)((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))); 

cv::putText(cameraFeed,intToString(distance),cv::Point(50,50),1,1,Scalar(255,0,0)); 

    if (distance < 100) 
     { 
      bSavePic= true; 
      }else{ 
      bSavePic= false; 
     } 
    } 

时遇到的问题是,当它们在彼此的下面的代码输出视频流100中,然后保存该帧时的标记物距离大于100再次。

if (bSavePic == true) 
     { 
      putText(cameraFeed,"Saving Image",Point(50,70),1,1,Scalar(0,0,255),1); 
      waitKey(10); 
      capture >> saveImage; 
     } 

    char buffer[1000]; 
    for(int c=0; c<1; c++) 
     { 
      sprintf(buffer,"C:\\Users\\Scott\\Documents\\Visual Studio 2010\\Projects\\MultipleObjectTracking\\Image-%d.jpg",c); 
      imwrite(buffer, saveImage);    
     } 

    if(!saveImage.empty()) 
     { 
      Mat readImage; 
      readImage = imread(buffer,CV_LOAD_IMAGE_COLOR); 
      imshow(windowName4, readImage); 
     } 

关于如何尽快保存第一帧中的距离小于100并且仅保存另一个当距离去外面100和该范围内的任何回建议?

c<1,因为它是节约每帧而距离小于100

回答

1

我解决了这个使用

bool bSingleFrame = true; 

int main{ 

     if (bSavePic == true)//distance<100 
    { 

     if (bSingleFrame == true) 
     { 
     putText(cameraFeed,"Saving Image",Point(50,70),1,1,Scalar(0,0,255),1); 
     Mat saveImage; 
     capture >> saveImage; 
      stringstream ssFileName;     //string stream initialised through each passing of while loop 
      ssFileName << "Image-" << c << ".jpg";  //File name based on frame captured 0++ 
      ssFileName >> sFileName; 


     if(!saveImage.empty())       //check if frame captured is stored in matrix 
     { 
      cout << "Frame captured." << endl;   //if data in frame cout 
      imwrite(sFileName.c_str(), saveImage);  //save frame 
      saveImage.release();       
      c++; 
      if (bSavePic == true)      //if distance still <100 
      { 
       bSingleFrame = false; 
      } 

      Mat readImage; 
      readImage = imread(sFileName.c_str(),CV_LOAD_IMAGE_COLOR); 
      imshow(windowName4, readImage);    //show captured frame in new window 
     }else{ 
      cout << "Error, could not capture frame to save." << endl; 
     }//if(!saveImage.empty()) 
     }//bSingleFrame 
    }//ifbSavePic 

    if (bSavePic == false) 
      { 
       bSingleFrame = true;//if distance back >100 can capture another frame 
       } 
} 

注意这仅仅是一个片断。但它确实需要什么。