BUG: Correctly convolve integer and floating point arrays.

This commit is contained in:
Stefan van der Walt
2011-04-19 17:44:50 +02:00
parent cef53c172d
commit 430821a910
2 changed files with 30 additions and 12 deletions
+16 -12
View File
@@ -67,9 +67,10 @@ def hsobel(image, mask=None):
big_mask = binary_erosion(mask,
generate_binary_structure(2, 2),
border_value = 0)
result = np.abs(convolve(image, np.array([[ 1, 2, 1],
[ 0, 0, 0],
[-1,-2,-1]]).astype(float) / 4.0))
result = np.abs(convolve(image.astype(float),
np.array([[ 1, 2, 1],
[ 0, 0, 0],
[-1,-2,-1]]).astype(float) / 4.0))
result[big_mask == False] = 0
return result
@@ -103,9 +104,10 @@ def vsobel(image, mask=None):
big_mask = binary_erosion(mask,
generate_binary_structure(2, 2),
border_value=0)
result = np.abs(convolve(image, np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]]).astype(float) / 4.0))
result = np.abs(convolve(image.astype(float),
np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]]).astype(float) / 4.0))
result[big_mask == False] = 0
return result
@@ -161,9 +163,10 @@ def hprewitt(image, mask=None):
big_mask = binary_erosion(mask,
generate_binary_structure(2, 2),
border_value=0)
result = np.abs(convolve(image, np.array([[ 1, 1, 1],
[ 0, 0, 0],
[-1,-1,-1]]).astype(float) / 3.0))
result = np.abs(convolve(image.astype(float),
np.array([[ 1, 1, 1],
[ 0, 0, 0],
[-1,-1,-1]]).astype(float) / 3.0))
result[big_mask == False] = 0
return result
@@ -197,8 +200,9 @@ def vprewitt(image, mask=None):
big_mask = binary_erosion(mask,
generate_binary_structure(2, 2),
border_value=0)
result = np.abs(convolve(image, np.array([[1, 0, -1],
[1, 0, -1],
[1, 0, -1]]).astype(float) / 3.0))
result = np.abs(convolve(image.astype(float),
np.array([[1, 0, -1],
[1, 0, -1],
[1, 0, -1]]).astype(float) / 3.0))
result[big_mask == False] = 0
return result
+14
View File
@@ -1,7 +1,11 @@
import os
from numpy.testing import *
import numpy as np
from scipy.ndimage import binary_dilation, binary_erosion
import scikits.image.filter as F
from scikits.image import data_dir
class TestSobel():
def test_00_00_zeros(self):
@@ -35,6 +39,16 @@ class TestSobel():
assert (np.all(result[j == 0] == 1))
assert (np.all(result[np.abs(j) > 1] == 0))
def test_convolution_upcast(self):
i, j = np.mgrid[-5:6, -5:6]
image = np.load(os.path.join(data_dir, 'lena_GRAY_U8.npy'))
result1 = F.sobel(image)
image = image.astype(float)
result2 = F.sobel(image)
assert_array_equal(result1, result2)
class TestHSobel():
def test_00_00_zeros(self):
"""Horizontal sobel on an array of all zeros"""