Commit accab14c authored by Richard Torenvliet's avatar Richard Torenvliet

Merge branch 'master' of github.com:taddeus/licenseplates

parents c1140af0 56fc1ca9
from scipy.ndimage import convolve1d
from pylab import ceil, zeros, pi, e, exp, sqrt, array
def f(x, s):
"""Return the value of a 1D Gaussian function for a given x and scale."""
return exp(-(x ** 2 / (2 * s ** 2))) / (sqrt(2 * pi) * s)
def gauss1(s, order=0):
"""Sample a one-dimensional Gaussian function of scale s."""
s = float(s)
r = int(ceil(3 * s))
size = 2 * r + 1
W = zeros(size)
# Sample the Gaussian function
W = array([f(x - r, s) for x in xrange(size)])
if not order:
# Make sure that the sum of all kernel values is equal to one
W /= W.sum()
return W
def filterNoise(image, s):
'''Apply a gaussian blur to an image, to suppress noise.'''
filt = gauss1(s)
image = convolve1d(image.data, filt, axis=0, mode='nearest')
return convolve1d(image, filt, axis=1, mode='nearest')
from GrayscaleImage import GrayscaleImage
from scipy.ndimage import convolve1d
from pylab import ceil, zeros, pi, e, exp, sqrt, array
class GaussianFilter:
def __init__(self, scale):
self.scale = scale
def gaussian(self, x):
'''Return the value of a 1D Gaussian function for a given x and scale'''
return exp(-(x ** 2 / (2 * self.scale ** 2))) / (sqrt(2 * pi) * self.scale)
def get_1d_gaussian_kernel(self):
'''Sample a one-dimensional Gaussian function of scale s'''
radius = int(ceil(3 * self.scale))
size = 2 * radius + 1
result = zeros(size)
# Sample the Gaussian function
result = array([self.gaussian(x - radius) for x in xrange(size)])
# The sum of all kernel values is equal to one
result /= result.sum()
return result
def get_filtered_copy(self, image):
'''Apply a gaussian blur to an image, to suppress noise.'''
kernel = self.get_1d_gaussian_kernel()
image = convolve1d(image.data, kernel, axis=0, mode='nearest')
return GrayscaleImage(None, convolve1d(image, kernel, axis=1, mode='nearest'))
def get_scale(self):
return self.scale
def set_scale(self, scale):
self.scale = float(scale)
scale = property(get_scale, set_scale)
\ No newline at end of file
from FilterNoise import filterNoise
from FilterNoise import GaussianFilter
from GrayscaleImage import GrayscaleImage
# Get the image
image = GrayscaleImage('../images/plate.png')
output_image = filterNoise(image, 1.4)
filter = GaussianFilter(1.4)
output_image = filter.get_filtered_copy(image)
# Show the licenseplate
output_image = GrayscaleImage(None, output_image)
output_image.show()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment