访问numpy数组中的每个元素

时间:2012-11-04 03:35:08

标签: python image-processing numpy pygame

我试图通过查看相邻的像素来移除alpha通道蒙版中的杂散白像素,但一直在获取TypeEror:'numpy.ndarray'对象不可调用 我使用pygame.surfarray创建了一个数组: alphachannel3d = pygame.surfarray.pixels3d(alphachannel) 然后尝试逐步执行它并将像素值从白色更改为黑色: if (alphachannel3d[x] == (0, 0, 0)) & (alphachannel3d[x + 3] == (255, 255, 255)) & (alphachannel3d[x -3]==(255, 255, 255)): alphachannel3d[x] = (255, 255, 255)

这是循环:

x=1

while 1:

  count = x
  print 'count is', count

  print 'waiting 5 seconds'
  pygame.time.wait(5000)


  img = cam.get_image()

  imgarray = pygame.PixelArray(img)

  alphachannelarray = basearray.compare(imgarray, distance=0.09, weights=(0.01, 0.01, 0.01))

  alphachannel = alphachannelarray.make_surface()

  alphachannel3d = pygame.surfarray.pixels3d(alphachannel)


  if (alphachannel3d[x] == (0, 0, 0)) & (alphachannel3d[x + 3] == (255, 255, 255)) & (alphachannel3d[x -3]==(255, 255, 255)):
    alphachannel3d[x] = (255, 255, 255)

  alphachannel = pygame.surfarray.make_surface(alphachannel3d)
  srfcScreen.blit(alphachannel, (0,0))
  print 'screen blitted'

  pygame.display.flip()
  print 'display flipped'

  x = x+1

1 个答案:

答案 0 :(得分:0)

根据您的评论,我将提供以下推测性答案(未经测试):

if np.all(alphachannel3d[x] == (0, 0, 0)) & np.all(alphachannel3d[x + 3] == (255, 255, 255)) & np.all(alphachannel3d[x -3]==(255, 255, 255)):
    alphachannel3d[x] = (255, 255, 255)