Python 中的霍夫变换 - 结果偏移不正确 - 索引错误?

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

我正在 Python 中编写一个基本的 Hough 变换 - 我相信我在概念上是正确的,但是,我的结果似乎是偏移的,因此它是顶部和底部分开的,而不是连续的。我想要得到的应该是这样的:

但我明白了:

很接近,但中间好像裂得很厉害!我确信这是由于我对 rho/theta 数组进行了索引,但是尽管我进行了许多更改,但我无法解决这个问题!非常感谢任何对我的错误步骤和我需要更改的解释!

我的源码应该是完整的运行直接...

非常感谢

大卫

来源

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()

当您创建 houghspace 作为列表的列表时,您混淆了索引。请更喜欢使用 numpy 数组,因为它会使索引更清晰。沿 x 轴,角度 theta 发生变化,沿 y 轴,角度 rho 发生变化。但是,在使用列表推导定义 houghspace 时,你已经得到了另一种方式。

下面是正确的代码。注意以 ##

开头的注释
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()

我得到以下情节: