0

I've been looking through the similar questions and they don't really help me. I clicked the link to see the results and double checked my code but it just doesn't seem to want to work. Here's my code:

void blur(int height, int width, RGBTRIPLE image[height][width])

{

//probably super redundant but to store the original values in
//a separate array that we can access
RGBTRIPLE orgArr[height][width];

for(int i = 0; i < height; i++){
    for(int j = 0; j < width; j++){
        orgArr[i][j] = image[i][j];        
    }
}

//now onto the actual array
int numElements;

for(int i = 0; i < height; i++){
    for(int j = 0; j < width; j++){
        //we need to look at all pixels within a 1 row/1 column basis and get the
        //average of their color values and assign to the middle pixel
        float avgRed;
        float avgGreen;
        float avgBlue;
        
        if(i > 0 && j > 0 && i < height - 1 && j < width - 1){
            //in the middle
            numElements = 9;
            avgRed = orgArr[i - 1][j - 1].rgbtRed + orgArr[i - 1][j].rgbtRed + orgArr[i - 1][j + 1].rgbtRed + orgArr[i][j - 1].rgbtRed + orgArr[i][j].rgbtRed + orgArr[i][j + 1].rgbtRed + orgArr[i + 1][j - 1].rgbtRed + orgArr[i + 1][j].rgbtRed + orgArr[i + 1][j + 1].rgbtRed;
            avgGreen = orgArr[i - 1][j - 1].rgbtGreen + orgArr[i - 1][j].rgbtGreen + orgArr[i - 1][j + 1].rgbtGreen + orgArr[i][j - 1].rgbtGreen + orgArr[i][j].rgbtGreen + orgArr[i][j + 1].rgbtGreen + orgArr[i + 1][j - 1].rgbtGreen + orgArr[i + 1][j].rgbtGreen + orgArr[i + 1][j + 1].rgbtGreen;
            avgBlue = orgArr[i - 1][j - 1].rgbtBlue + orgArr[i - 1][j].rgbtBlue + orgArr[i - 1][j + 1].rgbtBlue + orgArr[i][j - 1].rgbtBlue+ orgArr[i][j].rgbtBlue + orgArr[i][j + 1].rgbtBlue + orgArr[i + 1][j - 1].rgbtBlue + orgArr[i + 1][j].rgbtBlue + orgArr[i + 1][j + 1].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)(round(avgRed));
            image[i][j].rgbtGreen = (int)(round(avgGreen));
            image[i][j].rgbtBlue = (int)(round(avgBlue));
        }
        else if(i == 0 && j == 0){
            //corner case 1
            numElements = 4;
            avgRed = orgArr[i][j].rgbtRed + orgArr[i + 1][j + 1].rgbtRed + orgArr[i][j + 1].rgbtRed + orgArr[i + 1][j].rgbtRed;
            avgGreen = orgArr[i][j].rgbtGreen + orgArr[i + 1][j + 1].rgbtGreen + orgArr[i][j + 1].rgbtGreen + orgArr[i + 1][j].rgbtGreen;
            avgBlue = orgArr[i][j].rgbtBlue + orgArr[i + 1][j + 1].rgbtBlue + orgArr[i][j + 1].rgbtBlue + orgArr[i + 1][j].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)(round(avgRed));
            image[i][j].rgbtGreen = (int)(round(avgGreen));
            image[i][j].rgbtBlue = (int)(round(avgBlue));
        }
        else if(i == 0  && j == width - 1){
            //corner case 2
            numElements = 4;
            avgRed = orgArr[i][j - 1].rgbtRed + orgArr[i][j].rgbtRed + orgArr[i + 1][j - 1].rgbtRed + orgArr[i + 1][j].rgbtRed;
            avgGreen = orgArr[i][j - 1].rgbtGreen + orgArr[i][j].rgbtGreen + orgArr[i + 1][j - 1].rgbtGreen + orgArr[i + 1][j].rgbtGreen;
            avgBlue = orgArr[i][j - 1].rgbtBlue + orgArr[i][j].rgbtBlue + orgArr[i + 1][j - 1].rgbtBlue + orgArr[i + 1][j].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)avgRed;
            image[i][j].rgbtGreen = (int)avgGreen;
            image[i][j].rgbtBlue = (int)avgBlue;                                                                                                                                                                                                                                                                                                                                                                  
        }
        else if(i == height - 1 && j == 0){
            //corner case 3
            numElements = 4;
            avgRed = orgArr[i - 1][j].rgbtRed + orgArr[i - 1][j + 1].rgbtRed + orgArr[i][j].rgbtRed + orgArr[i][j + 1].rgbtRed;
            avgGreen = orgArr[i - 1][j].rgbtGreen + orgArr[i - 1][j + 1].rgbtGreen + orgArr[i][j].rgbtGreen + orgArr[i][j + 1].rgbtGreen;
            avgBlue = orgArr[i - 1][j].rgbtBlue + orgArr[i - 1][j + 1].rgbtBlue + orgArr[i][j].rgbtBlue + orgArr[i][j + 1].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)avgRed;
            image[i][j].rgbtGreen = (int)avgGreen;
            image[i][j].rgbtBlue = (int)avgBlue;
        }
        else if(i == height - 1 && j == width - 1){
            //corner case 4
            numElements = 4;
            avgRed = orgArr[i][j].rgbtRed + orgArr[i][j - 1].rgbtRed + orgArr[i - 1][j - 1].rgbtRed + orgArr[i - 1][j].rgbtRed;
            avgGreen = orgArr[i][j].rgbtGreen + orgArr[i][j - 1].rgbtGreen + orgArr[i - 1][j - 1].rgbtGreen + orgArr[i - 1][j].rgbtGreen;
            avgBlue = orgArr[i][j].rgbtBlue + orgArr[i][j - 1].rgbtBlue + orgArr[i - 1][j - 1].rgbtBlue + orgArr[i - 1][j].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)avgRed;
            image[i][j].rgbtGreen = (int)avgGreen;
            image[i][j].rgbtBlue = (int)avgBlue;
        }
        else if(j == 0){
            numElements = 6;
            avgRed = orgArr[i - 1][j].rgbtRed + orgArr[i][j].rgbtRed + orgArr[i + 1][j].rgbtRed + orgArr[i - 1][j + 1].rgbtRed + orgArr[i][j + 1].rgbtRed + orgArr[i + 1][j + 1].rgbtRed;
            avgGreen = orgArr[i - 1][j].rgbtGreen + orgArr[i][j].rgbtGreen + orgArr[i + 1][j].rgbtGreen + orgArr[i - 1][j + 1].rgbtRed + orgArr[i][j + 1].rgbtGreen+ orgArr[i + 1][j + 1].rgbtGreen;
            avgBlue = orgArr[i - 1][j].rgbtBlue + orgArr[i][j].rgbtBlue + orgArr[i + 1][j].rgbtBlue+ orgArr[i - 1][j + 1].rgbtBlue+ orgArr[i][j + 1].rgbtBlue + orgArr[i + 1][j + 1].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)(round(avgRed));
            image[i][j].rgbtGreen = (int)(round(avgGreen));
            image[i][j].rgbtBlue = (int)(round(avgBlue));
        }
        else if(j == width - 1){
            numElements = 6;
            avgRed = orgArr[i - 1][j].rgbtRed + orgArr[i][j].rgbtRed + orgArr[i + 1][j].rgbtRed + orgArr[i - 1][j - 1].rgbtRed + orgArr[i][j - 1].rgbtRed + orgArr[i + 1][j - 1].rgbtRed;
            avgGreen = orgArr[i - 1][j].rgbtGreen + orgArr[i][j].rgbtGreen + orgArr[i + 1][j].rgbtGreen + orgArr[i - 1][j - 1].rgbtRed + orgArr[i][j - 1].rgbtGreen+ orgArr[i + 1][j - 1].rgbtGreen;
            avgBlue = orgArr[i - 1][j].rgbtBlue + orgArr[i][j].rgbtBlue + orgArr[i + 1][j].rgbtBlue+ orgArr[i - 1][j - 1].rgbtBlue+ orgArr[i][j - 1].rgbtBlue + orgArr[i + 1][j - 1].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)(round(avgRed));
            image[i][j].rgbtGreen = (int)(round(avgGreen));
            image[i][j].rgbtBlue = (int)(round(avgBlue));
        }
        else if(i == 0){
            numElements = 6;
            avgRed = orgArr[i][j + 1].rgbtRed + orgArr[i][j].rgbtRed + orgArr[i][j - 1].rgbtRed + orgArr[i + 1][j - 1].rgbtRed + orgArr[i + 1][j].rgbtRed + orgArr[i + 1][j + 1].rgbtRed;
            avgGreen = orgArr[i][j + 1].rgbtGreen + orgArr[i][j].rgbtGreen + orgArr[i][j - 1].rgbtGreen + orgArr[i + 1][j - 1].rgbtGreen+ orgArr[i + 1][j].rgbtGreen + orgArr[i + 1][j + 1].rgbtGreen;
            avgBlue = orgArr[i][j + 1].rgbtBlue + orgArr[i][j].rgbtBlue + orgArr[i][j - 1].rgbtBlue + orgArr[i + 1][j - 1].rgbtBlue + orgArr[i + 1][j].rgbtBlue + orgArr[i + 1][j + 1].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)(round(avgRed));
            image[i][j].rgbtGreen = (int)(round(avgGreen));
            image[i][j].rgbtBlue = (int)(round(avgBlue));
        }
        else if(i == height - 1){
            numElements = 6;
            avgRed = orgArr[i][j - 1].rgbtRed + orgArr[i][j].rgbtRed + orgArr[i][j + 1].rgbtRed + orgArr[i - 1][j - 1].rgbtRed + orgArr[i - 1][j].rgbtRed + orgArr[i - 1][j + 1].rgbtRed;
            avgGreen = orgArr[i][j - 1].rgbtGreen + orgArr[i][j].rgbtGreen + orgArr[i][j + 1].rgbtGreen + orgArr[i - 1][j - 1].rgbtGreen + orgArr[i - 1][j].rgbtGreen + orgArr[i - 1][j + 1].rgbtGreen;
            avgBlue = orgArr[i][j - 1].rgbtBlue + orgArr[i][j].rgbtBlue + orgArr[i][j + 1].rgbtBlue + orgArr[i - 1][j - 1].rgbtBlue + orgArr[i - 1][j].rgbtBlue + orgArr[i - 1][j + 1].rgbtBlue;
            
            avgRed = round(avgRed / numElements);
            avgGreen = round(avgGreen / numElements);
            avgBlue = round(avgBlue / numElements);
            image[i][j].rgbtRed = (int)avgRed;
            image[i][j].rgbtGreen = (int)avgGreen;
            image[i][j].rgbtBlue = (int)avgBlue;
        }
    }
}
return;

}

and then I get as output through check50:

:( blur correctly filters 3x3 image expected "70 85 95\n80 9...", not "70 85 95\n80 9..." :( blur correctly filters 4x4 image expected "70 85 95\n80 9...", not "70 85 95\n80 9..."

For more output on the exact thing I got wrong it says something like: Expected output: ... ...117 130 140 Actual output: ... ... 117 128 140 This only happens for 2 lines on the 3x3 area and 4 for the 4x4. Is it a rounding error?

1
  • Could you please include the exact lines of problem.
    – Vsjain
    Sep 16 '20 at 12:46
0

Under j == 0 and j == width - 1 I had a .rgbtRed instead of a .rgbtGreen.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .