我正在编写一个用于粒子滤波器的opencv代码,但是我得到了一些错误,我认为这是因为下面的代码。如何在声明本身时初始化Mat数组?
Mat particle[N];
for (int i = 0; i < N; i++)
{
particle[i].at<float>(0, 0) = rec.x + distribution(generator);//x
particle[i].at<float>(1, 0) = rec.y + distribution(generator);//y
particle[i].at<float>(2, 0) = 0;//vel(x)
particle[i].at<float>(3, 0) = 0;//vel(y)
particle[i].at<float>(4, 0) = min(0.66 + abs(rng.uniform(0, 1)), MAX_a);//a
particle[i].at<float>(5, 0) = rec.height + 10 + rng.uniform(0, 1);//vel(h)
weight[i] = 1.0 / N;
}
答案 0 :(得分:2)
为什么不使用std :: vector:
std::vector<cv::Mat> particle(N, cv::Mat(6, 1, CV_32F)); // constructor which takes
// number of elems and allocates memory of them using second argument as a prototype object
for (int i = 0; i < N; i++)
{
particle[i].at<float>(0, 0) = rec.x + distribution(generator);//x
particle[i].at<float>(1, 0) = rec.y + distribution(generator);//y
particle[i].at<float>(2, 0) = 0;//vel(x)
particle[i].at<float>(3, 0) = 0;//vel(y)
particle[i].at<float>(4, 0) = min(0.66 + abs(rng.uniform(0, 1)), MAX_a);//a
particle[i].at<float>(5, 0) = rec.height + 10 + rng.uniform(0, 1);//vel(h)
weight[i] = 1.0 / N;
}
我们使用了以下构造函数:
显式向量(size_type n,const value_type&amp; val = value_type(), const allocator_type&amp; alloc = allocator_type());
其中构造一个包含n个元素的容器。每个元素都是val的副本。(reference)
是的,你之前的尝试遇到了麻烦,因为内置的Mat对象数组没有将它们初始化为正确的大小,是什么向量的构造函数。