Python中的Hough变换-结果偏移不正确-索引错误?
我正在用Python编写一个基本的Hough变换——我相信我在概念上是正确的,但是,我的结果似乎是偏移的,因此它是上下分开的,而不是连续的。我想要得到的应该是这样的: 但我明白了: 这很接近,但似乎是严重分裂通过中间!我确信这是因为我对ρ/θ数组进行了索引,尽管我做了很多修改,但我无法修复它!如果您能解释我的错误步骤以及我需要更改的内容,我们将不胜感激 我的源代码应该是完整的,直接运行 非常感谢 大卫 来源Python中的Hough变换-结果偏移不正确-索引错误?,python,numpy,image-processing,indexing,hough-transform,Python,Numpy,Image Processing,Indexing,Hough Transform,我正在用Python编写一个基本的Hough变换——我相信我在概念上是正确的,但是,我的结果似乎是偏移的,因此它是上下分开的,而不是连续的。我想要得到的应该是这样的: 但我明白了: 这很接近,但似乎是严重分裂通过中间!我确信这是因为我对ρ/θ数组进行了索引,尽管我做了很多修改,但我无法修复它!如果您能解释我的错误步骤以及我需要更改的内容,我们将不胜感激 我的源代码应该是完整的,直接运行 非常感谢 大卫 来源 import numpy as np import matplotlib.pyplo
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(行**2+列**2))#最大可能的rho是对角线距离。
角度_尺寸=360#测试所有角度
##houghspace=[[0表示范围内的y(角度大小),x表示范围内的x(2*rho大小)]]#buggy
houghspace=[[0表示范围内的x(角度大小)]表示范围内的y(角度大小*2)]#正确
##同时将rho_大小增加一倍,使地壳和正弦槽都可见
对于范围内的x(行):#对于每行
对于范围内的y(cols):#对于每个cols
如果testPixels[x][y]==0:#如果不是边缘点,则跳过
持续
对于范围内的θ(角度大小):
rho=int(x*np.cos(np.deg2rad(theta))+y*np.sin(np.deg2rad(theta)))\
+rho#u大小##也可以添加rho#u大小
##houghspace[theta][rho]=255辆四轮马车
houghspace[rho][theta]+=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 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()