0

I am stuck in Pset4-Filter/less the Blur function in particular. My problem is the image blurred correctly but does not pass check50.

Check50 result :

:( blur correctly filters middle pixel >> expected : 127 140 149 and actual : 147 162 171
:( blur correctly filters pixel on edge >> expected : 80 95 105 and actual : 90 106 116
:( blur correctly filters 3x3 image >> 

Expected Output:
70 85 95
80 95 105
90 105 115
117 130 140
127 140 149
137 150 159
163 178 188
170 185 194
178 193 201
Actual Output:
70 85 95
90 106 116
120 136 146
135 150 160
147 162 171
142 155 163
128 125 132
120 126 125
93 98 122

:( blur correctly filters 4x4 image >> 

Expected Output:
70 85 95
80 95 105
100 115 125
110 125 135
113 126 136
123 136 145
142 155 163
152 165 173
113 119 136
143 151 164
156 166 171
180 190 194
113 112 132
155 156 171
169 174 177
203 207 209
Actual Output:
70 85 95
90 106 116
108 124 134
131 146 156
132 147 156
142 156 166
157 172 180
154 163 177
121 125 141
143 148 162
156 163 169
136 139 171
94 94 98
142 117 140
158 156 164
123 124 179

my code :


void blur(int height, int width, RGBTRIPLE image[height][width])
{
    for( int i = 0; i < height  ; i++)
    {
        for( int j = 0; j < width ; j++)
        {
            // variables to store for new value for each color
            // count valid pixels to count average
            int sumR = 0, sumG = 0, sumB = 0, count = 0;
            // available pixels for each pixel
            int indexs[9][2] = {
                {i-1, j-1}, {i-1, j}, {i-1, j+1},
                {i, j-1}, {i, j}, {i, j+1},
                {i+1, j-1}, {i+1, j}, {i+1, j+1}
            };

            for( int k = 0; k < 9; k++)
            {
                // only add valid pixels considering corner and edge pixels
                if( indexs[k][0] >= 0 && indexs[k][0] <= height && indexs[k][1] >= 0 && indexs[k][1] <= width)
                {
                    // current valid pixel
                    RGBTRIPLE currentImg = image[indexs[k][0]][indexs[k][1]];
                    // add current pixel color values to corresponding color variable
                    sumR += currentImg.rgbtRed;
                    sumB += currentImg.rgbtBlue;
                    sumG += currentImg.rgbtGreen;
                    // increase valid pixels count
                    count++;
                }
            }
            // set new pixel color value
            image[i][j].rgbtRed = round(sumR/ (float) count);
            image[i][j].rgbtBlue = round(sumB/ (float) count);
            image[i][j].rgbtGreen = round(sumG/ (float) count);
        }
    }
}

1

Well, this code definitely blurs the image, but far from correctly. While the "math" may be right (not saying yes or no because I didn't look), there's a more serious logic problem.

To blur the image correctly, the inputs to the math must come from the ORIGINAL pixel values surrounding the pixel that's being blurred. With this code, each pixel is being blurred one by one. Unfortunately, as each pixel is processed, the next pixel is using the calculated blurred numbers from each surrounding pixel that's already been processed. So, each subsequent pixel is being blurred using "corrupted" data from the pixels above and to the left. Net result? Only the first pixel is guaranteed to be correct.

The solution is pretty simple and straightforward. Create a second image array. As you calculate the blur values, use values from the pixels in the source array and store the result in a result array. This preserves all the original values in one array and puts blurred values in another.

Finally, write the blur array out to the output file.

BTW, this has nothing to do with the indexs[] array.

Any questions? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • Impressive actually. It helped a lot although I still have problem with edge pixels but this another question. My question here is : what do you mean with this " this has nothing to do with the indexs[] array. " ? Do you mean that the bug is not in the indexs array ?? or indexs array is useless ? Aug 8 at 9:29
  • The major logic issue has nothing to do with the index array. Don't know if it has any relationship to the edge problems, but like you said, that's for a new question. ;-) Actually, your use of the indexs[] array is one of the more unique solutions I've seen.
    – Cliff B
    Aug 8 at 9:34
  • I guess I should be proud of this. Thank you Cliff for your help :) :) . Aug 8 at 9:42

You must log in to answer this question.

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