我正在做一个项目,在OpenCV上估计使用捕获的视频移动车辆的速度。这里的相机是静止的。我用质心和欧几里得距离估计了单个物体的速度。现在的问题是,我没有得到如何为多个对象做同样的事情。这里,我需要计算2个连续帧之间的物体的欧几里德距离。 我很感激,如果有人会帮助。使用C++跟踪openCV中的多个对象
我创建了讲座
class centroids
{
public:
vector<Point2f> ce;
vector<float> area;
};
centroids c[100];
这是我写的代码。我将不胜感激,如果有人帮我用代码:
findContours(fgMaskMOG2,
contours,
hierarchy,
CV_RETR_CCOMP,
CV_CHAIN_APPROX_SIMPLE);
int morph_size = 6;
Mat element = getStructuringElement(MORPH_RECT,
Size(2*morph_size+1, 2*morph_size+1),
Point(morph_size, morph_size));
Scalar color(255, 255, 255); // color of the contour in the
//Draw the contour and rectangle
for(int i = 0; i < contours.size(); i++)
{
drawContours(fgMaskMOG2,
contours,
i,
color,
CV_FILLED,
8,
hierarchy);
}
//imshow("morpho window",dst);
vector<Moments> mu(contours.size());
vector<Point2f> mc(contours.size());
vector<Point2f> m ;
vector<double> time;
vector<Point2f> centroid(mc.size());
//vector< vector<Point> >::iterator itc = contours.begin();
// iterate through each contour.
double time1[1000];
for(int i = 0; i < contours.size(); i++)
{
// Find the area of contour
double a = contourArea(contours[i], false);
if(a > 500)
{
mu[i] = moments(contours[i], false);
mc[i] = Point2f((mu[i].m10/mu[i].m00), (mu[i].m01/mu[i].m00));
m.push_back(mc[i]);
Point2f diff;
double euclidian = 0;
for(int f = 0; f < m.size(); f++)
{
if(k == 1)
{
c[f].ce.push_back(m[f]);
cout << "cen" << c[f].ce << endl;
euclidian = 0;
}
else
{
c[f+1].ce.push_back(m[f]);
cout << "cent" << c[f+1].ce << endl;
diff = c[f].ce[f] - c[f-1].ce[f-1];
euclidian = abs(sqrt((diff.x*diff.x) + (diff.y*diff.y)));
cout << "euclidian" << euclidian << endl;
}
}
cout << "\n centroid" << m << endl;
circle(fgMaskMOG2,
mc[i],
5,
Scalar(0, 0, 255),
1,
8,
0);
}
}
感谢提前:)
如果它们正在水平移动(或其他)但距离相机相同的距离(最终是这样,因为您没有立体视觉),那么计算轮廓面积a和最佳拟合方式意味着相同的对象 –
您需要找出一个算法来跟踪连续帧之间的单个车辆(即找到对应于同一对象的对)。对于灵感,你可以看看我在[这个问题]中做了什么(http://stackoverflow.com/questions/36254452/counting-cars-opencv-python-issue/36274515#36274515) –