python - How to iterate through a pygame 3d surfarray and change the individual color of the pixels if they're less than a specific value? -


i'm trying iterate pygame 3d surfarray, more pygame.surfarray.array3d("your image"). i'm receiving video captured webcam converting them 3d array, displaying onto window code.

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()          event in pygame.event.get():             if event.type == pygame.quit:                 sys.exit() 

my problem i'm trying have camera display pixel has amount of blue > 200 (ranging 0 - 255) , change color value of other pixels 0. i've tried using if statement array error stating should using any() or all().

all code:

try:     import pygame     import pygame.camera     import pygame.surfarray     import numpy     import os     import sys     import time except:     print("there 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()              event in pygame.event.get():                 if event.type == pygame.quit:                     sys.exit()    eyesawake = aaivision('', '', '', '')    if __name__ == "__main__":       eyesawake.startup()       eyesawake.camerasee() 

sorry of indentation errors, i'm not sure how use in text code blocks xd

rather iterating on pixels in list comprehension (which bound slow in python) , converting list result desired numpy array, can "vectorize" problem using magic of numpy. convenient here, since pygame.surfarray.array3d returns numpy array!

here possible solution takes image (loaded disk; not code work since relies on linux directories /dev/video0 webcam input , espeak command, unavailable in windows):

import numpy np import pygame import sys  if __name__ == "__main__":     pygame.init()     screen = pygame.display.set_mode((800, 600))     img = pygame.image.load("test.jpg").convert_alpha()     clock = pygame.time.clock()      while true:         event in pygame.event.get():             if event.type == pygame.quit:                 pygame.quit()                 sys.exit()          data = pygame.surfarray.array3d(img)         mask = data[..., 2] < 200 #mark super-not-blue pixels true (select opposite!)         masked = data.copy() #make copy of original image data         masked[mask] = [0, 0, 0] #set of true pixels black (rgb: 0, 0, 0)         out = pygame.surfarray.make_surface(masked) #convert numpy array surface         screen.blit(out, (0, 0))         pygame.display.update()         print clock.get_fps()         clock.tick(60) 

on computer, runs @ ~30 fps, which, while not great, should significant improvement! slowness here appears due masked[mask] = [0, 0, 0] in particular. if numpy experts chime in, terrific! otherwise, can chime in additional cython answer instead (where adding type data should improve loop performance).


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -