diff --git a/scikits/image/transform/_hough_transform.pyx b/scikits/image/transform/_hough_transform.pyx index eeb4bb0d..ddcf91f6 100644 --- a/scikits/image/transform/_hough_transform.pyx +++ b/scikits/image/transform/_hough_transform.pyx @@ -1,11 +1,13 @@ cimport cython - import numpy as np cimport numpy as np from random import randint np.import_array() - +cdef extern from "stdlib.h": + int rand() + void randomize() + cdef extern from "math.h": int abs(int) double fabs(double) @@ -95,7 +97,6 @@ def _probabilistic_hough(np.ndarray img, int value_threshold, int line_length, \ cdef int xflag, x0, y0, dx0, dy0, dx, dy, gap, x1, y1, good_line, count max_distance = 2 * ceil((sqrt(img.shape[0] * img.shape[0] + img.shape[1] * img.shape[1]))) - #max_distance = (img.shape[0] + img.shape[1]) * 2 accum = np.zeros((max_distance, theta.shape[0]), dtype=np.int64) offset = max_distance / 2 # find the nonzero indexes @@ -103,7 +104,6 @@ def _probabilistic_hough(np.ndarray img, int value_threshold, int line_length, \ y_idxs, x_idxs = np.nonzero(img) num_indexes = y_idxs.shape[0] # x and y are the same shape nthetas = theta.shape[0] - points = [] for i in range(num_indexes): points.append((x_idxs[i], y_idxs[i])) @@ -116,7 +116,7 @@ def _probabilistic_hough(np.ndarray img, int value_threshold, int line_length, \ count = len(points) if count == 0: break - index = randint(0, count-1) + index = rand() % (count) x = points[index][0] y = points[index][1] del points[index] diff --git a/scikits/image/transform/hough_transform.py b/scikits/image/transform/hough_transform.py index 45d44599..31ffa565 100644 --- a/scikits/image/transform/hough_transform.py +++ b/scikits/image/transform/hough_transform.py @@ -54,7 +54,7 @@ _py_hough = _hough # try to import and use the faster Cython version if it exists try: - from ._hough_transform import _hough + from ._hough_transform import _hough except ImportError: pass @@ -67,11 +67,16 @@ def probabilistic_hough(img, threshold=10, line_length=50, line_gap=10, theta=No img : (M, N) ndarray Input image with nonzero values representing edges. value_threshold: int - Threshold - theta :1D ndarray, dtype=double + Threshold + line_length: int, optional (default 50) + Minimum accepted length of detected lines. + Increase the parameter to extract longer lines. + line_gap: int, optional, (default 10) + Maximum gap between pixels to still form a line. + Increase the parameter to merge broken lines more aggresively. + theta :1D ndarray, dtype=double, optional, default (-pi/2 .. pi/2) Angles at which to compute the transform, in radians. - Defaults to -pi/2 .. pi/2 - + Returns ------- lines : list diff --git a/scikits/image/transform/tests/test_hough_transform.py b/scikits/image/transform/tests/test_hough_transform.py index 3d185521..175c1b37 100644 --- a/scikits/image/transform/tests/test_hough_transform.py +++ b/scikits/image/transform/tests/test_hough_transform.py @@ -14,7 +14,6 @@ def append_desc(func, description): return func from scikits.image.transform import * -import math def test_hough(): # Generate a test image @@ -56,7 +55,7 @@ def test_probabilistic_hough(): img[i, i] = 100 # decrease default theta sampling because similar orientations may confuse # as mentioned in article of Galambos et al - theta=np.linspace(0, math.pi, 45) + theta=np.linspace(0, np.pi, 45) lines = probabilistic_hough(img, theta=theta, threshold=10, line_length=10, line_gap=1) # sort the lines according to the x-axis sorted_lines = []