1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

line detection

Posted by Alexander Lysov on Apr 03, 2010



Hi there,
I'm experimenting with image recognition in Nodebox 1.9.5.
This code detects lines in small images up to 200x200px, but doesn't work with big or edge-detected images:

def houghTransform(im):
    width, height = im.size
    size(width, height)
    rmax = int(round(math.sqrt(height**2 + width**2)))
    print rmax
    acc = zeros((rmax, 180))
    for x,y in [(x,y) for x in range(1, width) for y in range(1, height)]:
        if im.getpixel((x,y)) == 255:
            for m in range(1, 180):
                arg = (m*math.pi) / 180
                r = int(round( (x * math.cos(arg)) + (y * math.sin(arg)) ))
                if 0 < r < rmax:
                    acc[r][m] += 1
    max = 0
    for x,y in [(x,y) for x in range(rmax) for y in range(180)]:
        if acc[x][y] > max:
            max = acc[x][y];
            r, theta = (x,y)
   
    piTheta180 = (math.pi * theta)/180
    cosTheta = math.cos(piTheta180)
    sinTheta = math.sin(piTheta180)
 
    if (r/sinTheta) > height:
        m = -1 * math.tan(piTheta180)
        c = int(round(r/cosTheta))
        upper = (c,0)
        lower = (int(round(abs((m*512)+c))),512)
    else:
        m = (-1.0) / math.tan(piTheta180)
        c = int(round(r/sinTheta))
        c2 = int(round(r/cosTheta))
        upper = (c2,0)
        lower = (0,c)
    return upper, lower, r*cosTheta, r*sinTheta
 
import math
from Numeric import zeros    
 
img = "image.jpg"
 
from PIL import Image
im = Image.open(img)
im.show()
points = houghTransform(im)
 
upper = points[0]
ux = upper[0]
uy = upper[1]
lower = points[1]
lx = lower[0]
ly = lower[1]
 
print points, upper, ux, uy, lx, ly
stroke(0)
line(ux,uy,lx,ly)
Needs PIL.
Can you help fide the bug?


 
Posted by Alexander Lysov on Apr 03, 2010

Oh, it simply works only with one-channel grayscale images.