0

Need some help with blur. I think i'm getting it entirely wrong but i can't seem to understand why. None of the blur assessment are correct at all. Wonder if anyone can give me any tips on getting the thinking right.

void blur(int height, int width, RGBTRIPLE image[height][width]) { //create another array to place a copy of these images so that can manipulate the original image RGBTRIPLE newimage[height][width]; //create variables to store the manipulated values of the image int red, green, blue, counter;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        //assign original value of the image into the replacement array for manipulation  
        newimage[i][j] = image[i][j];

        //reset values of red,green,blue,counter for this pixel into 0
        red = green = blue = counter = 0;

        //for top left-most pixel 
        if (i == 0 && j == 0)
        {
            red = round ((newimage[0][0].rgbtRed + newimage[1][0].rgbtRed + newimage[1][1].rgbtRed + newimage[0][1].rgbtRed) / 4.0); 
            green = round ((newimage[0][0].rgbtGreen + newimage[1][0].rgbtGreen + newimage[1][1].rgbtGreen + newimage[0][1].rgbtGreen) / 4.0);
            blue = round ((newimage[0][0].rgbtBlue + newimage[1][0].rgbtBlue + newimage[1][1].rgbtBlue + newimage[0][1].rgbtBlue) / 4.0);
        }

        //for top right-most pixel
        if (i == 0 && j == (width - 1))
        {
            red = round((newimage[i][j].rgbtRed + newimage[i][j-1].rgbtRed + newimage[i+1][j-1].rgbtRed + newimage[i+1][j].rgbtRed) / 4.0);
            green = round((newimage[i][j].rgbtGreen + newimage[i][j-1].rgbtGreen + newimage[i+1][j-1].rgbtGreen + newimage[i+1][j].rgbtGreen) / 4.0);
            blue = round((newimage[i][j].rgbtBlue + newimage[i][j-1].rgbtBlue + newimage[i+1][j-1].rgbtBlue + newimage[i+1][j].rgbtBlue) / 4.0);
        }

        //for everything else on the top row
        if ((i == 0 && j != (width - 1)) || (i == 0 && j!= 0))
        {
            red = round((newimage[i][j].rgbtRed + newimage[i][j - 1].rgbtRed + newimage[i + 1][j - 1].rgbtRed + newimage[i + 1][j].rgbtRed
            + newimage [i + 1][j + 1].rgbtRed + newimage[i][j+1].rgbtRed) / 6.0);
            green = round((newimage[i][j].rgbtGreen + newimage[i][j - 1].rgbtGreen + newimage[i + 1][j - 1].rgbtGreen + newimage[i + 1][j].rgbtGreen
            + newimage [i + 1][j + 1].rgbtGreen + newimage[i][j+1].rgbtGreen) / 6.0);
            blue = round((newimage[i][j].rgbtBlue + newimage[i][j - 1].rgbtBlue + newimage[i + 1][j - 1].rgbtBlue + newimage[i + 1][j].rgbtBlue
            + newimage [i + 1][j + 1].rgbtBlue + newimage[i][j+1].rgbtBlue) / 6.0);
        }

        //for bottom left pixel
        if (i == height -1 && j == 0)
        {
            red = round((newimage[i][j].rgbtRed + newimage[i -1][j].rgbtRed + newimage[i - 1][j + 1].rgbtRed + 
            newimage[i][j + 1].rgbtRed) / 4.0);
            green = round((newimage[i][j].rgbtGreen + newimage[i -1][j].rgbtGreen + newimage[i - 1][j + 1].rgbtGreen + 
            newimage[i][j + 1].rgbtGreen) / 4.0);
            blue = round((newimage[i][j].rgbtBlue + newimage[i -1][j].rgbtBlue + newimage[i - 1][j + 1].rgbtBlue + 
            newimage[i][j + 1].rgbtBlue) / 4.0);
        }

        //for 1st column (for everything other than top left and bottom left pixel)
        if ((j == 0 && i != 0 ) || (j == 0 && i != height - 1))
        {
            red = round((newimage[i][j].rgbtRed + newimage[i - 1][j].rgbtRed + newimage[i - 1][j + 1].rgbtRed + newimage[i][j + 1].rgbtRed
            + newimage[i + 1][j + 1].rgbtRed + newimage[i + 1][j].rgbtRed) / 6.0);
            green = round((newimage[i][j].rgbtGreen + newimage[i - 1][j].rgbtGreen + newimage[i - 1][j + 1].rgbtGreen + 
            newimage[i][j + 1].rgbtGreen + newimage[i + 1][j + 1].rgbtGreen + newimage[i + 1][j].rgbtGreen) / 6.0);
            blue = round((newimage[i][j].rgbtGreen + newimage[i - 1][j].rgbtGreen + newimage[i - 1][j + 1].rgbtGreen + 
            newimage[i][j + 1].rgbtGreen + newimage[i + 1][j + 1].rgbtGreen + newimage[i + 1][j].rgbtGreen) / 6.0);
        }

        //for bottom right pixel 
        if ( i == (height - 1) && j == (width - 1))
        {
            red =  round((newimage[i][j].rgbtRed + newimage[i - 1][j].rgbtRed + newimage[i - 1][j - 1].rgbtRed + 
            newimage [i][j - 1].rgbtRed)/4.0);
            green =  round((newimage[i][j].rgbtGreen + newimage[i - 1][j].rgbtGreen + newimage[i - 1][j - 1].rgbtGreen + 
            newimage [i][j - 1].rgbtGreen)/4.0);
            blue =  round((newimage[i][j].rgbtBlue + newimage[i - 1][j].rgbtBlue + newimage[i - 1][j - 1].rgbtBlue + 
            newimage [i][j - 1].rgbtBlue)/4.0);
        }

        //for everything on bottom row that is not the 1st and the last row 
        if ((i == (height - 1) && j != 0) || (i == (height - 1) && j != (width - 1)))
        {
            red = round((newimage[i][j].rgbtRed + newimage[i][j - 1].rgbtRed + newimage[i - 1][j - 1].rgbtRed + newimage[i - 1][j].rgbtRed
            + newimage[i - 1][j + 1].rgbtRed + newimage[i][j + 1].rgbtRed) / 6.0);
            green = round((newimage[i][j].rgbtGreen + newimage[i][j - 1].rgbtGreen + newimage[i - 1][j - 1].rgbtGreen
            + newimage[i - 1][j].rgbtGreen + newimage[i - 1][j + 1].rgbtGreen + newimage[i][j + 1].rgbtGreen) / 6.0);
            blue = round((newimage[i][j].rgbtBlue + newimage[i][j - 1].rgbtBlue + newimage[i - 1][j - 1].rgbtBlue
            + newimage[i - 1][j].rgbtBlue + newimage[i - 1][j + 1].rgbtGreen + newimage[i][j + 1].rgbtGreen) / 6.0);
        }

        //for the last column except top right and bottom right pixel
        if (( j == (width - 1) && i != 0) || (j == ((width - 1) && i == (height - 1))))
        {
            red = round((newimage[i][j].rgbtRed + newimage[i -1][j].rgbtRed + newimage[i -1][j - 1].rgbtRed + newimage[i][j - 1].rgbtRed 
            + newimage[i + 1][j -1].rgbtRed + newimage[i + 1][j].rgbtRed) / 6.0);
            green = round((newimage[i][j].rgbtGreen + newimage[i -1][j].rgbtGreen + newimage[i -1][j - 1].rgbtGreen + 
            newimage[i][j - 1].rgbtGreen + newimage[i + 1][j -1].rgbtGreen + newimage[i + 1][j].rgbtGreen) / 6.0);
            blue = round((newimage[i][j].rgbtBlue + newimage[i -1][j].rgbtBlue + newimage[i -1][j - 1].rgbtBlue + 
            newimage[i][j - 1].rgbtBlue + newimage[i + 1][j -1].rgbtBlue + newimage[i + 1][j].rgbtBlue) / 6.0);
        }

        //for all ther pixels 
        if (i != 0 || i != (height - 1) || j != 0 || j != (width -1))
        {
            red = round((newimage[i][j].rgbtRed + newimage[i - 1][j - 1].rgbtRed + newimage[i - 1][j].rgbtRed + newimage[i - 1][j + 1].rgbtRed 
            + newimage[i][j + 1].rgbtRed + newimage[i + 1][j + 1].rgbtRed + newimage[i + 1][j].rgbtRed + newimage[i + 1][j -1].rgbtRed
            + newimage[i][j -1].rgbtRed) / 10.0);
            green = round((newimage[i][j].rgbtGreen + newimage[i - 1][j - 1].rgbtGreen + newimage[i - 1][j].rgbtGreen + 
            newimage[i - 1][j + 1].rgbtGreen + newimage[i][j + 1].rgbtGreen + newimage[i + 1][j + 1].rgbtGreen + newimage[i + 1][j].rgbtGreen
            + newimage[i + 1][j -1].rgbtGreen + newimage[i][j -1].rgbtGreen) / 10.0);
            blue = round((newimage[i][j].rgbtBlue + newimage[i - 1][j - 1].rgbtBlue + newimage[i - 1][j].rgbtBlue + 
            newimage[i - 1][j + 1].rgbtBlue + newimage[i][j + 1].rgbtBlue + newimage[i + 1][j + 1].rgbtBlue + newimage[i + 1][j].rgbtBlue
            + newimage[i + 1][j -1].rgbtBlue + newimage[i][j -1].rgbtBlue) / 10.0);
        }

        image[i][j].rgbtRed = red;
        image[i][j].rgbtGreen = green;
        image[i][j].rgbtBlue = blue;

(added and modified) I modified the if conditions and tried to get it to work this way. Still unsure why this is not working, could someone help with viewing the if conditions please.

// Blur image void blur(int height, int width, RGBTRIPLE image[height][width]) { //create another array to place a copy of these images so that can manipulate the original image RGBTRIPLE newimage[height][width]; //create variables to store the manipulated values of the image int red, green, blue, counter;

for (int i = 0; i < height; i++)
{
    for (int j = 0; j < width; j++)
    {
        //assign original value of the image into the replacement array for manipulation  
        newimage[i][j] = image[i][j];

        //reset values of red,green,blue,counter for this pixel into 0
        red = green = blue = counter = 0;

        //for pixel that you are looking at 
        if (i >= 0 && j >=0)
        {
            red += newimage[i][j].rgbtRed;
            green += newimage[i][j].rgbtGreen;
            blue += newimage[i][j].rgbtBlue;
            counter++;

        }

        //for pixel to the top left (exclude the pixels that are on left most column and top row)
        if((i != 0 && j > 0) || (i > 0 && j!= 0)) 
        {
            red += newimage[i - 1][j - 1].rgbtRed;
            green += newimage[i - 1][j - 1].rgbtGreen;
            blue += newimage[i - 1][j - 1].rgbtBlue;
            counter++;
        }

        //for pixel above the one you are looking at (exclude pixels on top row)
        if (i != 0  && j >= 0 )
        {
            red += newimage[i - 1][j].rgbtRed;
            green += newimage[i - 1][j].rgbtGreen;
            blue += newimage[i - 1][j].rgbtBlue;
            counter++;
        }

        //for pixel to the top right of the one you looking at (Exclude pixels that are on the right most column and top row) 
        if (i != 0 && j != (width -1))
        {
            red += newimage[i - 1][j + 1].rgbtRed;
            green += newimage[i - 1][j + 1].rgbtGreen;
            blue += newimage[i - 1][j + 1].rgbtBlue;
            counter++;

        }

        //for pixel to the right of the one you looking at (exclude pixels that are on the right most column)
        if (j != (width - 1))
        {
            red += newimage[i][j +1].rgbtRed;
            green += newimage[i][j + 1].rgbtGreen;
            blue += newimage[i][j + 1].rgbtBlue;
            counter++;
        }

        //for pixel to the bottom rght of the one you are looking at (exclude pixels that are on right most column and bottom row)
        if (i != (height - 1) && j != (width - 1))
        {
            red += newimage[i + 1][j + 1].rgbtRed;
            green += newimage[i + 1][j + 1].rgbtGreen;
            blue += newimage[i + 1][j + 1].rgbtBlue;
            counter++;
        }

        //for pixel to the bottom of the one you are looking at (exclude pixels that are on bottom row)
        if (i != (height - 1))
        {
            red += newimage[i + 1][j].rgbtRed;
            green += newimage[i + 1][j].rgbtGreen;
            blue += newimage[i + 1][j].rgbtBlue;
            counter++;
        }

        //for pixel to the bottom left of the one you ae looking at (exclude pixels that are on left most column and bottom row
        if (i != (height - 1) && j != 0)
        {
            red += newimage[i + 1][j - 1].rgbtRed;
            green += newimage[i + 1][j - 1].rgbtGreen;
            blue += newimage[i + 1][j - 1].rgbtBlue;
            counter++;
        }

        //for pixel to the left of the one you are looking at (exclude pixels that are on left most column)
        if (j != 0)
        {
            red += newimage[i][j - 1].rgbtRed;
            green += newimage[i][j - 1].rgbtGreen;
            blue += newimage[i + 1][j - 1].rgbtBlue;
            counter++;
        }

        image[i][j].rgbtRed = round (red / counter);
        image[i][j].rgbtGreen = round (green / counter);
        image[i][j].rgbtBlue = round (blue / counter);

    }

}

return;

1 Answer 1

1

What I understand at a quick glance at your code is that you transfer every value one by one into another 2D array at every iteration.
To process the colors you program calculations for every possible situation of a 3x3 grid around a pixel (nine different cases).
I have not looked too deeply into the specific calculations but what I have noticed is that you use values you have not assigned anything to yet. With newimage[i][j] = image[i][j]; at i = 0 and j = 0 you put in the first value into your new array. The first if statement however, takes all four values in the upper-left corner. You did not assign any value to three of these yet, which could result in an error.

There might be a better solution but I would suggest that you do not assign anything to the newly created array at first. Process the colors in the original array first and put the new value into the new array after that. At the end you can transfer all the values in the new array into the original array with a simple nested for loop.
As I said, I have not checked your specific calculations for the nine different cases but if it does not work out and you can not figure out why, I would suggest trying this if you want to:
Try finding an algorithm that always calculates the sum of all nine values in the 3x3 grid for all pixels BUT check each pixels "coordinates" first to know if they are inside of the image or not. If it is not, do not add it to the sum. Then divide by the number of values you summed up. That would safe you from having nine individual cases to worry about.

0

You must log in to answer this question.

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