I have some images that I want to calculate mean value of each pixel of all images. Lets say I have 5 image of size 3x3. I read all pixels of each image and put them in row of a Mat object so at the end I have a 5x15 Mat. Using c++ code it is easy (I have a Vector<Vector<double>>
)
std::for_each(data->begin(), data->end(),
[&](const std::vector<uchar>& row)
{
// Use transform overload that takes two input ranges.
// Note that colsums is the second input range as well as the output range.
// We take each element of the row and add it to the corresponding
// element of colsums vector:
std::transform(row.begin(), row.end(), colSums.begin(), colSums.begin(),
[](double d1, double d2) { return d1 + d2; });
});
but how I can use opencv itself to do this? I want sum of each column divided by number of images to form a new Mat. I tried the code below but it does not work gives only a 1x1 mat!!!
void BayesianClassifier::createAggregateFromTrainingVector(pr::training_vector tv)
{
//building a Mat object from vector pointer
cv::Mat mat(tv.size(), tv.at(0).size(), CV_8UC1);
int rows = mat.rows;
int cols = mat.cols;
for (int r = 0; r < rows; ++r) {
uchar *pInput = mat.ptr<uchar>(r);
for (int c = 0; c < cols; ++c) {
*pInput = tv.at(r)[c];
++pInput;
}
}
mData = mat;
cv::meanStdDev(mData, mMatMean, mMatVariance);
}