我有一个霍夫变换,试图查找其中中心和半径均未知的圆,因此累加器空间为3维。

DIMENSION = 200
R_DIMENSION = 200

xbins = np.linspace(-0.5,0.5,DIMENSION)
ybins = np.linspace(-0.5,0.5,DIMENSION)
rbins = np.linspace(0,0.5, R_DIMENSION)

x,y,r = np.broadcast_arrays( xbins[...,np.newaxis,np.newaxis], \
                               ybins[np.newaxis,...,np.newaxis], \
                               rbins[np.newaxis,np.newaxis,...])
score = 0
circle_counter = 0
while True:
  weights = np.zeros( (DIMENSION, DIMENSION, R_DIMENSION))
  for x0,y0 in data['allPoints']:
    s = 0.001
    eta = (x-x0)**2 + (y-y0)**2 - r**2
    weights += 1. / ( sqrt( 2 * sconst.pi ) * s ) * np.exp( -( eta ** 2 )\
                                                              / ( 2 * s ** 2 ) )
  index = np.argmax( weights )
  ii,jj,rr = np.unravel_index( index, (DIMENSION, DIMENSION, R_DIMENSION))
  score = weights[ii][jj][rr]
  if score < 200:
    break


现在,由于某种原因,如果我想可视化rr的x,y空间,我不会使用未拆散的索引来确定实际半径和中心得到分数最高的平面,但在绘制时会给我正确的结果。

circle = {}
circle['center'] = (xbins[ii], ybins[jj])
circle['radius'] = rbins[rr]
circles.append(circle)

plt.imshow(weights[:][:][rr])
plt.colorbar()
plt.show()


所以我的问题是我是否误解了如何显示给定半径索引的x,y平面?

这是可视化的两张图片。第二个包含实际最大值。在两张图片中,令我感到困惑的是,这些线条是弯曲的,这意味着它看起来并不是固定半径的。

第一张图片是用imshow(weights[:][:][rr])创建的,第二张图片是一系列的

for r_i in range(R_DIMENSION):
  imshow(weights[:][:][r_i])


然后我找到了得分最高的那个。
python - 从3D ndarray显示切片-LMLPHP

python - 从3D ndarray显示切片-LMLPHP

实际上,我期望像这样(这是从我知道半径的2D霍夫变换中得出的):
python - 从3D ndarray显示切片-LMLPHP

最佳答案

好的,我找到了一个解决方案,但是如果有人可以向我解释一下,那就太好了。

我必须更改的是如何广播阵列:

x,y,r = np.broadcast_arrays( xbins[np.newaxis,...,np.newaxis], \
                             ybins[np.newaxis,np.newaxis,...], \
                             rbins[...,np.newaxis,np.newaxis])


当然,然后在我使用索引/维度时更改它们的顺序。

while True:
  weights = np.zeros( (R_DIMENSION, DIMENSION, DIMENSION))
  for x0,y0 in data['allPoints']:
    s = 0.001
    eta = (x-x0)**2 + (y-y0)**2 - r**2
    weights += 1. / ( sqrt( 2 * sconst.pi ) * s ) * np.exp( -( eta ** 2 )\
                                                            / ( 2 * s ** 2 ) )
  index = np.argmax( weights )
  rr,ii,jj = np.unravel_index( index, (R_DIMENSION, DIMENSION, DIMENSION))
  score = weights[rr][ii][jj]
  if score < 200:
    print 'finished after %s circle(s) found' % circle_counter
    break


然后,x,y平面的不同半径的动画看起来像this

10-08 15:19