显示三维阵列中的切片

2024-06-26 14:05:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Hough变换,它试图找到圆心和半径都未知的圆,所以累加器空间是三维的。你知道吗

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

然后我找到得分最高的那个。 Slice with 3rd dimension fixed with <code>rr</code>

real maximum

实际上,我期望这样的东西(这是一个二维hough变换,我知道半径): enter image description here


Tags: indexnprr半径iiscoredimensionweights
1条回答
网友
1楼 · 发布于 2024-06-26 14:05:38

好吧,我找到了一个解决办法,但如果有人能给我解释一下那就太好了。你知道吗

我要改变的是我如何广播阵列:

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。你知道吗

相关问题 更多 >