Code Search for Developers
 
 
  

fft_imdenoise_skel.py from matplotlib at Krugle


Show fft_imdenoise_skel.py syntax highlighted

#!/usr/bin/env python
"""Image denoising example using 2-dimensional FFT."""

import numpy as N
import pylab as P
import scipy as S

def mag_phase(F):
    """Return magnitude and phase components of spectrum F."""

    # XXX Look at the absolute and angle functions in numpy...

def plot_spectrum(F, amplify=1000):
    """Normalise, amplify and plot an amplitude spectrum."""

    M = # XXX use mag_phase to get the magnitude...

    # XXX Now, rescale M by amplify/maximum_of_M.  Numpy arrays can be scaled
    # in-place with ARR *= number.  For the max of an array, look for its max
    # method.

    # XXX Next, clip all values larger than one to one.  You can set all
    # elements of an array which satisfy a given condition with array indexing
    # syntax: ARR[ARR<VALUE] = NEWVALUE, for example.


    # Display: this one already works, if you did everything right with M
    P.imshow(M, P.cm.Blues)


# 'main' script


im = # XXX make an image array from the file 'moonlanding.jpg', using the
     # imread() function from scipy.misc.pilutil.

F = # Compute the 2d FFT of the input image.  Look for a 2-d FFT in N.dft

# Define the fraction of coefficients (in each direction) we keep
keep_fraction = 0.1

# XXX Call ff a copy of the original transform.  Numpy arrays have a copy method
# for this purpose.

# XXX Set r and c to be the number of rows and columns of the array.  Look for
# the shape attribute...

# Set to zero all rows with indices between r*keep_fraction and
# r*(1-keep_fraction):

# Similarly with the columns:


# Reconstruct the denoised image from the filtered spectrum.  There's an
# inverse 2d fft in the dft module as well. Call the result im_new

# Show the results.

# The code below already works, if you did everything above right.
P.figure()

P.subplot(221)
P.title('Original image')
P.imshow(im, P.cm.gray)

P.subplot(222)
P.title('Fourier transform')
plot_spectrum(F)

P.subplot(224)
P.title('Filtered Spectrum')
plot_spectrum(ff)

P.subplot(223)
P.title('Reconstructed Image')
P.imshow(im_new, P.cm.gray)

P.show()




See more files for this project here

matplotlib

Matplotlib is a pure python plotting library with the goal of making\r\npublication quality plots using a syntax familiar to matlab users. \r\nThe library uses Numeric for handling large\r\ndata sets and supports a variety of output backends

Project homepage: http://sourceforge.net/projects/matplotlib
Programming language(s): C,C++,Python
License: other

  faces/
    faces_skel0.tgz
    fmatch_skel1.py
  fortran_wrap/
    Makefile
    fib3.f
    test.py
  distributions_skel.py
  erathostenes_skel.py
  fft_imdenoise_skel.py
  fit_synapse_skel.py
  fitting_skel.py
  montecarlo_pi_skel.py
  polyroots1d_skel.py
  qsort_skel.py
  quad_newton_skel.py
  recarray_demo_skel.py
  regress_demo_skel.py
  scrape_key_stats_skel.py
  shoot_skel.py
  spline_demo_skel.py
  stats_descriptives_skel.py
  stats_distributions_skel.py
  trapezoid_skel.py
  wallis_pi_skel.py
  wordfreqs_skel.py