Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent Numpy/ SciPy gaussian blur from converting image to grey scale?

I want to perform gaussian blur on an image but I don't want to be convert to grey scale. Is there anyway to perform this operation and keep the color?

from scipy import misc

import scipy

import numpy as np

a = misc.imread('A.jpg')

# A retains its color
misc.imsave('color.jpg', a)

# A_G_Blur gets converted to grey scale, I want to prevent this
a_g_blure = ndimage.uniform_filter(a, size=11)

# I want it to keep it's color
misc.imsave('now_grey.jpg', a)
like image 388
Dr.Knowitall Avatar asked Jul 31 '15 16:07

Dr.Knowitall


People also ask

What is Sigma in Gaussian blur?

The role of sigma in the Gaussian filter is to control the variation around its mean value. So as the Sigma becomes larger the more variance allowed around mean and as the Sigma becomes smaller the less variance allowed around mean. Filtering in the spatial domain is done through convolution.

What is Gaussian blur in image processing?

What is Gaussian blurring? Named after mathematician Carl Friedrich Gauss (rhymes with “grouse”), Gaussian (“gow-see-an”) blur is the application of a mathematical function to an image in order to blur it. “It's like laying a translucent material like vellum on top of the image,” says photographer Kenton Waltz.

Why Gaussian filter is used in image processing?

A Gaussian Filter is a low pass filter used for reducing noise (high frequency components) and blurring regions of an image. The filter is implemented as an Odd sized Symmetric Kernel (DIP version of a Matrix) which is passed through each pixel of the Region of Interest to get the desired effect.


1 Answers

a is a 3-d array with shape (M, N, 3). The problem is that ndimage.uniform_filter(a, size=11) applies a filter with length 11 to each dimension of a, include the third axis that holds the color channels. When you apply the filter with length 11 to an axis with length 3, the resulting values are all pretty close to the average of the three values, so you get something pretty close to a gray scale. (Depending on the image, you might have some color left.)

What you actually want is to apply a 2-d filter to each color channel separately. You can do this by giving a tuple as the size argument, using a size of 1 for the last axis:

a_g_blure = ndimage.uniform_filter(a, size=(11, 11, 1))

Note: uniform_filter is not a Gaussian blur. For that, you would use scipy.ndimage.gaussian_filter. You might also be interested in the filters provided by scikit-image. In particular, see skimage.filters.gaussian_filter.

like image 78
Warren Weckesser Avatar answered Sep 20 '22 12:09

Warren Weckesser