Skip to content

Commit

Permalink
ENH Add zernike_moments
Browse files Browse the repository at this point in the history
This adds a default to the degree and makes radius the first parameter.

Deprecated zernike
  • Loading branch information
luispedro committed Mar 8, 2012
1 parent 295c5a7 commit 717bcde
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Version 0.7.2+
* Use imread module (if available)
* Add output argument to erode() & dilate()
* Add 14th Haralick feature (patch by MattyG)
* Improved zernike interface (zernike_moments)

Version 0.7.2 2012-02-13 by luispedro
* Fix type bug in 32 bit machines (Bug report by Lech Wiktor Piotrowski)
Expand Down
3 changes: 2 additions & 1 deletion mahotas/features.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .texture import haralick
from .tas import tas, pftas
from .zernike import zernike
from .zernike import zernike, zernike_moments
from .lbp import lbp

__all__ = [
Expand All @@ -9,4 +9,5 @@
'pftas',
'tas',
'zernike',
'zernike_moments',
]
8 changes: 4 additions & 4 deletions mahotas/tests/test_zernike.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from mahotas import zernike
from mahotas.features import zernike_moments
from mahotas.center_of_mass import center_of_mass
from math import atan2
from numpy import cos, sin, conjugate, pi, sqrt
Expand All @@ -25,7 +25,7 @@ def _factorial(n):
v *= (n+1)/pi
return v

def _slow_zernike(img, D, radius):
def _slow_zernike(img, radius, D):
zvalues = []

Y,X = np.where(img > 0)
Expand All @@ -49,7 +49,7 @@ def _slow_zernike(img, D, radius):

def test_zernike():
A = (np.arange(256) % 14).reshape((16, 16))
slow = _slow_zernike(A, 12, 8.)
fast = zernike.zernike(A, 12, 8.)
slow = _slow_zernike(A, 8., 12)
fast = zernike_moments(A, 8., 12)
delta = np.array(slow) - fast
assert np.abs(delta).max() < 0.001
30 changes: 19 additions & 11 deletions mahotas/zernike.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2006-2011, Luis Pedro Coelho <luis@luispedro.org>
# Copyright (C) 2006-2012, Luis Pedro Coelho <luis@luispedro.org>
# vim: set ts=4 sts=4 sw=4 expandtab smartindent:
#
# This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -27,25 +27,33 @@
from center_of_mass import center_of_mass
import _zernike

__all__ = ['zernike']
__all__ = ['zernike', 'zernike_moments']

def zernike(img, degree, radius, cm=None):
def zernike(im, degree, radius, cm=None):
"""
zvalues = zernike(img, degree, radius, cm={center_of_mass(img)})
zvalues = zernike(im, degree, radius, cm={center_of_mass(im)})
"""
import warnings
warnings.warn('mahotas.zernike.zernike: This interface is deprecated. Switch your arguments and use ``zernike_moments``', DeprecationWarning)
return zernike_moments(im, radius, degree, cm)

def zernike_moments(im, radius, degree=8, cm=None):
"""
zvalues = zernike_moments(im, radius, degree=8, cm={center_of_mass(im)})
Zernike moments through ``degree``
Returns a vector of absolute Zernike moments through ``degree`` for the
image ``img``.
image ``im``.
Parameters
----------
img : 2-ndarray
im : 2-ndarray
input image
degree : integer
Maximum degree to use
radius : integer
the maximum radius for the Zernike polynomials, in pixels
degree : integer, optional
Maximum degree to use (default: 8)
cm : pair of floats, optional
the centre of mass to use. By default, uses the image's centre of mass.
Expand All @@ -61,12 +69,12 @@ def zernike(img, degree, radius, cm=None):
"""
zvalues = []
if cm is None:
c0,c1 = center_of_mass(img)
c0,c1 = center_of_mass(im)
else:
c0,c1 = cm

Y,X = np.mgrid[:img.shape[0],:img.shape[1]]
P = img.ravel()
Y,X = np.mgrid[:im.shape[0],:im.shape[1]]
P = im.ravel()

def rescale(C, centre):
Cn = C.astype(np.double)
Expand Down

0 comments on commit 717bcde

Please sign in to comment.