Documenting the code; Removing ascontiguousarray statements

This commit is contained in:
Ankit Agrawal
2013-08-15 14:59:56 +05:30
parent 555da870fe
commit 4d9baceb8a
2 changed files with 50 additions and 17 deletions
+19 -7
View File
@@ -24,6 +24,7 @@ def _get_filtered_image(image, n_scales, mode):
_censure_dob_loop(image, n, integral_img, filtered_image, inner_weight, outer_weight)
scales[:, :, i] = filtered_image
return scales
elif mode == 'Octagon':
# TODO : Decide the shapes of Octagon filters for scales > 7
outer_shape = [(5, 2), (5, 3), (7, 3), (9, 4), (9, 7), (13, 7), (15, 10)]
@@ -32,11 +33,9 @@ def _get_filtered_image(image, n_scales, mode):
integral_img = integral_image(image)
integral_img1 = _slanted_integral_image_modes(image, 1)
integral_img2 = _slanted_integral_image_modes(image, 2)
integral_img2 = np.ascontiguousarray(integral_img2)
integral_img3 = _slanted_integral_image_modes(image, 3)
integral_img3 = np.ascontiguousarray(integral_img3)
integral_img4 = _slanted_integral_image_modes(image, 4)
integral_img4 = np.ascontiguousarray(integral_img4)
for k in range(n_scales):
n = k + 1
filtered_image = np.zeros(image.shape)
@@ -59,7 +58,11 @@ def _slanted_integral_image_modes(img, mode=1):
if mode == 1:
"""
The following figures describe area that is summed up to calculate
the value at point @ in slanted integral image.
the value at point @ in slanted integral image. The subtended at @ is
135 degrees.
censure_cy._slanted_integral_image performs the mode1
_slanted_integral_image
_________________
|********/ |
|*******/ |
@@ -70,12 +73,17 @@ def _slanted_integral_image_modes(img, mode=1):
|_________________|
"""
image = np.copy(img, order='C')
mode1 = np.zeros((image.shape[0] + 1, image.shape[1]), order='C')
_slanted_integral_image(image, mode1)
return mode1[1:, :]
elif mode == 2:
"""
For mode2, the image can be first flipped left-right and then up-down.
Then we can use censure_cy._slanted_integral_image and the returned
result can be flipped left-right and then up-down to get the following
mode.
_________________
| |
| |
@@ -88,9 +96,10 @@ def _slanted_integral_image_modes(img, mode=1):
image = np.copy(img, order='C')
image = np.fliplr(image)
image = np.flipud(image)
image = np.ascontiguousarray(image)
mode2 = np.zeros((image.shape[0] + 1, image.shape[1]), order='C')
_slanted_integral_image(image, mode2)
mode2 = mode2[1:, :]
mode2 = np.fliplr(mode2)
mode2 = np.flipud(mode2)
@@ -110,9 +119,10 @@ def _slanted_integral_image_modes(img, mode=1):
image = np.copy(img, order='C')
image = np.flipud(image)
image = image.T
image = np.ascontiguousarray(image)
mode3 = np.zeros((image.shape[0] + 1, image.shape[1]), order='C')
_slanted_integral_image(image, mode3)
mode3 = mode3[1:, :]
mode3 = np.flipud(mode3.T)
return mode3
@@ -131,9 +141,10 @@ def _slanted_integral_image_modes(img, mode=1):
image = np.copy(img, order='C')
image = np.fliplr(image)
image = image.T
image = np.ascontiguousarray(image)
mode4 = np.zeros((image.shape[0] + 1, image.shape[1]), order='C')
_slanted_integral_image(image, mode4)
mode4 = mode4[1:, :]
mode4 = np.fliplr(mode4.T)
return mode4
@@ -143,6 +154,7 @@ def _suppress_line(response, sigma, rpc_threshold):
Axx, Axy, Ayy = _compute_auto_correlation(response, sigma)
detA = Axx * Ayy - Axy**2
traceA = Axx + Ayy
# ratio of principal curvatures
rpc = traceA**2 / (detA + 0.001)
response[rpc > rpc_threshold] = 0
+31 -10
View File
@@ -22,8 +22,8 @@ def _censure_dob_loop(double[:, ::1] image, Py_ssize_t n,
filtered_image[i, j] = outer_weight * outer - (inner_weight + outer_weight) * inner
def _slanted_integral_image(double[:, ::1] image,
double[:, ::1] integral_img):
def _slanted_integral_image(double[:, :] image,
double[:, :] integral_img):
cdef Py_ssize_t i, j
cdef double[:] left_sum = np.zeros(image.shape[0], dtype=np.float)
@@ -33,8 +33,10 @@ def _slanted_integral_image(double[:, ::1] image,
left_sum[image.shape[1] - 1 - i] = np.sum(flipped_lr.diagonal(i))
left_sum_np = np.asarray(left_sum)
# Initializing the leftmost column of the slanted integral image
left_sum_np = left_sum_np.cumsum(0)
# Initializing the rightmost column of the slanted integral image
right_sum_np = np.sum(image, 1).cumsum(0)
for i in range(image.shape[0]):
@@ -50,32 +52,51 @@ def _slanted_integral_image(double[:, ::1] image,
integral_img[i, j] += integral_img[i, j - 1] + integral_img[i - 1, j + 1] - integral_img[i - 1, j]
def _censure_octagon_loop(double[:, ::1] image, double[:, ::1] integral_img,
double[:, ::1] integral_img1,
double[:, ::1] integral_img2,
double[:, ::1] integral_img3,
double[:, ::1] integral_img4,
double[:, ::1] filtered_image,
def _censure_octagon_loop(double[:, :] image, double[:, :] integral_img,
double[:, :] integral_img1,
double[:, :] integral_img2,
double[:, :] integral_img3,
double[:, :] integral_img4,
double[:, :] filtered_image,
double outer_weight, double inner_weight,
int mo, int no, int mi, int ni):
cdef Py_ssize_t i, j, o_m, i_m, o_set, i_set
"""
For a (5, 2) octagon, i.e. mo = 5 and no = 2,
|---o_set---|
[0, 0, 1, 1, 1, 1, 1, 0, 0]
[0, 1, 1, 1, 1, 1, 1, 1, 0]
[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, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1]
[0, 1, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 1, 1, 1, 1, 1, 0, 0]
|-o_m-|
"""
o_m = (mo - 1) / 2
i_m = (mi - 1) / 2
# o_set and i_set are the distances of the center of the octagon
# from the horizontal or vertical sides of the octagon,
# for outer and inner octagon respectively
o_set = o_m + no
i_set = i_m + ni
for i in range(o_set + 1, image.shape[0] - o_set - 1):
for j in range(o_set + 1, image.shape[1] - o_set - 1):
# Calculating the sum of pixels in the outer octagon
outer = integral_img1[i + o_set, j + o_m] - integral_img1[i + o_m - 1, j + o_set + 1] - integral_img[i + o_set, j - o_m] + integral_img[i + o_m - 1, j - o_m]
outer += integral_img[i + o_m - 1, j + o_m - 1] - integral_img[i - o_m, j + o_m - 1] - integral_img[i + o_m - 1, j - o_m] + integral_img[i - o_m, j - o_m]
outer += integral_img4[i + o_m, j - o_set] - integral_img4[i + o_set + 1, j - o_m + 1] - integral_img[i - o_m, j - o_m] + integral_img[i - o_m, j - o_set - 1]
outer += integral_img2[i - o_set, j - o_m] - integral_img2[i - o_m + 1, j - o_set - 1] - integral_img[i - o_m, -1] - integral_img[i - o_set - 1, j + o_m - 1] + integral_img[i - o_m, j + o_m - 1] + integral_img[i - o_set - 1, -1]
outer += integral_img3[i - o_m, j + o_set] - integral_img3[i - o_set - 1, j + o_m - 1] - integral_img[-1, j + o_set] - integral_img[i + o_m - 1, j + o_m - 1] + integral_img[-1, j + o_m - 1] + integral_img[i + o_m - 1, j + o_set]
# Calculating the sum of pixels in the inner octagon
inner = integral_img1[i + i_set, j + i_m] - integral_img1[i + i_m - 1, j + i_set + 1] - integral_img[i + i_set, j - i_m] + integral_img[i + i_m - 1, j - i_m]
inner += integral_img[i + i_m - 1, j + i_m - 1] - integral_img[i - i_m, j + i_m - 1] - integral_img[i + i_m - 1, j - i_m] + integral_img[i - i_m, j - i_m]
inner += integral_img4[i + i_m, j - i_set] - integral_img4[i + i_set + 1, j - i_m + 1] - integral_img[i - i_m, j - i_m] + integral_img[i - i_m, j - i_set - 1]