davidhood2
davidhood2

Reputation: 1367

Hough Transform in Python - Results incorrectly offset - index error?

I'm writing a basic Hough Transform in Python - I believe I have got it conceptually correct however, my result is appearing to be offset such that it is split top and bottom, rather than continuous. What I want to get should look like this:

enter image description here

But I get this:

enter image description here

Which is close, but seems to be split badly through the middle! I'm convinced this is due to my indexing of the rho/theta arrays, however despite my many alterations, I cannot get this to fix! Any explanation of my erroneous step and what I need to change would be very gratefully appreciated!

My source code should be complete and run directly...

Thanks very much

David

Source

import numpy as np
import matplotlib.pyplot as mpl

cols, rows = [256,256]  # Set size of image 
grey_levels = 256 #Grey levels in image
testPixels = [[0 for x in range(rows)] for y in range(cols)]  # Convert to black and white
testPixels[100][100] = 255 #Set 3 pixels to white
testPixels[200][200] = 255
testPixels[150][150] = 255

rho_size = int(np.sqrt(rows**2 + cols**2)) #Max possible rho is diagonal dist.
angle_size = 360 #Test all angles

houghspace = [[0 for x in range(rho_size)] for y in range(angle_size)]  # Create hough space array

for x in range(rows):  # For each rows
    for y in range(cols):  # For each cols
        if testPixels[x][y] == 0: #Skip if not edge point
            continue
        for theta in range(angle_size):
            rho = int(x*np.cos(np.deg2rad(theta)) + y*np.sin(np.deg2rad(theta)))
            houghspace[theta][rho] = 255
houghspace = [list(a) for a in zip(*houghspace)] #Transpose to get angle on x axis

fig = mpl.figure()  # Create a figure
fig.add_subplot(1, 2, 1).set_title("Original")
mpl.imshow(np.uint8(np.dstack((testPixels,testPixels,testPixels))),cmap='Greys')
fig.add_subplot(1, 2, 2).set_title("Hough Transform")
mpl.imshow(np.uint8(np.dstack((houghspace, houghspace, houghspace))),cmap='Greys')
mpl.show()

Upvotes: 2

Views: 598

Answers (1)

kmario23
kmario23

Reputation: 61455

You've mixed up the indices when you create houghspace as a list of lists. Please prefer using numpy arrays as it will make things much clearer with indices. Along the x-axis, the angle theta changes and along the y-axis the rho changes. But, you've got it the other way when defining houghspace using list comprehension.

Below is the correct code. Note the comments starting with ##

rho_size = int(np.sqrt(rows**2 + cols**2)) #Max possible rho is diagonal dist.
angle_size = 360 #Test all angles

##houghspace = [[0 for y in range(angle_size) for x in range(2*rho_size)]]  #buggy
houghspace = [[0 for x in range(angle_size)] for y in range(rho_size*2)]   #correct
## Also double the rho_size, so that both crust and trough of sinusoidal is visible

for x in range(rows):  # For each rows
    for y in range(cols):  # For each cols
        if testPixels[x][y] == 0: #Skip if not edge point
            continue
        for theta in range(angle_size):
            rho = int(x*np.cos(np.deg2rad(theta)) + y*np.sin(np.deg2rad(theta))) \
                                                  + rho_size  ## also add rho_size
            ##houghspace[theta][rho] = 255   ## buggy
            houghspace[rho][theta] += 255    # <==== indices switched & it's += 
##houghspace = [list(a) for a in zip(*houghspace)] 
##Transposing not needed now (we switched indices)

fig = mpl.figure()  # Create a figure
fig.add_subplot(1, 2, 1).set_title("Original")
mpl.imshow(np.uint8(np.dstack((testPixels,testPixels,testPixels))),cmap='Greys')
fig.add_subplot(1, 2, 2).set_title("Hough Transform")
mpl.imshow(np.uint8(np.dstack((houghspace, houghspace, houghspace))),cmap='Greys')
mpl.show()

I get the following plot:

enter image description here

Upvotes: 1

Related Questions