Skip to content

Instantly share code, notes, and snippets.

@tacaswell
Last active December 21, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tacaswell/6242663 to your computer and use it in GitHub Desktop.
Save tacaswell/6242663 to your computer and use it in GitHub Desktop.
Tweaked version of code at http://mail.scipy.org/pipermail/scipy-user/attachments/20060803/ad246212/attachment.py with minor bug fix and improved documentation.
## Copyright (C) 2006 Stefan van der Walt <stefan@sun.ac.za>
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are
## met:
##
## 1. Redistributions of source code must retain the above copyright
## notice, this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright
## notice, this list of conditions and the following disclaimer in
## the documentation and/or other materials provided with the
## distribution.
##
## THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
## DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
## INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
## (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
## STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
## IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.
## Modified 2013-08-14 by Thomas A Caswell tcaswell@gmail.com
import numpy as np
itype = np.uint16 # See ticket 225
def houghtf(img, angles=None):
"""Perform the straight line Hough transform.
Parameters
----------
img : a boolean np.ndarray
The image to be processed
angles : np.ndarray
The angles to process in degrees
Returns
-------
H : np.ndarray
the Hough transform coefficients, columns are fixed :math:`\theta`,
rows are fixed :math:`\rho`
angles : np.ndarray
The :math:`\theta` values
distances : np.ndarray
The :math:`\rho` values
"""
if img.ndim != 2:
raise ValueError("Input must be a two-dimensional array")
img = img.astype(bool)
# numpy did not like `if angles` -> truth value of array ambiguous
if angles is None:
angles = np.linspace(-90,90,180)
theta = angles / 180. * np.pi
d = np.ceil(np.hypot(*img.shape))
# can't just change this number due to implicit hashing below
nr_bins = 2*d - 1
bins = np.linspace(-d,d,nr_bins)
out = np.zeros((nr_bins,len(theta)),dtype=itype)
rows,cols = img.shape
x,y = np.mgrid[:rows,:cols]
for i,(cT,sT) in enumerate(zip(np.cos(theta),np.sin(theta))):
# this line is tricksy implicit hashing
rho = np.round_(cT*x[img] + sT*y[img]) - bins[0] + 1
rho = rho.astype(itype)
rho[(rho < 0) | (rho > nr_bins)] = 0
bc = np.bincount(rho.flat)[1:]
out[:len(bc),i] = bc
return out,angles,bins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment