1

after trying to implement a Gaussian blur for an image i have ran into a problem where the output image looks like multiple blurred versions of the original image (input image)

I have too low of a reputation to post images so have no idea how to fully show you what is happening however, i can post a gyazo link to the image:

https://gyazo.com/38fbe1abd442a3167747760866584655 - Original, https://gyazo.com/471693c49917d3d3e243ee4156f4fe12 - Output

Here is some code:

int kernel[3][3] = { 1, 2, 1,
                   2, 4, 2,
                   1, 2, 1 };

void guassian_blur2D(unsigned char * arr, unsigned char * result, int width, int height)
{
    for (int row = 0; row < height; row++) 
    {
        for (int col = 0; col < width; col++) 
        {
            for (int k = 0; k < 3; k++) 
            {
                result[3 * row * width + 3 * col + k] = accessPixel(arr, col, row, k, width, height);
            }
        }
    }
}

int accessPixel(unsigned char * arr, int col, int row, int k, int width, int height) 
{
    int sum = 0;
    int sumKernel = 0;

    for (int j = -1; j <= 1; j++) 
    {
        for (int i = -1; i <= 1; i++) 
        {
            if ((row + j) >= 0 && (row + j) < height && (col + i) >= 0 && (col + i) < width) 
            {
                int color = arr[(row + j) * 3 * width + (col + i) * 3 + k];
                sum += color * kernel[i + 1][j + 1];
                sumKernel += kernel[i + 1][j + 1];
            }
        }
    }

    return sum / sumKernel;
}

Image is saved:

guassian_blur2D(inputBuffer, outputBuffer, width, height);

//Save the processed image
outputImage.convertToType(FREE_IMAGE_TYPE::FIT_BITMAP);
outputImage.convertTo24Bits();
outputImage.save("appleBlur.png");
cout << "Blur Complete" << endl;

Any help would be great, if this also helps i am trying to store the image as a grey-scale so that no colour is saved.

user7195486
  • 33
  • 1
  • 1
  • 8
  • Hard to answer without seeing the image. It can look blurry even without using a Gaussian blur filter if you try to reduce an image to 24 bits from some higher color space. So the one thing I would check is to make sure that the original image is 24 bits (at least to verify that that is not the problem) – bremen_matt Feb 12 '17 at 10:38
  • After that, you should try removing the Gaussian blur effect by seeing the k loop bound from k < 3 to k < 1. My understanding is that you should get back exactly the original image in that case. Taking these two steps will help you isolate where the problem lies – bremen_matt Feb 12 '17 at 10:40
  • The 24 bit conversion is fine and yeah it is hard to example, if i am allowed to post a gyazo here (will try now) hopefully you can see. – user7195486 Feb 12 '17 at 10:43
  • https://gyazo.com/38fbe1abd442a3167747760866584655 - Original https://gyazo.com/471693c49917d3d3e243ee4156f4fe12 - Output – user7195486 Feb 12 '17 at 10:44
  • 1
    Ha. Oh. Not at all what I imagined. – bremen_matt Feb 12 '17 at 10:48
  • Yeah the Gyazo post worked :)! hopefully someone has encountered this before and can help – user7195486 Feb 12 '17 at 10:56
  • Your indexing does not seem correct. You keep writing 3 * row * width for example. That doesn't seem correct to me. I would encourage you to think what the loop would look like if you weren't applying a filter, and to think of the filter as applying a perturbation. So for instance, your indexing equations would be much clearer if your wrote ... result[row*width+col+xxx] – bremen_matt Feb 12 '17 at 11:06
  • Perhaps an even better approach would be to write an indexing function that converts the desired row and column inxe to the linear index... so a function that looks like ...int indexer (row, col, width,height). Then for instance, in your loop you would have result[indexer(...)] = – bremen_matt Feb 12 '17 at 11:09
  • I believe this is an indexing problem, and this would help fix that. Afterwards, you can remove the indexer fuction if you want to optimize the code – bremen_matt Feb 12 '17 at 11:10
  • 1
    here is an example of Gaussian Blur https://github.com/ragnraok/android-image-filter/tree/master/library/jni – Jon Goodwin Feb 12 '17 at 11:17
  • okay thankyou for your help, i am struggling to re order the code but will have to keep trying, thankyou again – user7195486 Feb 12 '17 at 11:17

1 Answers1

2

Looks like the problem is not within your blurring code, and is related to saving or accessing image data.

I have used OpenCV to read/save images, and got expected result. Here's a snippet:

cv::Mat3b img = cv::imread("path_to_img.png");
cv::Mat3b out = img.clone();

guassian_blur2D(img.data, out.data, img.cols, img.rows);

cv::imshow("img", img);
cv::imshow("out", out);
cv::waitKey(0);

And here are input and output images: input output

The blur is not very noticeable (due to high image resolution and small kernel), but if you look carefully - it looks correct.

alexisrozhkov
  • 1,623
  • 12
  • 18
  • Wow okay, i shall use OpenCV and see how i get on, thankyou for trying this yourself and showing results! It really helps – user7195486 Feb 12 '17 at 12:26