Thread: Need help making a blur effect in c

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    1

    Need help making a blur effect in c

    I had an assignment to make a blur effect in c. I'm using a 3x3 pixel sum avarage for the blur effect. It still doesnt work right and I dont understand what im doing right. It's supposed to be running correctly. Any help would be appreciated.
    Code:
    // Blur image
    void blur(int height, int width, RGBTRIPLE image[height][width])
    {
        RGBTRIPLE sum;
        int change;
        for(int i = 0; i < height; i++)
        {
            for(int j = 0; j < width; j++)
            {
                change = 0;
                sum.rgbtRed = 0;
                sum.rgbtGreen = 0;
                sum.rgbtBlue = 0;
                for(int y = -1; y < 2; y++)
                {
                    //check if the row exists
                    if(y + i > -1 && y + i < height)
                    {
                    for(int x = -1; x < 2; x++)
                    {
                        //check if the pixel exists
                        if(x + j > -1 && x + j < width)
                        {
                            sum.rgbtRed += image[i + y][j + x].rgbtRed;
                            sum.rgbtGreen += image[i + y][j + x].rgbtGreen;
                            sum.rgbtBlue += image[i + y][j + x].rgbtBlue;
                            change++;
                        }
                    }
                    }
                }
                //if change is 0 then no pixels were found
                if(change != 0)
                    //devide the sum by number of pixels found
                    sum.rgbtRed = sum.rgbtRed / change;
                    sum.rgbtGreen = sum.rgbtGreen / change;
                    sum.rgbtBlue = sum.rgbtBlue / change;
                    image[i][j] = sum;
            }
        }
        return;
    }
    Last edited by odachi; 04-17-2020 at 09:17 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,056
    > image[i][j] = sum;
    You do realise that you're going to blur again on the next iteration right?

    Try outputting to a separate array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    676
    Take a look at Gaussian Blur. Maybe this is what you are looking for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quadratic and Gaussian Blur
    By TIMBERings in forum C++ Programming
    Replies: 0
    Last Post: 09-19-2010, 06:15 PM
  2. Blur on.. skipping codings/go to.
    By cheongzewei in forum C Programming
    Replies: 4
    Last Post: 11-14-2008, 06:07 AM
  3. Motion Blur Questions
    By Sentral in forum Game Programming
    Replies: 5
    Last Post: 02-25-2006, 01:58 PM
  4. Window Drag / Blur
    By ventolin in forum Windows Programming
    Replies: 6
    Last Post: 07-26-2004, 07:27 AM
  5. Makes Me So Blur!!!
    By Mitchell in forum Windows Programming
    Replies: 1
    Last Post: 10-22-2001, 06:44 AM

Tags for this Thread