0
votes

I am trying to find the average of a matrix. I have created a 4800 x 5 matrix and now would like to make it into a 4800 x 1 matrix by finding average of each row in the matrix. I know this means that I add together all the elements in each row and divide by 5. I am considering the function cvAvg but so far, I cannot find an example of its use. Below is my code in which I converted 5 images to matrices of 4800 x 1 and combined them to create the super matrix of 4800 x 5. The next step is to find the average of the super matrix. Please, I would appreciate it greatly if someone can help me.

1
What exactly is stopping you from dividing the sum by 5? seems like a long way for a short cut...James Allan
Do you mean to add up the row elements and divine by 5 individually, instead of using cvAvg?sue-ling

1 Answers

0
votes

Unless I'm not understanding something can't you just do:

CvMat* avgMat = cvCreateMat(4800, 1, CV_8UC1);

for(int i = 0; i < 4800; ++i)
{
    CV_MAT_ELEM( *avgMat , float, i, 0 ) = ( CV_MAT_ELEM( *col0, float, i, 0 ) + 
                                             CV_MAT_ELEM( *col1, float, i, 0 ) + 
                                             CV_MAT_ELEM( *col2, float, i, 0 ) + 
                                             CV_MAT_ELEM( *col3, float, i, 0 ) +
                                             CV_MAT_ELEM( *col4, float, i, 0 )
                                            ) / 5;
}

If you used cvAvg() you'd need a custom mask for each element averaged which seems like a really round about way of doing the same thing.