tags:

views:

34

answers:

2

I have this function semi-working, but it seems that the image that comes out is overly-blurred. this is because it is processing already blurred portions, I imagine?

I was told I need to write it to a new image, but I can't figure out how to create that. When I was using the Image tempImage constructor, I was getting errors of segmentation fault and could not determine why. Prior to this function, there is a function scaleUp that makes the image larger and save it to newImage.pixelData, created by the constructor Image newImage(width*numTimes, height*numTimes, colorMode);

void Image::simpleBlur(int numPixels)
      {
           int middlePixX, middlePixY, redSum, greenSum, blueSum, denominator = 0;       

           //Image tempImage(width, height, colorMode);
           //tempImage.createImage(width, height);

           for (int middlePixX = 0; middlePixX < width; middlePixX++)
           {
              for (int middlePixY = 0; middlePixY < height; middlePixY++)
              {
                 for (int x_subgrid = -1*numPixels; x_subgrid <= numPixels; x_subgrid++) //for (int x_subgrid = 0;  x_subgrid < DIM_SQUARE; x_subgrid++)
                 {
                    for (int y_subgrid = -1*numPixels; y_subgrid <= numPixels; y_subgrid++) //for (int y_subgrid = 0; y_subgrid < DIM_SQUARE; y_subgrid++)
                    {
                        int xPos = x_subgrid + middlePixX;  
                        int yPos = y_subgrid + middlePixY;

                if( xPos >= 0 && xPos < width && yPos >= 0 && yPos < height ) 
                        {            
                          redSum = redSum + pixelData[xPos][yPos].getRed();
                          greenSum = greenSum + pixelData[xPos][yPos].getGreen();
                          blueSum = blueSum + pixelData[xPos][yPos].getBlue();
                          denominator++;
                       }
                    }
                 }
                 redSum = redSum / denominator; 
                 greenSum = greenSum / denominator;
                 blueSum = blueSum / denominator;
                 pixelData[middlePixX][middlePixY].setRed(redSum);
                 pixelData[middlePixX][middlePixY].setGreen(greenSum);
                 pixelData[middlePixX][middlePixY].setBlue(blueSum);

                 redSum, greenSum, blueSum = 0; denominator = 0;
              }
           }  
        }
+1  A: 

You're trying to do this operation in-place, but since it's a neighbourhood operation you'll get bizarre results. Ideally you need separate input and output images, or, if you really have to do this in-place, then you'll need a small temporary buffer (and slightly more complicated code) to prevent modified pixels being used in your kernel.

Paul R
A: 

That is a bug in your algorithm, as you seem to suspect - as you blur pixel (x,y), it averages pixels above and directly to the left of it that have already been blurred, and you want to compute an average of the original contents. So you do need to process the old image to a new image somehow, or something similar.

Grumdrig