Reputation: 534
I am writing a opencv code for particle filter but i am getting some error, i think it is because of the below code.How can i initialise the Mat array at the time of declaration itself?
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;
}
Upvotes: 0
Views: 119
Reputation: 4074
Why don't you use 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;
}
We have used following constructor:
explicit vector (size_type n, const value_type& val = value_type(), const allocator_type& alloc = allocator_type());
which Constructs a container with n elements. Each element is a copy of val. (reference)
And yes, you had a trouble with previous attempt due to built-in array of Mat's objects does not initialize them to proper sizes, what vector's constructor does.
Upvotes: 2