Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gaussian Smoothing an image in python

I am very new to programming in python, and im still trying to figure everything out, but I have a problem trying to gaussian smooth or convolve an image. This is probably an easy fix, but I've spent so much time trying to figure it out im starting to go crazy. I have a 3d .fits file of a group of galaxies and have cut out a certain one and saved it to a png with aplpy. Basically, it needs to be smoothed as a gaussian to a larger beam size (i.e. make the whole thing larger by expanding out the FWHM but dimming the output). I know there are things like scipy.ndimage.convolve and a similar function in numpy that I can use, but im having a hard time translating it into something usefull. If anyone can give me a hand with this and point me in the right direction it would be a huge help.

like image 315
Jenn Avatar asked Jul 11 '13 14:07

Jenn


People also ask

How do you smooth the edges of an image in Python?

To smoothen an image with a custom-made kernel we are going to use a function called filter2D() which basically helps us to convolve a custom-made kernel with an image to achieve different image filters like sharpening and blurring and more.

How do you do a Gaussian blur in Python?

Syntax – cv2 GaussianBlur() function [height width]. height and width should be odd and can have different values. If ksize is set to [0 0], then ksize is computed from sigma values. Kernel standard deviation along X-axis (horizontal direction).


1 Answers

Something like this perhaps?

import numpy as np
import scipy.ndimage as ndimage
import matplotlib.pyplot as plt

img = ndimage.imread('galaxies.png')
plt.imshow(img, interpolation='nearest')
plt.show()
# Note the 0 sigma for the last axis, we don't wan't to blurr the color planes together!
img = ndimage.gaussian_filter(img, sigma=(5, 5, 0), order=0)
plt.imshow(img, interpolation='nearest')
plt.show()

enter image description hereenter image description here

(Original image taken from here)

like image 76
Jaime Avatar answered Oct 11 '22 19:10

Jaime