From dc83c64d44ca8296f1757321fad9603bad79a676 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 3 Mar 2013 15:37:45 +0100 Subject: [PATCH 1/5] Change data type of index variables --- skimage/transform/_hough_transform.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/transform/_hough_transform.pyx b/skimage/transform/_hough_transform.pyx index 29a38eb7..a6ce6c27 100644 --- a/skimage/transform/_hough_transform.pyx +++ b/skimage/transform/_hough_transform.pyx @@ -50,11 +50,11 @@ def _hough_circle(cnp.ndarray img, \ x, y = np.nonzero(img) # Offset the image - cdef int max_radius = radius.max() + cdef Py_ssize_t max_radius = radius.max() x = x + max_radius y = y + max_radius - cdef int px, py + cdef Py_ssize_t px, py cdef cnp.ndarray[ndim=1, dtype=cnp.intp_t] tx, ty, circle_x, circle_y cdef cnp.ndarray acc = np.zeros((radius.size, img.shape[0] + 2 * max_radius, From dec104e9ffb5514ea5cf6c8423d64acbe9831a39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 4 Mar 2013 15:00:37 +0100 Subject: [PATCH 2/5] Improve performance and usage of data types of hough circle transformation --- skimage/transform/_hough_transform.pyx | 30 +++++++++++++++----------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/skimage/transform/_hough_transform.pyx b/skimage/transform/_hough_transform.pyx index a6ce6c27..e0c004a3 100644 --- a/skimage/transform/_hough_transform.pyx +++ b/skimage/transform/_hough_transform.pyx @@ -20,10 +20,9 @@ cdef inline Py_ssize_t round(double r): return ((r + 0.5) if (r > 0.0) else (r - 0.5)) -@cython.boundscheck(False) def _hough_circle(cnp.ndarray img, \ cnp.ndarray[ndim=1, dtype=cnp.intp_t] radius, \ - normalize=True): + char normalize=True): """Perform a circular Hough transform. Parameters @@ -49,32 +48,39 @@ def _hough_circle(cnp.ndarray img, \ cdef cnp.ndarray[ndim=1, dtype=cnp.intp_t] x, y x, y = np.nonzero(img) + cdef Py_ssize_t num_pixels = x.size + # Offset the image cdef Py_ssize_t max_radius = radius.max() x = x + max_radius y = y + max_radius - cdef Py_ssize_t px, py - cdef cnp.ndarray[ndim=1, dtype=cnp.intp_t] tx, ty, circle_x, circle_y - cdef cnp.ndarray acc = np.zeros((radius.size, - img.shape[0] + 2 * max_radius, - img.shape[1] + 2 * max_radius)) + cdef Py_ssize_t i, p, c, num_circle_pixels, tx, ty + cdef cnp.ndarray[ndim=1, dtype=cnp.intp_t] circle_x, circle_y + + cdef cnp.ndarray[ndim=3, dtype=cnp.double_t] acc = \ + np.zeros((radius.size, + img.shape[0] + 2 * max_radius, + img.shape[1] + 2 * max_radius), dtype=np.double) for i, rad in enumerate(radius): # Store in memory the circle of given radius # centered at (0,0) circle_x, circle_y = circle_perimeter(0, 0, rad) + num_circle_pixels = circle_x.size + # For each non zero pixel - for (px, py) in zip(x, y): + for p in range(num_pixels): # Plug the circle at (px, py), # its coordinates are (tx, ty) - tx = circle_x + px - ty = circle_y + py - acc[i, tx, ty] += 1 + for c in range(num_circle_pixels): + tx = circle_x[c] + x[p] + ty = circle_y[c] + y[p] + acc[i, tx, ty] += 1 if normalize: - acc[i] = acc[i] / len(circle_x) + acc[i, :, :] /= num_circle_pixels return acc From 2e3f82e9d9dd4ce73b3b37c6c6aa566b64588d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 4 Mar 2013 15:01:03 +0100 Subject: [PATCH 3/5] Remove unnecessary statement breaks --- skimage/transform/_hough_transform.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skimage/transform/_hough_transform.pyx b/skimage/transform/_hough_transform.pyx index e0c004a3..97ff720b 100644 --- a/skimage/transform/_hough_transform.pyx +++ b/skimage/transform/_hough_transform.pyx @@ -20,8 +20,8 @@ cdef inline Py_ssize_t round(double r): return ((r + 0.5) if (r > 0.0) else (r - 0.5)) -def _hough_circle(cnp.ndarray img, \ - cnp.ndarray[ndim=1, dtype=cnp.intp_t] radius, \ +def _hough_circle(cnp.ndarray img, + cnp.ndarray[ndim=1, dtype=cnp.intp_t] radius, char normalize=True): """Perform a circular Hough transform. From 2237cc1617f62618a78a8b1bc1a2b19b8106274f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 4 Mar 2013 15:02:09 +0100 Subject: [PATCH 4/5] Change color of circles for better visibility --- doc/examples/plot_circular_hough_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/examples/plot_circular_hough_transform.py b/doc/examples/plot_circular_hough_transform.py index fc891d04..d2f8f2ae 100755 --- a/doc/examples/plot_circular_hough_transform.py +++ b/doc/examples/plot_circular_hough_transform.py @@ -66,7 +66,7 @@ for idx in np.argsort(accums)[::-1][:5]: center_x, center_y = centers[idx] radius = radii[idx] cx, cy = circle_perimeter(center_y, center_x, radius) - image[cy, cx] = (220, 250, 150) + image[cy, cx] = (220, 20, 20) ax.imshow(image, cmap=plt.cm.gray) plt.show() From 4925205082011dda230e7690f17042763a87b281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Mon, 4 Mar 2013 16:26:06 +0100 Subject: [PATCH 5/5] Improve performance of normalization --- skimage/transform/_hough_transform.pyx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/skimage/transform/_hough_transform.pyx b/skimage/transform/_hough_transform.pyx index 97ff720b..2b1acc7c 100644 --- a/skimage/transform/_hough_transform.pyx +++ b/skimage/transform/_hough_transform.pyx @@ -56,6 +56,7 @@ def _hough_circle(cnp.ndarray img, y = y + max_radius cdef Py_ssize_t i, p, c, num_circle_pixels, tx, ty + cdef double incr cdef cnp.ndarray[ndim=1, dtype=cnp.intp_t] circle_x, circle_y cdef cnp.ndarray[ndim=3, dtype=cnp.double_t] acc = \ @@ -70,6 +71,11 @@ def _hough_circle(cnp.ndarray img, num_circle_pixels = circle_x.size + if normalize: + incr = 1.0 / num_circle_pixels + else: + incr = 1 + # For each non zero pixel for p in range(num_pixels): # Plug the circle at (px, py), @@ -77,10 +83,7 @@ def _hough_circle(cnp.ndarray img, for c in range(num_circle_pixels): tx = circle_x[c] + x[p] ty = circle_y[c] + y[p] - acc[i, tx, ty] += 1 - - if normalize: - acc[i, :, :] /= num_circle_pixels + acc[i, tx, ty] += incr return acc