#! /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 ###open image img = scipy.misc.imread('lena_std_gray.tif') struct= numpy.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) ###grayscale erosion img_eroded = numpy.zeros(img.shape, dtype=float) ndimage.morphology.grey_erosion(img,structure=struct, output=img_eroded) ###grayscale erosion img_dil = numpy.zeros(img.shape, dtype=float) ndimage.morphology.grey_dilation(img,structure=struct,size=struct.shape, output=img_dil) ###grayscale opening img_open = numpy.zeros(img.shape, dtype=float) ndimage.morphology.grey_opening(img,structure=struct,size=struct.shape, output=img_open) ###grayscale closing img_close = numpy.zeros(img.shape, dtype=float) ndimage.morphology.grey_closing(img,structure=struct,size=struct.shape, output=img_close) ###morphological gradient img_grad = numpy.zeros(img.shape, dtype=float) ndimage.morphology.morphological_gradient(img,structure=struct,size=struct.shape, output=img_grad) ###morphological laplace img_laplace = numpy.zeros(img.shape, dtype=float) ndimage.morphology.morphological_laplace(img,structure=struct,size=struct.shape, output=img_laplace) ###white tophat img_wtop = numpy.zeros(img.shape, dtype=float) ndimage.morphology.white_tophat(img,structure=struct,size=struct.shape, output=img_wtop) ###white tophat img_btop = numpy.zeros(img.shape, dtype=float) ndimage.morphology.black_tophat(img,structure=struct,size=struct.shape, output=img_btop) ###show result matplotlib.pyplot.imshow(img) matplotlib.pyplot.gray() matplotlib.pyplot.show() matplotlib.pyplot.imshow(img_btop) matplotlib.pyplot.gray() matplotlib.pyplot.show()