2012-11-04 37 views
0

我試圖通過查看相鄰像素來刪除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)訪問numpy數組中的每個元素

這裏是循環:

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 
+0

什麼是x?你可以包含你實際使用的代碼循環(或者它的縮減版本)嗎?此外,我建議看看你是否可以使用合成numpy唯一的數據來觸發相同的行爲。 –

+0

x是一個計數器,逐步穿過陣列中的像素。我將在上面編輯以包含循環。 – TfromNYC

+0

你可以發佈你的異常的完整回溯?這應該有助於我們看到它來自哪裏。 – Blckknght

回答

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)