我試圖迭代一個pygame 3d surfarray,更具體地說pygame.surfarray.array3d("your image")
。我正在接收從網絡攝像頭捕獲的視頻,然後將它們轉換成3D數組,然後使用此代碼將其顯示在我的窗口上。如何遍歷pygame 3d surfarray並更改像素的個別顏色,如果它們小於特定值?
def cameraSee(self):
while True:
self.cam.query_image()
self.image = self.cam.get_image()
self.imageArr = pygame.surfarray.array3d(self.image)
pygame.surfarray.blit_array(self.screen,self.imageArr)
os.system("clear")
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
我的問題是我想要有我的相機只顯示其具有藍色> 200(範圍從0 - 255)的量的任何像素和改變所有其他像素的顏色值設置爲0。我試着用數組的if語句,但是我得到一個錯誤,指出我應該使用any()
或all()
。
我的所有代碼:
try:
import pygame
import pygame.camera
import pygame.surfarray
import numpy
import os
import sys
import time
except:
print("there was an error importing modules...")
os.system("espeak 'there, was, an, error, importing, modules'")
time.sleep(2)
class aaiVision(object):
def __init__(self,screen,cam,image,imageArr):
self.screen = screen
self.cam = cam
self.image = image
self.imageArr = imageArr
def startUp(self):
os.system("espeak 'eh, eh, i, vision, initializing'")
pygame.init()
pygame.camera.init()
time.sleep(1)
os.system("espeak 'Vision, initialized'")
camList = pygame.camera.list_cameras()
print(camList)
time.sleep(1)
os.system("espeak 'cameras, found, %s'" % str(len(camList)))
time.sleep(1)
self.screen = pygame.display.set_mode((640,480))
time.sleep(0.5)
self.cam = pygame.camera.Camera("/dev/video0",(640,480),"YUV")
time.sleep(0.5)
self.cam.start()
os.system("espeak 'eh, eh, i, vision, online'")
def cameraSee(self):
while True:
self.cam.query_image()
self.image = self.cam.get_image()
self.imageArr = pygame.surfarray.array3d(self.image)
pygame.surfarray.blit_array(self.screen,self.imageArr)
os.system("clear")
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
eyesAwake = aaiVision('', '', '', '')
if __name__ == "__main__":
eyesAwake.startUp()
eyesAwake.cameraSee()
對不起一些縮進錯誤的,我不知道如何使用文本代碼塊XD
你從哪裏得到一個錯誤,指出我應該使用'any()'或'all()'?請[編輯]你的問題,幷包括完整的追溯導致發生這種情況。您可能無法正確縮進代碼,因爲您使用製表符而不是空格來執行此操作。 – martineau