From f6b279bff7d867b38468a259687a37c7356d1fb2 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Sun, 11 Dec 2011 19:57:40 -0500 Subject: [PATCH] Fix whitespace --- skimage/detection/_template.pyx | 33 ++++++++++++------------ skimage/detection/setup.py | 1 + skimage/detection/template.py | 14 +++++----- skimage/detection/tests/test_template.py | 6 ++--- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/skimage/detection/_template.pyx b/skimage/detection/_template.pyx index 9c9b9f6f..67a462d7 100644 --- a/skimage/detection/_template.pyx +++ b/skimage/detection/_template.pyx @@ -15,12 +15,12 @@ cdef extern from "math.h": cdef integral_image(np.ndarray[float, ndim=2, mode="c"] image): """ Calculate the summed integral image. - + Parameters ---------- image : array_like, dtype=float Source image. - + Returns ------- output : ndarray, dtype=np.double_t @@ -42,7 +42,7 @@ cdef integral_image(np.ndarray[float, ndim=2, mode="c"] image): for y in range(0, height): s += image[y, x] ii[y, x] = s + ii[y, x - 1] - + return ii @@ -50,12 +50,12 @@ cdef integral_image(np.ndarray[float, ndim=2, mode="c"] image): cdef integral_image_sqr(np.ndarray[float, ndim=2, mode="c"] image): """ Calculate the squared integral image. - + Parameters ---------- image : array_like, dtype=float Source image. - + Returns ------- output : ndarray, dtype=np.double_t @@ -77,7 +77,7 @@ cdef integral_image_sqr(np.ndarray[float, ndim=2, mode="c"] image): for y in range(0, height): s += image[y, x] * image[y, x] ii2[y, x] = s + ii2[y, x - 1] - + return ii2 @@ -85,12 +85,12 @@ cdef integral_image_sqr(np.ndarray[float, ndim=2, mode="c"] image): cdef integral_images(np.ndarray[float, ndim=2, mode="c"] image): """ Calculate the summed and sqared integral image. - + Parameters ---------- image : array_like, dtype=float Source image. - + Returns ------- output : tuple (ndarray, ndarray) of type np.double_t @@ -118,12 +118,12 @@ cdef integral_images(np.ndarray[float, ndim=2, mode="c"] image): s2 += image[y, x] * image[y, x] ii[y, x] = s + ii[y, x - 1] ii2[y, x] = s2 + ii2[y, x - 1] - + return ii, ii2 @cython.boundscheck(False) -cdef double sum_integral(np.ndarray[np.double_t, ndim=2, mode="c"] sat, +cdef double sum_integral(np.ndarray[np.double_t, ndim=2, mode="c"] sat, int r0, int c0, int r1, int c1): """ Using a summed area table / integral image, calculate the sum @@ -178,15 +178,15 @@ def match_template(np.ndarray[float, ndim=2, mode="c"] image, # variance ** 2 = 1/K Sigma[(x_k - mean) ** 2] = 1/K Sigma[x_k ** 2] - mean ** 2 cdef double template_norm cdef double template_mean = np.mean(template) - + if num_type == 0: template_norm = sqrt((np.std(template) ** 2 + template_mean ** 2)) / sqrt(inv_area) else: template_norm = sqrt((template_mean ** 2)) / sqrt(inv_area) - + # define window of template size in squared integral image cdef int i, j - cdef double num, window_sum2, window_mean2, normed, t, + cdef double num, window_sum2, window_mean2, normed, t, # move window through convolution results, normalizing in the process for i in range(result.shape[0] - 1): for j in range(result.shape[1] - 1): @@ -196,7 +196,7 @@ def match_template(np.ndarray[float, ndim=2, mode="c"] image, t = sum_integral(integral_sum, i, j, i + template.shape[0], j + template.shape[1]) window_mean2 = t * t * inv_area num -= t*template_mean - + # calculate squared template window sum in the image window_sum2 = sum_integral(integral_sqr, i, j, i + template.shape[0], j + template.shape[1]) normed = sqrt(window_sum2 - window_mean2) * template_norm @@ -207,7 +207,7 @@ def match_template(np.ndarray[float, ndim=2, mode="c"] image, if num > 0: num = 1 else: - num = -1 + num = -1 else: num = 0 result[i, j] = num @@ -215,5 +215,6 @@ def match_template(np.ndarray[float, ndim=2, mode="c"] image, for i in range(result.shape[0]): result[i, -1] = 0 for j in range(result.shape[1]): - result[-1, j] = 0 + result[-1, j] = 0 return result + diff --git a/skimage/detection/setup.py b/skimage/detection/setup.py index 52445d0c..6295cba5 100644 --- a/skimage/detection/setup.py +++ b/skimage/detection/setup.py @@ -29,3 +29,4 @@ if __name__ == '__main__': license = 'SciPy License (BSD Style)', **(configuration(top_path='').todict()) ) + diff --git a/skimage/detection/template.py b/skimage/detection/template.py index 9ea8dc56..eb780799 100644 --- a/skimage/detection/template.py +++ b/skimage/detection/template.py @@ -4,6 +4,7 @@ import numpy as np import cv import _template + #XXX add to opencv backend once backend system in place def match_template_cv(image, template, out=None, method="norm-coeff"): """Finds a template in an image using normalized correlation. @@ -43,17 +44,17 @@ def match_template(image, template, method="norm-coeff"): Template to locate. method: str (default 'norm-coeff') The correlation method used in scanning. - T represents the template, I the image and R the result. + T represents the template, I the image and R the result. The summation is done over x' = 0..w-1 and y' = 0..h-1 of the template. - 'norm-coeff': + 'norm-coeff': R(x, y) = Sigma(x',y')[T(x', y').I(x + x', y + y')] / N N = sqrt(Sigma(x',y')[T(x', y')**2].Sigma(x',y')[I(x + x', y + y')**2]) - 'norm-corr': - R(x,y) = Sigma(x',y)[T'(x', y').I'(x + x', y + y')] / N + 'norm-corr': + R(x,y) = Sigma(x',y)[T'(x', y').I'(x + x', y + y')] / N N = sqrt(Sigma(x',y)[T'(x', y')**2].Sigma(x',y')[I'(x + x', y + y')**2]) where: T'(x, y) = T(x', y') - 1/(w.h).Sigma(x'',y'')[T(x'', y'')] - I'(x + x', y + y') = I(x + x', y + y') - + I'(x + x', y + y') = I(x + x', y + y') - 1/(w.h).Sigma(x'',y'')[I(x + x'', y + y'')] Returns @@ -70,6 +71,3 @@ def match_template(image, template, method="norm-coeff"): raise ValueError("Unknown template method: %s" % method) return _template.match_template(image, template, method_num) - - - diff --git a/skimage/detection/tests/test_template.py b/skimage/detection/tests/test_template.py index c781279b..987a3496 100644 --- a/skimage/detection/tests/test_template.py +++ b/skimage/detection/tests/test_template.py @@ -11,14 +11,14 @@ def test_template(): for x, y in target_positions: image[x:x+size, y:y+size] = target image += randn(400, 400)*2 - + for method in ["norm-corr", "norm-coeff"]: result = match_template(image, target, method=method) delta = 5 found_positions = [] # find the targets for i in range(50): - index = np.argmax(result) + index = np.argmax(result) y, x = np.unravel_index(index, result.shape) if not found_positions: found_positions.append((x, y)) @@ -38,7 +38,7 @@ def test_template(): if distance < delta: found = True assert found - + if __name__ == "__main__": from numpy import testing testing.run_module_suite()