Changed integral_image parameters to new format

This commit is contained in:
Tabish
2014-04-17 12:16:40 +05:30
parent bd442bd816
commit 083212b201
+36 -22
View File
@@ -33,15 +33,23 @@ def integral_image(img):
return S
def integrate(ii, *args):
def integrate(ii, begining, ending, *args):
"""Use an integral image to integrate over a given window.
Parameters
----------
ii : ndarray
Integral image.
args: lists of start and end coordinates
(see example for detailed explanation)
begining : A tuple
Coordinates of top left corner of window
For multiple windows each coordinate should be a list
ending : A tuple
Coordinates of bottom right corner of window
For multiple windows each coordinate should be a list
args: optional
for backward compatibility
used when each coordinate is specified in a seperate list
Returns
-------
@@ -50,27 +58,33 @@ def integrate(ii, *args):
Notes
-----
Example
>>arr = np.asarray([[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1],[1,1,1,1,1,1]])
>>print arr
[[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]
[1 1 1 1 1 1]]
>>ii = integral_image(arr);
>>print integrate(ii,1,0,1,2) #sum from (1,0) -> (1,2)
>>> arr = np.ones((5, 6), dtype=np.float)
>>> ii = integral_image(arr)
>>> print(integrate(ii,(1, 0), (1, 2))) # sum from (1,0) -> (1,2)
[ 3.]
>>print integrate(ii,3,3,4,5) #sum form (3,3) -> (4,5)
>>> print(integrate(ii,(3, 3), (4, 5))) # sum form (3,3) -> (4,5)
[ 6.]
>>print integrate(ii,[1,3],[0,3],[1,4],[2,5]) # sum from (1,0) -> (1,2) and (3,3) -> (4,5)
[3. 6.]
>>> print(integrate(ii,([1, 3], [0, 3]), ([1, 4], [2, 5]))) # sum from (1,0) -> (1,2) and (3,3) -> (4,5)
[3. 6.]
>>> print(integrate(ii, [1, 3], [0, 3], [1, 4], [2, 5])) # deprecated usage
[3. 6.]
"""
assert(len(args) == 2 * ii.ndim)
start = args[0]
end = args[ii.ndim]
for i in range(1, ii.ndim):
start = np.vstack((start, args[i]))
end = np.vstack((end, args[i + ii.ndim]))
# handle new input format
if(len(args) == 0):
start = begining[0]
end = ending[0]
for i in range(1, ii.ndim):
start = np.vstack((start, begining[i]))
end = np.vstack((end, ending[i]))
# handle deprecated input format
else:
args = (begining, ending) + args
start = args[0]
end = args[ii.ndim]
for i in range(1, ii.ndim):
start = np.vstack((start, args[i]))
end = np.vstack((end, args[i + ii.ndim]))
# each row of start/end is a starting/ending point
start = start.T
@@ -79,6 +93,7 @@ def integrate(ii, *args):
rows = start.shape[0]
image_shape = ii.shape
total_shape = image_shape
# take care of negative coordinates
for i in range(1, rows):
total_shape = np.vstack((total_shape, image_shape))
@@ -110,4 +125,3 @@ def integrate(ii, *args):
S += [sign * ii[tuple(corner_points[r])] if(bad[r] == False) else 0 for r in range(rows)] # add only good rows
return S