计算Point2f C ++ OpenCV

时间:2015-08-07 13:56:37

标签: c++ opencv

我最近开始在OpenCV的帮助下编写一些程序,我正在编写一个跟踪叉车移动的应用程序。 我设法做了所有艰难的事情,即。检测叉车当前的位置,现在我正在编写将跟踪屏幕上叉车的部分代码。 我的想法是,我从叉车的轮廓计算质量点,我在最近的帧中搜索最近和最近的质量点,如果我发现它在它们之间划线,并搜索更多。如果我找不到或者它们太远或太旧我会创造新物体(叉车)。

为了证明我的问题,我创建了一个程序,可以在不分析任何框架的情况下执行任务:

#include <stdio.h>

#include "opencv2/core/core.hpp"
#include <opencv2/features2d/features2d.hpp>
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/video/video.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/video/background_segm.hpp>
#include <unistd.h>
#include <iostream>
using namespace std;
using namespace cv;


class Wozek
{
    public:
    string nazwa; //Keeps the name of the object
    int color; // Keeps color
    vector<Point2f> punkty; // Poits of mass detected in frames ( 1st one is the oldest)
    int waznosc;  // How long till the object expire

    int odnow(int x){ //  Refresh the object
     waznosc = x;
     return waznosc;
    }
    int odnow(){
      waznosc = 50;
      return waznosc;
    }

    int klatka(int x){  // Decay the object x times
       waznosc=waznosc-x;
       return waznosc;
     }

     int klatka(){ // Decay only once
       waznosc = waznosc-1;
       return waznosc;
     }

    Point2f dodaj(Point2f x){  // add new center of mass
       punkty.push_back(x);
       return x;
     }

};
bool przeterminowane(const Wozek& value){ return(value.waznosc<1);} // Fuction for deleting decated objects


int main(){
     srand( time( NULL ) );
    list<Wozek > objects; // Create list of objects

while(1==1){

    vector<Point2f> cc;  // New points of mass
for(int i =0;i<5;i++){ // ceating random points of mass

    int x =  (rand()%640) +1;
    int y =  (rand()%480) +1;

    Point2f temp(x,y);
    cc.push_back(temp);


}


objects.remove_if(przeterminowane); // Delete objects that decayed;

    for(int i =0;i<cc.size();i++){ // For each new point of mass
                            // Checking how for the point is from objects last position
                             list<Wozek>::iterator it;
                             double najmniejszy = 0; // lowest lenght
                            Wozek * wnajmniejszy = 0 ; // object with lowest lenght
                            if (objects.size() >0){
                            for(it = objects.begin();it!=objects.end();++it);
                            {

                                Point2f temp = (*it).punkty.back(); // get last position

double wiel ;
                                 wiel = sqrt(((cc[i].x-temp.x)*(cc[1].x-temp.x)) + ((cc[1].y-temp.y)*(cc[1].y-temp.y)));// Wont' work
                                wiel = norm(temp-cc[i]); // Won't work too
printf("%f/n",wiel);
                                if(najmniejszy == 0 or wiel<najmniejszy ) { //if 1st run or wiel is shorter
                                    najmniejszy = wiel; // save smallest value
                                    wnajmniejszy = &(*it); // save ref to object;
                                }
                            }
                            }
//print("%f/n",wiel);
                       if (najmniejszy <100 && najmniejszy!=0)  {
                           printf(" For %d point of mass  object %s is closest at %d\n",i,wnajmniejszy->nazwa.c_str(),najmniejszy); // Print debug msg
                                   wnajmniejszy->punkty.push_back(cc[i]); // Add point of mass to object;
                                   wnajmniejszy->odnow(); // Refresh object so it won't go away
                       }else {  // Couldn't find mass point close enough , create new object
                             Wozek tmp;
                             int color = (rand()%255) +1; // Random color
                             string nazwa;
                             sprintf((char*)nazwa.c_str(), "objects no. %d\n", objects.size()); // Creating name for object
                             tmp.nazwa = nazwa;
                             tmp.color = color;
                             tmp.punkty.push_back(cc[i]);
                             tmp.odnow(); // Setting it fresh and new
                             objects.push_back(tmp); // Saving
                             printf(" For %d point of mass could't find object. Creating new object : %s \n",i,tmp.nazwa.c_str()); // Print debug msg
                       }
    }

    //decay every object
     list<Wozek>::iterator it;
    for(it = objects.begin();it!=objects.end();++it) it->klatka();

    sleep(5);
}
return 0;

}

问题是当我确定两点之间的距离时,名为wiel的变量似乎总是得到-nan值,而且我在第一个FOR中看到了一些对象的存在行为,它似乎即使在对象应该是空的,这就是为什么我放在那里if(objects.size()&gt; 0)。

提前谢谢

在Ubuntu 14.04上编写,Eclipse,Opencv 2.4.9

0 个答案:

没有答案