Thread: Box blur implementation

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    7

    Box blur implementation

    Having trouble with this - I know I've written way too much code but I can't figure out what different to do...

    RGB is a
    Code:
    struct
    containing the red green and blue

    Code:
    void blur(int height, int width, RGB image[height][width])
    {
        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                    //0,0
                    float red1 = image[i][j].rgbtRed;
                    float green1 = image[i][j].rgbtGreen;
                    float blue1 = image[i][j].rgbtBlue;
                    
                    //0,1
                    float red2 = image[i][j + 1].rgbtRed;
                    float green2 = image[i][j + 1].rgbtGreen;
                    float blue2 = image[i][j + 1].rgbtBlue;
                    
                    //0,2
                    float red3 = image[i][j + 2].rgbtRed;
                    float green3 = image[i][j + 2].rgbtGreen;
                    float blue3 = image[i][j + 2].rgbtBlue;
                    
                    //1,0
                    float red4 = image[i + 1][j].rgbtRed;
                    float green4 = image[i + 1][j].rgbtGreen;
                    float blue4 = image[i + 1][j].rgbtBlue;
                    
                    //1,1
                    float red5 = image[i + 1][j + 1].rgbtRed;
                    float green5 = image[i + 1][j + 1].rgbtGreen;
                    float blue5 = image[i + 1][j + 1].rgbtBlue;
                    
                    //1,2
                    float red6 = image[i + 1][j + 2].rgbtRed;
                    float green6 = image[i + 1][j + 2].rgbtGreen;
                    float blue6 = image[i + 1][j + 2].rgbtBlue;
                
                    //2,0
                    float red7 = image[i + 2][j].rgbtRed;
                    float green7 = image[i + 2][j].rgbtGreen;
                    float blue7 = image[i + 2][j].rgbtBlue;
                    
                    //2,1
                    float red8 = image[i + 2][j + 1].rgbtRed;
                    float green8 = image[i + 2][j + 1].rgbtGreen;
                    float blue8 = image[i + 2][j + 1].rgbtBlue;
                    
                    //2,2
                    float red9 = image[i + 2][j + 2].rgbtRed;
                    float green9 = image[i + 2][j + 2].rgbtGreen;
                    float blue9 = image[i + 2][j + 2].rgbtBlue;
                    
                    float averageRed = red1 + red2 + red3 + red4 + red5 + red6 + red7 + red8 + red9;
                    float averageGreen = green1 + green2 + green3 + green4 + green5 + green6 + green7 + green8 + green9;
                    float averageBlue = blue1 + blue2 + blue3 + blue4 + blue5 + blue6 + blue7 + blue8 + blue9;
                    
                    averageRed = averageRed / 9;
                    averageGreen = averageGreen / 9;
                    averageBlue = averageBlue / 9;
                    
                    image[i][j].rgbtRed = roundf(averageRed);
                    image[i][j].rgbtGreen = roundf(averageGreen);
                    image[i][j].rgbtBlue = roundf(averageBlue);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    > float red2 = image[i][j + 1].rgbtRed;
    All these +1 and +2 are going to overflow your array bounds at the limits of your loops.

    Code:
    float red = 0;
    float green = 0;
    float blue = 0;
    for ( int x = 0 ; x < 2 ; x++ ) {
      for ( int y = 0 ; y < 2 ; y++ ) {
        red += image[i+x][j+y].rgbtRed;
      }
    }
    Saves you copy/pasting 40 lines of code.
    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
    May 2020
    Posts
    7
    Quote Originally Posted by Salem View Post
    > float red2 = image[i][j + 1].rgbtRed;
    All these +1 and +2 are going to overflow your array bounds at the limits of your loops.

    Code:
    float red = 0;
    float green = 0;
    float blue = 0;
    for ( int x = 0 ; x < 2 ; x++ ) {
      for ( int y = 0 ; y < 2 ; y++ ) {
        red += image[i+x][j+y].rgbtRed;
      }
    }
    Saves you copy/pasting 40 lines of code.
    Yes you're correct that's exactly what happened when I ran the program.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    426
    Quote Originally Posted by Salem View Post
    > float red2 = image[i][j + 1].rgbtRed;
    All these +1 and +2 are going to overflow your array bounds at the limits of your loops.

    Code:
    float red = 0;
    float green = 0;
    float blue = 0;
    for ( int x = 0 ; x < 2 ; x++ ) {
      for ( int y = 0 ; y < 2 ; y++ ) {
        red += image[i+x][j+y].rgbtRed;
      }
    }
    Saves you copy/pasting 40 lines of code.
    This is probably what you want for a box filter. a 3x3 box
    centred on the target pixel.
    You can be a bit more sophisticated in handling the edges.

    You can't write directly to your source as you corrupt the image
    for the next pixel.

    Code:
    float red = 0;
    float green = 0;
    float blue = 0;
    int N = 0;
    for ( int x = -1 ; x < 2 ; x++ ) {
      for ( int y = -1 ; y < 2 ; y++ ) {
        if (i + x >= 0 && i + x < width && j + y >= 0 && j + y < height){
          red += image[i+x][j+y].rgbtRed;
          N++;
        }
      }
    }
    outputimage[j][i].rgbtRed = red / N;
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    Registered User
    Join Date
    May 2020
    Posts
    7
    Quote Originally Posted by Malcolm McLean View Post
    This is probably what you want for a box filter. a 3x3 box
    centred on the target pixel.
    You can be a bit more sophisticated in handling the edges.

    You can't write directly to your source as you corrupt the image
    for the next pixel.

    Code:
    float red = 0;
    float green = 0;
    float blue = 0;
    int N = 0;
    for ( int x = -1 ; x < 2 ; x++ ) {
      for ( int y = -1 ; y < 2 ; y++ ) {
        if (i + x >= 0 && i + x < width && j + y >= 0 && j + y < height){
          red += image[i+x][j+y].rgbtRed;
          N++;
        }
      }
    }
    outputimage[j][i].rgbtRed = red / N;
    Thanks. I'm not going to lie and pretend that I understand how/where to implement the changes you've offered! What are the underlying concepts behind this box blur implementation in C?
    Specifically, what areas of C should I learn more about to deal with this? The problem came alongside teaching about memory, so I assume it's that? I try not to look at other people's code but in the little snippets I have seen, I've seen people using malloc.


    At the moment, I'm having fundamental problems understanding what I can and cannot do with the syntax e.g. how do I access the surrounding pixels in order to calculate their averages - hence the +1/-1 type approach I took.

    The code I wrote was my naive way of implementing the following:

    1) Assign each color within each pixel to a variable (float red1, green1 etc)

    2) Work out the averages

    3) Assign the averages to the current pixel (i.e. image[i][j])
    Last edited by JPEG23; 08-04-2020 at 08:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Image Blur Filter
    By Mcdom34 in forum C Programming
    Replies: 5
    Last Post: 05-01-2015, 08:36 AM
  2. Quadratic and Gaussian Blur
    By TIMBERings in forum C++ Programming
    Replies: 0
    Last Post: 09-19-2010, 06:15 PM
  3. Blur and sharpen a bitmap
    By Mikro in forum C++ Programming
    Replies: 20
    Last Post: 03-13-2005, 04:34 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