Fix whitespace

This commit is contained in:
Tony S Yu
2012-05-08 21:28:49 -04:00
parent 424a2b8e52
commit f6b279bff7
4 changed files with 27 additions and 27 deletions
+17 -16
View File
@@ -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
+1
View File
@@ -29,3 +29,4 @@ if __name__ == '__main__':
license = 'SciPy License (BSD Style)',
**(configuration(top_path='').todict())
)
+6 -8
View File
@@ -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)
+3 -3
View File
@@ -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()