Ask Your Question

Revision history [back]

Calculating mean value of pixels of many images

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);
}

Calculating mean value of pixels of many images

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(),

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);
}

Calculating mean value of pixels of many images

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)
    {
        std::transform(row.begin(), row.end(), colSums.begin(), colSums.begin(),
                       [](double d1, double d2) { return d1 + d2; });
    });
for (unsigned int i = 0; i < elementsCount; i++) {
    colSums[i] /= dataCount;
}

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);
}

Calculating mean value of pixels of many images

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)
    {
        std::transform(row.begin(), row.end(), colSums.begin(), colSums.begin(),
                       [](double d1, double d2) { return d1 + d2; });
    });
for (unsigned int i = 0; i < elementsCount; i++) {
    colSums[i] /= dataCount;
}

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);
}

UPDATE

I feel that I didn't clarify enough. Imagine the 6x6 matrix below, where each row represents a 2x3 image, and for simplification lets imagine all images are the same! (I call this Mat mData)

{ 1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6 }

I want to get the mean value of each pixel, so at the end I want to show a 3x3 image of the mean values. For this, I need to calculate mean of each column. For the simple case above, for column 1 it will be:

SUM OF COL 1 => 1 + 1 + 1 + 1 + 1 + 1 = 6 
MEAN OF COL 1 => 6/6 = 1

Doing this for all column will result in:

{ 1, 2, 3, 4, 5, 6 }

and converting this single row into 2x3 image:

{ 1, 2, 3
  4, 5, 6 }

So my question is, if I do the following code:

Mat cov, mean;
cv::calcCovarMatrix(mData, cov, mean, CV_COVAR_NORMAL | CV_COVAR_ROWS);

The mean will not be a 1x6 { 1, 2, 3, 4, 5, 6 } matrix like what I theoretically calculated above?

Calculating mean value of pixels of many images

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)
    {
        std::transform(row.begin(), row.end(), colSums.begin(), colSums.begin(),
                       [](double d1, double d2) { return d1 + d2; });
    });
for (unsigned int i = 0; i < elementsCount; i++) {
    colSums[i] /= dataCount;
}

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);
}

UPDATE

I feel that I didn't clarify enough. Imagine the 6x6 matrix below, where each row represents a 2x3 image, and for simplification lets imagine all images are the same! (I call this Mat mData)

{ 1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6
  1, 2, 3, 4, 5, 6 }

I want to get the mean value of each pixel, so at the end I want to show a 3x3 image of the mean values. For this, I need to calculate mean of each column. For the simple case above, for column 1 it will be:

SUM OF COL 1 => 1 + 1 + 1 + 1 + 1 + 1 = 6 
MEAN OF COL 1 => 6/6 = 1

Doing this for all column will result in:

{ 1, 2, 3, 4, 5, 6 }

and converting this single row into 2x3 image:

{ 1, 2, 3
  4, 5, 6 }

So my question is, if I do the following code:

Mat cov, mean;
cv::calcCovarMatrix(mData, cov, mean, CV_COVAR_NORMAL | CV_COVAR_ROWS);

The mean will not be a 1x6 { 1, 2, 3, 4, 5, 6 } matrix like what I theoretically calculated above?