0

My code works fine but for the ending edges not. I mean the right & bottom edges values. Here is my code :


void blur(int height, int width, RGBTRIPLE image[height][width])
{
    // h = 400 w = 600
    // doesn't read right corners nor edges
    // create a temp copy of the image pixels to process
    RGBTRIPLE originalImg[height][width];
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            originalImg[i][j].rgbtRed = image[i][j].rgbtRed;
            originalImg[i][j].rgbtBlue = image[i][j].rgbtBlue;
            originalImg[i][j].rgbtGreen = image[i][j].rgbtGreen;
        }
    }

    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,r = 0, b = 0, g = 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)
                {
                    int k0 = indexs[k][0], k1 = indexs[k][1];
                    // current valid pixel
                    RGBTRIPLE currentImg = originalImg[indexs[k][0]][indexs[k][1]];
                    // add current pixel color values to corresponding color variable
                    sumR += currentImg.rgbtRed;
                    sumB += currentImg.rgbtBlue;
                    sumG += currentImg.rgbtGreen;
                    r = currentImg.rgbtRed;
                    b = currentImg.rgbtBlue;
                    g = 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);
        }
    }
}


And check50 result :


:( 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
80 95 105
112 127 137
117 130 140
127 140 149
140 166 151
138 161 133
138 177 136
126 177 149


:( 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
80 95 105
100 115 125
124 139 149
113 126 136
123 136 145
142 155 163
141 150 164
113 119 136
143 151 164
156 166 171
157 179 169
111 157 128
128 159 140
127 144 139
106 122 108


0

The biggest clue of what is wrong is that the code works on the top and left edges but not the bottom and right edges. That indicates that it is likely to be an error with limits. An examination of the code reveals that there are several limit errors.

Hint: can an index for any of the image arrays be equal to height or width??? Or is the code going beyond the edge of the image??? ;-)

If you can't figure it out from this, please leave a comment.

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

1

You must log in to answer this question.

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