4
votes

I am writing an application in C++ using OpenCV to apply a Gaussian filter to individual pixels in an image. For example, I loop through each pixel in the image and if they match a particular RGB value, I want to apply the Gaussian algorithm to only those pixels so that blurring only occurs around those parts of an image.

However, I'm having trouble finding a way to do this. The GaussianBlur() function offered by the OpenCV library only allows me to blur an entire image and not simply apply the algorithm and kernel to one pixel at a time. Does anyone have any ideas on how I could achieve this (e.g. is there another method I don't know about)? I'm hoping I don't have to write the entire algorithm out myself to apply it to just a single pixel.

2

2 Answers

4
votes

A friend of mine came up with a good solution:

  • clone the original matrix
  • apply GaussianBlur() to the clone
  • compare each pixel to the RGB value
  • if match, replace the original's pixel with the clone's pixel

Can't believe how simple that was.

1
votes

You can code the gaussian blur yourself if you need to apply it only on a few pixels. It is much easier than you think and only takes a few lines. It is a simple stencil operator using a gaussian function for its kernel. All you need is the coordinates of the pixel and its neighbors.

The rest is straight forward. Here is an example of Gaussian matrix, which you can easily code, or generate using a Gaussian function:

Example of Gaussian blur matrix

In short, it is just a weighed average of the neighboring values.