質問

+1I have a 2D array in which I need to calculate the mean/average and variance. Problem is that when I do it, I get number bigger than I should. IE: My max value in my 2D array is 256 however I can end up with averages as high as 303.

As such, it is clear that I am calculating mean and variance incorrectly. Can someone tell me where I am going wrong here? Code in entirety below.

Here is one of the files if you would like to try and compile and see the results: http://shawndibble.com/baboon.pgma

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int THRESHOLD = 2048; //random threshold variable to determine when to replace a row.

void optimizeImage(int (*imageFile)[512], int mincol, int maxcol, int row) {
        int sum, average;
    double temp, variance, elements;

        elements = (maxcol-mincol+1)*row;

        if(mincol != maxcol)    { // if maxcol and mincol are the same. We are on a single row & don't need to do anything.
            // Figure out average
            for (int i = mincol; i < maxcol; i++) {
                    for (int j = 0; j < row; j++) {                     
                            sum += imageFile[i][j];
                    }
            }       
            average = sum / elements;

            // figure out variance
            for (int i = mincol; i < maxcol; i++) {
                    for (int j = 0; j < row; j++) {
                            temp += (imageFile[i][j] - average) * (imageFile[i][j] - average);      
                    }
            }   
            variance = temp / elements; //calculate variance

            //cout << "var: " << variance << " thresh: " << THRESHOLD << endl;
            cout << "rows : " << maxcol-mincol+1 << " | average: " << average << endl;

            // if threshold is higher than variance, replace entire row with average
            if(THRESHOLD >= variance) {
                    for(int i = mincol; i < maxcol; i++) {                              // if quad variance is < THRESHOLD, write whole quad to output array
                            for (int j = 0; j < row; j++) {
                                    imageFile[i][j] = average;
                            }
                    }
                    cout << "run average" << endl;

            // otherwise break in half and repeat.
            } else {
                    int mid = ((maxcol+mincol)/2) ;
                    optimizeImage(imageFile, mincol, mid, row);
                    optimizeImage(imageFile, mid+1 , maxcol, row);
            }
        }   
}

int main() {
    ifstream inFile;
    inFile.open ("baboon.pgma");

    if (!inFile) {
        cout << inFile << " is a not working for me.";
    } else {
        //time to read the file, 
        string line1, line2;
        int row, col, maxval;
        getline (inFile, line1);
        getline (inFile, line2);
        inFile >> row;
        inFile >> col;
        inFile >> maxval;
        int imageFile [512][512];


        // read each integer and place it into a 2D array
        for (int i=0; i < col ; i++) {
            for (int j=0; j<row; j++){
                inFile >> imageFile[i][j];
            }
        } 
        inFile.close();

        ofstream dataOut;
        dataOut.open ("BaboonOptimized.pgma");
        dataOut << line1 << endl << line2 << endl << row << " " << col << endl << maxval << endl; 

        optimizeImage(imageFile, 0, col, row);

        for (int i=0; i < col ; i++) {
            for (int j=0; j<row; j++){
            dataOut << imageFile[i][j] << " ";
        }
    }
    dataOut.close();
    }

    return 0;
};
役に立ちましたか?

解決

You have not initialized sum and temp.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top