#! /usr/bin/env python # -*- coding: utf-8 -*- # # Copyright (C) Mar 2011 Angelos Tzotsos # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see import numpy import scipy import pylab import matplotlib from scipy import ndimage from scipy import signal ###open image img = scipy.misc.imread('lena_std_gray.tif') ###show histogram from matplotlib hist, bins, patches = matplotlib.pyplot.hist(img.flatten(), 256) print hist matplotlib.pyplot.show() ###gaussian filter img_g = ndimage.gaussian_filter(img, 10) ###rank filter img_rnk = ndimage.filters.rank_filter(img,rank=3, size=5) ###median filter img_med = ndimage.filters.median_filter(img,size=3) ###maximum filter img_max = ndimage.filters.maximum_filter(img, size=3) maxima = img_max - img ###prewitt filter img_pwt = numpy.zeros(img.shape, dtype=float) ndimage.filters.prewitt(img, output=img_pwt) ###sobel filter img_sobel = numpy.zeros(img.shape, dtype=float) ndimage.filters.sobel(img, axis=-1, output=img_sobel) ###laplacian filter img_gen_laplace = numpy.zeros(img.shape, dtype=float) ndimage.filters.laplace(img, output=img_gen_laplace) ###log img_log = numpy.zeros(img.shape, dtype=float) ndimage.filters.gaussian_laplace(img, sigma=1, output=img_log) ###gaussian gradient img_ggrand = numpy.zeros(img.shape, dtype=float) ndimage.filters.gaussian_gradient_magnitude(img, sigma=3, output=img_ggrand) ###sobel gradient img_sgrad = numpy.zeros(img.shape, dtype=float) ndimage.filters.generic_gradient_magnitude(img, ndimage.filters.sobel, output=img_sgrad) ###generic filter fltr = numpy.array([[1,2,3],[3,2,1],[2,2,2]],float) deriv = signal.convolve2d(img, fltr, mode='same',boundary='symm') ###show result matplotlib.pyplot.imshow(img) matplotlib.pyplot.gray() matplotlib.pyplot.show() matplotlib.pyplot.imshow(deriv) matplotlib.pyplot.gray() matplotlib.pyplot.show()