1

i can`t get the picture blurred at all (it comes out a complete mash up of colours blended together ...) ...i' ve been trying to figure this out for months now :(( HELP!

void blur(int height, int width, RGBTRIPLE image[height][width])
{
    int sumred = 0;
    int sumblue = 0;
    int sumgreen = 0;
    int count = 0;
    RGBTRIPLE copy[height][width];
    
    
    //make a copy of the image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            copy[i][j] = image[i][j];
        }
    }
        
    //go through each pixel and check the surrounding pixels
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            //check if there is a top right (corner) pixel and add it
            if (i - 1 >= 0 && j + 1 < width)
            {
                sumblue += image[i - 1][j + 1].rgbtBlue;
                sumred += image[i - 1][j + 1].rgbtRed;
                sumgreen += image[i - 1][j + 1].rgbtGreen;
                count++;
            }
             
            //check if there is a right pixel and add it
            if (j + 1 < width)
            {
                sumblue += image[i][j + 1].rgbtBlue;
                sumred += image[i][j + 1].rgbtRed;
                sumgreen += image[i][j + 1].rgbtGreen;
                count++;
            }
             
            //check if there is a bottom right (corner) pixel and add it
            if (i + 1 < height && j + 1 < width)
            {
                sumblue += image[i + 1][j + 1].rgbtBlue;
                sumred += image[i + 1][j + 1].rgbtRed;
                sumgreen += image[i + 1][j + 1].rgbtGreen;
                count++;
            }
             
            //check if there is a bottom pixel and add it
            if (i + 1 < height)
            {
                sumblue += image[i + 1][j].rgbtBlue;
                sumred += image[i + 1][j].rgbtRed;
                sumgreen += image[i + 1][j].rgbtGreen;
                count++;
            }
             
            //check if there is bottom left pixel and add it
            if (i + 1 < height && j - 1 >= 0)
            {
                sumblue += image[i + 1][j - 1].rgbtBlue;
                sumred += image[i + 1][j - 1].rgbtRed;
                sumgreen += image[i + 1][j - 1].rgbtGreen;
                count++;
            }
            //check if there is a left pixel and add it
            if (j - 1 >= 0)
            {
                sumblue += image[i][j - 1].rgbtBlue;
                sumred += image[i][j - 1].rgbtRed;
                sumgreen += image[i][j - 1].rgbtGreen;
                count++;
            }
             
            //check if there is a top left pixel and add it
            if (i - 1 >= 0 && j - 1 >= 0)
            {
                sumblue += image[i - 1][j - 1].rgbtBlue;
                sumred += image[i - 1][j - 1].rgbtRed;
                sumgreen += image[i - 1][j - 1].rgbtGreen;
                count++;
            }
             
            //add the top pixel - the current one
            if (i - 1 >= 0)
            {
                sumblue += image[i - 1][j].rgbtBlue;
                sumred += image[i - 1][j].rgbtRed;
                sumgreen += image[i - 1][j].rgbtGreen;
                count++;
            }
             
            //add the middle pixel
            sumblue += image[i][j].rgbtBlue;
            sumred += image[i][j].rgbtRed;
            sumgreen += image[i][j].rgbtGreen;
            count++;
            
            //calculate the average for each colour
            copy[i][j].rgbtBlue = round(sumblue / count);
            copy[i][j].rgbtRed = round(sumred / count);
            copy[i][j].rgbtGreen = round(sumgreen / count);
        }
    }
    
    //change the image pixels to the values form the copy image
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            image[i][j].rgbtRed = copy[i][j].rgbtRed; 
            image[i][j].rgbtGreen = copy[i][j].rgbtGreen;
            image[i][j].rgbtBlue = copy[i][j].rgbtBlue;
        }
    }
    return;
}
0

First of all, I'm sorry for my English. In the first place, what you need to do in order to write a more efficient code is to create a "blurring function" which is going to return a "blurred pixel" and be called by the blur function. Through that function you can iterate through the neighbor pixels using a for loop for each axis from -1 to 1 : you need to pass as a parameter the pixel position (column/row) and use it within the loops. Then you need to check if it is a valid pixel, count them and sum every RGB value.

RGBTRIPLE get_blurred (int i, int j, int height, int width, RGBTRIPLE image[height][width])
{

    int counter=0;
    int red=0,blue=0,green=0;
    for(int a=-1; a<2;a++) //iterate through the neighbor column 
    {
        for(int b=-1; b<2;b++) //iterate through the neighbor row
        {
            int new_i=i+a; //add neighbor pixels 
            int new_j=j+b; //add neighbor pixels
        
        if(new_i>=0&&new_i<height&&new_j>=0&&new_j<width) //check pixel is valid
        {
            counter++; //count valid pixels
            red+=image[new_i][new_j].rgbtRed; //sum red values
            green+=image[new_i][new_j].rgbtGreen; //sum green values
            blue+=image[new_i][new_j].rgbtBlue; //sum blue values
        }
    }
}
//change pixel values to the neighbor pixels average 
RGBTRIPLE blurred_pixel;
blurred_pixel.rgbtBlue=round((float)blue/counter); 
blurred_pixel.rgbtGreen=round((float)green/counter);
blurred_pixel.rgbtRed=round((float)red/counter);
return blurred_pixel; //return the blurred pixel

}

Then you can replace every copy image pixel and assign them to the original image. Hope that was useful!

| improve this answer | |

You must log in to answer this question.

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