Demo entry 6327462

HT

   

Submitted by anonymous on Nov 23, 2016 at 23:28
Language: Python 3. Code size: 1.0 kB.

class HoughTransformer:
    def __init__(self):
        self._thetas = np.deg2rad(np.arange(-90, 90))
        self._cos_thetas = np.cos(self._thetas)
        self._sin_thetas = np.sin(self._thetas)

    @prepare_image_to_hough
    def line(self, img):
        diagonal_len = int(np.ceil(
            np.sqrt(img.width*img.width + img.height*img.height)
        ))
        rhos = np.linspace(-diagonal_len, diagonal_len, diagonal_len*2)
        accumulator = np.zeros(
                shape=(2*diagonal_len, len(self._thetas)),
                dtype=np.uint64,
            )
        ys, xs = np.nonzero(np.asarray(img))
        for i in range(len(ys)):
            x = xs[i]
            y = ys[i]
            for theta_index in range(len(self._thetas)):
                rho = int(round(x*self._cos_thetas[theta_index] +
                          y*self._sin_thetas[theta_index]) + diagonal_len)
                accumulator[rho, theta_index] += 1
        return accumulator, rhos

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).