Reputation: 3125
I have a function which returns an opencv Mat, with 4 rows and 1 column. It runs in a for loop, iterating between two and 100 times.
What I need, is to copy the returned column to a new Mat, so that each iteration adds a new column.
I have (pseudo):
cv::Mat ResultMat(4, 1, CV_64F);
Mat returnMat(4, 1, CV_64F);
for (int i = 0; i < iterations; i++)
{
returnMat= (function that returns a 4x1 Mat);
returnMat.col(0).copyTo(ResultMat.col(0));
}
which works fine, but overwrites the Resultmat column each time.
Replacing the last line with
returnMat.col(0).copyTo(ResultMat.col(i));
causes an exception error.
EDIT: If I create the ResultMat as 'Resultmat(4, 100, CV_64F)', it works, but I need it to self-adjust the number of columns...
How can I add columns to the ResultMat dynamically, one for each loop?
Upvotes: 1
Views: 2042
Reputation: 1459
Not sure why it didn't work for you when you changed the size of ResultMat. But here's a solution, given that you know the number of iterations, which is bascially the number of columns in the end result. I defined a function func() to demonstrate the function called inside the loop.
Mat func() {
Mat m(4, 1, CV_64F);
randn(m, 0., 1.); // fill with random values.
return m;
}
int main() {
int iterations = 3;
cv::Mat result(4, iterations, CV_64F);
for (int i = 0; i < iterations; i++) {
Mat m = func();
m.col(0).copyTo(result.col(i));
}
return 0;
}
Upvotes: 4