Skip to content

Instantly share code, notes, and snippets.

@fartagaintuxedo
Created August 11, 2016 16:40
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 fartagaintuxedo/46a5075577d0c501c872149c9593454b to your computer and use it in GitHub Desktop.
Save fartagaintuxedo/46a5075577d0c501c872149c9593454b to your computer and use it in GitHub Desktop.
from math import hypot, pi, cos, sin
from PIL import Image
import numpy as np
def hough(im, ntx=460, mry=360):
"Calculate Hough transform."
pim = im.load()
nimx, mimy = im.size
mry = int(mry/2)*2 #Make sure that this is even
him = Image.new("L", (ntx, mry), 255)
phim = him.load()
rmax = hypot(nimx, mimy)
dr = rmax / (mry/2)
dth = pi / ntx
for jx in xrange(nimx):
for iy in xrange(mimy):
col = pim[jx, iy]
if col < 1: continue #when color is black (0)
for jtx in xrange(ntx):
th = dth * jtx
r = jx*cos(th) + iy*sin(th)
iry = mry/2 + int(r/dr+0.5)
phim[jtx, iry] -= 1 #update values in the accumulator (here we are subtracting 1 instead of adding, idea is the same)
return him
def test():
"Test Hough transform image previously obtained form Canny Edge Detection."
im = Image.open("mycanny.jpg").convert("L")
him = hough(im)
him.save("ho5.bmp")
if __name__ == "__main__": test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment