Skip to content

Commit

Permalink
BUG Fix Sobel filtering on newer numpy versions
Browse files Browse the repository at this point in the history
Some slight change in recent numpy versions triggered dodgy code in
sobel(). Curiously enough, Matlab seems to also not behave consistently
across different versions and output very different results with minor
differences in the input.
  • Loading branch information
luispedro committed Oct 17, 2014
1 parent 8ea3c30 commit b28431f
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 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 1.2.1+
* Allow floating point inputs to ``cwatershed()``
* Correctly check for float16 & float128 inputs
* Make sobel into a pure function (i.e., do not normalize its input)
* Fix sobel filtering

Version 1.2.1 2014-07-21 by luispedro
* Explicitly set numpy.include_dirs() in setup.py [patch by Andrew
Expand Down
10 changes: 3 additions & 7 deletions mahotas/edge.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2008-2013, Luis Pedro Coelho <luis@luispedro.org>
# Copyright (C) 2008-2014, Luis Pedro Coelho <luis@luispedro.org>
# vim: set ts=4 sts=4 sw=4 expandtab smartindent:
#
# License: MIT (see COPYING file)

from __future__ import division
import numpy as np
import mahotas as mh
from . import convolve

_hsobel_filter = np.array([
Expand Down Expand Up @@ -63,12 +64,7 @@ def sobel(img, just_filter=False):
if just_filter:
return filtered
thresh = 2*np.sqrt(filtered.mean())
filtered *= (np.sqrt(filtered) > thresh)

r,c = filtered.shape
x = (filtered > np.hstack((np.zeros((r,1)),filtered[:,:-1]))) & (filtered > np.hstack((filtered[:,1:], np.zeros((r,1)))))
y = (filtered > np.vstack((np.zeros(c),filtered[:-1,:]))) & (filtered > np.vstack((filtered[1:,:], np.zeros(c))))
return x | y
return mh.regmax(filtered) * (np.sqrt(filtered) > thresh)



3 changes: 1 addition & 2 deletions mahotas/tests/test_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ def test_sobel_pure():

def test_find_edge():
f = np.zeros((32,48))
f[:,16:] = 255
f[:,16:] = 1
f = mh.gaussian_filter(f,4)
fs = mh.sobel(f)
assert np.all(fs[:,15] > 0)

@raises(ValueError)
def test_3d_error():
import mahotas as mh
f = np.zeros((32,16,3))
sobel(f)

0 comments on commit b28431f

Please sign in to comment.