Esempio n. 1
0
def main():
    mouseDown = False

    pygame.init()
    screen = makeWindow((600, 400))

    maxParticlesSlider = Slider(100, 1, 450, 10, 100, "maxParticles")
    gravityXSlider = Slider(0.5, -0.5, 450, 50, 100, "gravityX")
    gravityXSlider.t = 0.5
    gravityYSlider = Slider(0.5, -0.5, 450, 90, 100, "gravityY")
    gravityYSlider.t = 0.5
    frictionSlider = Slider(1, 0, 450, 130, 100, "friction")
    minSizeSlider = Slider(20, 5, 450, 170, 100, "minSize")
    thetaSlider = Slider(360, 0, 450, 210, 100, "theta")
    velocitySlider = Slider(10, 0.5, 450, 250, 100, "velocity")

    emitter = Emitter(1, 15, 20, 5, 0.15, Particle, [200, 200])

    walls = []
    walls.append(Wall([10, 390], [390, 390], True))
    walls.append(Wall([10, 10], [10, 390], True))
    walls.append(Wall([10, 10], [390, 10], False))
    walls.append(Wall([390, 390], [390, 10], True))
    walls.append(Wall([200, 390], [390, 200], True))

    running = True
    while running:
        screen.fill((0, 0, 0))
        emitter.update(round(maxParticlesSlider.getValue()), round(minSizeSlider.getValue()), round(thetaSlider.getValue()), velocitySlider.getValue())
        for particleI in emitter.particles:
            particleI.update(walls, (gravityXSlider.getValue(), gravityYSlider.getValue()), frictionSlider.getValue())
            pygame.draw.circle(screen, (255, 255, 255), (particleI.position.x, particleI.position.y), particleI.size, 1)
        
        for wall in walls:
            pygame.draw.line(screen, (255, 255, 255), (wall.start.x, wall.start.y), (wall.end.x, wall.end.y), 1)
        
        for wall in walls:
            pygame.draw.line(screen, (255, 0, 0), (wall.start.x, wall.start.y), (wall.start.x + wall.normal.x * 10, wall.start.y + wall.normal.y * 10), 1)

        maxParticlesSlider.drawSlider(screen)
        gravityXSlider.drawSlider(screen)
        gravityYSlider.drawSlider(screen)
        frictionSlider.drawSlider(screen)
        minSizeSlider.drawSlider(screen)
        thetaSlider.drawSlider(screen)
        velocitySlider.drawSlider(screen)

        if mouseDown:
            pos = pygame.mouse.get_pos()
            maxParticlesSlider.setT(pos[0], pos[1])
            gravityXSlider.setT(pos[0], pos[1])
            gravityYSlider.setT(pos[0], pos[1])
            frictionSlider.setT(pos[0], pos[1])
            minSizeSlider.setT(pos[0], pos[1])
            thetaSlider.setT(pos[0], pos[1])
            velocitySlider.setT(pos[0], pos[1])

        pygame.display.update()
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouseDown = True
            if event.type == pygame.MOUSEBUTTONUP:
                mouseDown = False

        time.sleep(0.0166)
Esempio n. 2
0
class App(tk.Frame):
    UPDATE_DELAY = 15

    BRIGHT_RGB = cv2.COLOR_BGR2RGB
    BRIGHT_HSV = cv2.COLOR_BGR2HSV

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        self.defaultVideoCapture = VideoCapture(0)

        self.webcamCanvas = Canvas(self.parent, self.defaultVideoCapture)
        self.maskCanvas = Canvas(self.parent, self.defaultVideoCapture)

        self.lowHueSlider = Slider(self.parent, "Low Hue", 10, 0, 180)
        self.highHueSlider = Slider(self.parent, "High Hue", 25, 0, 180)
        self.lowSaturationSlider = Slider(self.parent, "Low Saturation", 100,
                                          0, 255)
        self.highSaturationSlider = Slider(self.parent, "High Saturation", 255,
                                           0, 255)
        self.lowValueSlider = Slider(self.parent, "Low Value", 20, 0, 255)
        self.highValueSlider = Slider(self.parent, "High Value", 255, 0, 255)

        self.button = tk.Button(self.parent,
                                text="Get histogram",
                                fg="blue",
                                command=self.drawHSVHHistogram)
        self.button.pack(anchor=tk.CENTER)

        self.updateFrame()

    def updateFrame(self):
        isFrameRead, frame = self.defaultVideoCapture.getFrame(self.BRIGHT_RGB)
        if isFrameRead:
            self.webcamCanvasPhoto = PIL.ImageTk.PhotoImage(
                image=PIL.Image.fromarray(frame))
            self.webcamCanvas.createImage(0, 0, self.webcamCanvasPhoto, tk.NW)
            self.updateMask(frame)
        self.parent.after(self.UPDATE_DELAY, self.updateFrame)

    def drawHSVHHistogram(self):
        isFrameRead, frame = self.defaultVideoCapture.getFrame(self.BRIGHT_RGB)

        pixelColors = frame.reshape(
            (np.shape(frame)[0] * np.shape(frame)[1], 3))
        norm = colors.Normalize(vmin=-1., vmax=1.)
        norm.autoscale(pixelColors)
        pixelColors = norm(pixelColors).tolist()
        hsvFrame = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)
        h, s, v = cv2.split(hsvFrame)
        fig = plt.figure()

        axis = fig.add_subplot(1, 1, 1, projection="3d")
        axis.scatter(h.flatten(),
                     s.flatten(),
                     v.flatten(),
                     facecolors=pixelColors,
                     marker=".")
        axis.set_xlabel("Hue")
        axis.set_ylabel("Saturation")
        axis.set_zlabel("Value")

        plt.show()

    def updateMask(self, frame):
        isMaskFrameRead, maskFrame = self.defaultVideoCapture.getFrame(
            self.BRIGHT_HSV)

        lowHue = self.lowHueSlider.getValue()
        highHue = self.highHueSlider.getValue()
        lowSaturation = self.lowSaturationSlider.getValue()
        highSaturation = self.highSaturationSlider.getValue()
        lowValue = self.lowValueSlider.getValue()
        highValue = self.highValueSlider.getValue()

        lower_hsv = np.array([lowHue, lowSaturation, lowValue])
        higher_hsv = np.array([highHue, highSaturation, highValue])

        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, lower_hsv, higher_hsv)

        frame = cv2.bitwise_and(frame, frame, mask=mask)

        #contour features
        self.drawContours(frame)

        self.maskCanvasPhoto = PIL.ImageTk.PhotoImage(
            image=PIL.Image.fromarray(frame))
        self.maskCanvas.createImage(0, 0, self.maskCanvasPhoto, tk.NW)

    def drawContours(self, frame):
        grayImg = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        retValue, threshImg = cv2.threshold(grayImg, 91, 255,
                                            cv2.THRESH_BINARY)
        contours = cv2.findContours(threshImg, cv2.RETR_TREE,
                                    cv2.CHAIN_APPROX_SIMPLE)[-2]
        for c in contours:
            cv2.drawContours(frame, [c], 0, (255, 0, 0), 2)