1

I have successfully finished the blur effect on PSET4.

Here is a small smattering of the code

 if(i - 1 >= 0 && j - 1 >= 0)
                {
                    red += OgImage[i-1][j-1].rgbtRed;
                    blue += OgImage[i-1][j-1].rgbtBlue;
                    green += OgImage[i-1][j-1].rgbtGreen;
                    
                    Divide++;
                }
                if(i - 1 >= 0 && j >= 0)
                {
                    red += OgImage[i-1][j].rgbtRed;
                    blue += OgImage[i-1][j].rgbtBlue;
                    green += OgImage[i-1][j].rgbtGreen;
                    
                    Divide++;
                }
                if(i - 1 >= 0 && j + 1 < width)
                {
                    red += OgImage[i-1][j+1].rgbtRed;
                    blue += OgImage[i-1][j+1].rgbtBlue;
                    green += OgImage[i-1][j+1].rgbtGreen;
                    
                    Divide++;
                }
                if(i >= 0 && j - 1 >= 0)
                {
                    red += OgImage[i][j-1].rgbtRed;
                    blue += OgImage[i][j-1].rgbtBlue;
                    green += OgImage[i][j-1].rgbtGreen;
                    
                    Divide++;
                }

etc...

It feels like there is probably some better way of doing this without writing out all this code though? My code passed all the verifications and works as intended. But is there some better way of creating edge of the image detection? He talked a lot about recursion in some of the previous lectures, does this situation lend itself to recursion or some other technique?

2 Answers 2

0

You can loop over the inner 3 by 3 grid and add a condition that covers all cases. Here's my code for the same section of blur.

for (int k = i - 1; k < i + 2; k++)
{
    for (int l = j - 1; l < j + 2; l++)
    {
        if (l >= 0 && l < width && k >= 0 && k < height)
        {
            sumred += image[k][l].rgbtRed;
            sumgrn += image[k][l].rgbtGreen;
            sumblu += image[k][l].rgbtBlue;

            pixelcount++; // same as your divide
        }
    }
}
3
  • Sorry to interupt, but why don't you used the i & j variables in it? I mean you use k & l. It means this Loops is an addition after the i & j variables iterated? Or how? I really am clueless about this problem:( can you explain more? Apr 29, 2021 at 21:51
  • @RizqiSitiRahmah Yes. These two loops are nested inside the outer i and j loops, which iterate over the lines and columns respectively. You'll see that I initialize k and l from values I get from i and j. So it becomes: for each line from first to last, for each column in that line from first to last, for each line from 1 before the current to 1 after current. for each column from 1 before current to 1 after. In total 4 nested loops. Apr 30, 2021 at 6:48
  • Aaah I see, I see🤩 Thanks a lot, you're amazingg👍👍👍 I used your method, and it works. Once again, thanks a million🙇‍♀️🙇‍♀️🙇‍♀️ May 1, 2021 at 5:52
0

Actually there are several ways of achieving blur. Heck you only need a single conditional statement to check if a pixel is valid or not.

In your implementation, it seems like you wrote an iterative solution(for loops) to do summation of pixels (check if they are valid using lot of conditional statements !). This can also be done using recursion to get final summation value

You must log in to answer this question.

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