1

New version of my previous problem here PSET4 LESS BLUR function -Ending Edges problem

I know the problem is with the imitations but can't figure out what is it

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


// Blur image
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++)
        {
            // if(i == 0 && j == 600)
            // {

            // 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] > -1 && indexs[k][0] < height+1 && indexs[k][1] > -1 && indexs[k][1] < width+1)
                {
                    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);
        }
        // }
    }
}


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


1 Answer 1

2

Not sure what you changed but the problem code remains exactly the same. It's in these lines (from the original code):

for( int i = 0; i <= height  ; i++)
{
    for( int j = 0; j <= width ; j++)
    {
       ...
            // 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)
            {

The problem is straightforward. The code is going beyond the upper boundaries of width and height.

The fix is straightforward. In the for loops, replace >= with >. In the if statement, replace <= with <.

The explanation is that the = allowed the values to exceed the array limit. Remember that the largest index used by an array is one less than the element count (because the index starts at 0, not 1.)

Note: This code(below) is effectively identical to the original code, so it has no different effect.

            // only add valid pixels considering corner and edge pixels
            if( indexs[k][0] > -1 && indexs[k][0] < height+1 && indexs[k][1] > -1 && indexs[k][1] < width+1)
            {
                ...

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

3
  • 1
    It was obvious but I didn't concentrate enough. Thank you Cliff. you always help us. Commented Aug 10, 2021 at 9:19
  • @Cliff B, I would like to ask a question concerning pset4/filter-less/blur Is it possible to solve this problem by using pointers to filter those pixels whose pointer is NOT NULL? And only use those pixels to calculate the total number of pixels to use within the 3x3 array to calc the average for R, G, B ? I see that most of the people who publish their code don't use pointers. Wonder why. Commented Aug 3, 2022 at 2:20
  • There's really no need to use pointers here. Generally speaking, one uses pointers when you have to process an unknown or indeterminate amount of data and can't process it linearly. Linked lists (coming soon) are a good example of that. In this case, it's just a matter of loading the image pixels into one array and putting the computed results into another array before writing that array to an output file. Is it possible to do this with pointers? I don't see why not. Is it necessary, or even efficient? Not really.
    – Cliff B
    Commented Aug 7, 2022 at 5:04

You must log in to answer this question.

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