diff --git a/skimage/feature/_template.pyx b/skimage/feature/_template.pyx index 58d48524..938e3a18 100644 --- a/skimage/feature/_template.pyx +++ b/skimage/feature/_template.pyx @@ -42,12 +42,17 @@ from skimage._shared.transform cimport integrate @cython.boundscheck(False) def match_template(np.ndarray[float, ndim=2, mode="c"] image, np.ndarray[float, ndim=2, mode="c"] template): + cdef np.ndarray[float, ndim=2, mode="c"] corr cdef np.ndarray[float, ndim=2, mode="c"] image_sat cdef np.ndarray[float, ndim=2, mode="c"] image_sqr_sat cdef float template_mean = np.mean(template) cdef float template_ssd cdef float inv_area + cdef ssize_t r, c, r_end, c_end + cdef ssize_t template_rows = template.shape[0] + cdef ssize_t template_cols = template.shape[1] + cdef float den, window_sqr_sum, window_mean_sqr, window_sum image_sat = integral.integral_image(image) image_sqr_sat = integral.integral_image(image**2) @@ -63,24 +68,24 @@ def match_template(np.ndarray[float, ndim=2, mode="c"] image, mode="valid"), dtype=np.float32) - cdef int i, j - cdef float den, window_sqr_sum, window_mean_sqr, window_sum, - # move window through convolution results, normalizing in the process - for i in range(corr.shape[0]): - for j in range(corr.shape[1]): - # subtract 1 because `i_end` and `j_end` are used for indexing into - # summed-area table, instead of slicing windows of the image. - i_end = i + template.shape[0] - 1 - j_end = j + template.shape[1] - 1 - window_sum = integrate(image_sat, i, j, i_end, j_end) + # move window through convolution results, normalizing in the process + for r in range(corr.shape[0]): + for c in range(corr.shape[1]): + # subtract 1 because `i_end` and `c_end` are used for indexing into + # summed-area table, instead of slicing windows of the image. + r_end = r + template_rows - 1 + c_end = c + template_cols - 1 + + window_sum = integrate(image_sat, r, c, r_end, c_end) window_mean_sqr = window_sum * window_sum * inv_area - window_sqr_sum = integrate(image_sqr_sat, i, j, i_end, j_end) + window_sqr_sum = integrate(image_sqr_sat, r, c, r_end, c_end) if window_sqr_sum <= window_mean_sqr: - corr[i, j] = 0 + corr[r, c] = 0 continue den = sqrt((window_sqr_sum - window_mean_sqr) * template_ssd) - corr[i, j] /= den + corr[r, c] /= den + return corr diff --git a/skimage/feature/_texture.pyx b/skimage/feature/_texture.pyx index 70a446bb..e2e3bb35 100644 --- a/skimage/feature/_texture.pyx +++ b/skimage/feature/_texture.pyx @@ -39,7 +39,7 @@ def _glcm_loop(np.ndarray[dtype=np.uint8_t, ndim=2, """ cdef: np.int32_t a_inx, d_idx - np.int32_t r, c, rows, cols, row, col + ssize_t r, c, rows, cols, row, col np.int32_t i, j rows = image.shape[0] @@ -52,8 +52,8 @@ def _glcm_loop(np.ndarray[dtype=np.uint8_t, ndim=2, i = image[r, c] # compute the location of the offset pixel - row = r + (sin(angle) * distance + 0.5) - col = c + (cos(angle) * distance + 0.5); + row = r + (sin(angle) * distance + 0.5) + col = c + (cos(angle) * distance + 0.5); # make sure the offset is within bounds if row >= 0 and row < rows and \ @@ -123,11 +123,11 @@ def _local_binary_pattern(np.ndarray[double, ndim=2] image, output_shape = (image.shape[0], image.shape[1]) cdef np.ndarray[double, ndim=2] output = np.zeros(output_shape, np.double) - cdef int rows = image.shape[0] - cdef int cols = image.shape[1] + cdef ssize_t rows = image.shape[0] + cdef ssize_t cols = image.shape[1] cdef double lbp - cdef int r, c, changes, i + cdef ssize_t r, c, changes, i for r in range(image.shape[0]): for c in range(image.shape[1]): for i in range(P): diff --git a/skimage/feature/corner_cy.pyx b/skimage/feature/corner_cy.pyx index 2f001ea4..34913450 100644 --- a/skimage/feature/corner_cy.pyx +++ b/skimage/feature/corner_cy.pyx @@ -10,7 +10,7 @@ from skimage.color import rgb2grey from skimage.util import img_as_float -def corner_moravec(image, int window_size=1): +def corner_moravec(image, ssize_t window_size=1): """Compute Moravec corner measure response image. This is one of the simplest corner detectors and is comparatively fast but @@ -56,8 +56,8 @@ def corner_moravec(image, int window_size=1): [ 0., 0., 0., 0., 0., 0., 0.]]) """ - cdef int rows = image.shape[0] - cdef int cols = image.shape[1] + cdef ssize_t rows = image.shape[0] + cdef ssize_t cols = image.shape[1] cdef cnp.ndarray[dtype=cnp.double_t, ndim=2, mode='c'] cimage, out @@ -71,7 +71,7 @@ def corner_moravec(image, int window_size=1): cdef double* out_data = out.data cdef double msum, min_msum - cdef int r, c, br, bc, mr, mc, a, b + cdef ssize_t r, c, br, bc, mr, mc, a, b for r in range(2 * window_size, rows - 2 * window_size): for c in range(2 * window_size, cols - 2 * window_size): min_msum = DBL_MAX