-
-
Save Dobiasd/f9c0427857ff0e1de4bd29d064090b7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import numpy | |
import sys | |
import cv2.cv as cv | |
def game_of_life(): | |
"""Capture images with a webcam, binarize them | |
and run Conways game of life on it.""" | |
if len(sys.argv) == 1: | |
capture = cv.CreateCameraCapture(0) | |
elif len(sys.argv) == 2 and sys.argv[1].isdigit(): | |
capture = cv.CreateCameraCapture(int(sys.argv[1])) | |
elif len(sys.argv) == 2: | |
capture = cv.CreateFileCapture(sys.argv[1]) | |
if not capture: | |
sys.exit("Can not initialize capturing...") | |
capture_window_name = "Hit any key to snap or escape to quit."; | |
binary_window_name = "binary"; | |
snap_window_name = "snap"; | |
gol_window_name = "GoL"; | |
cv.NamedWindow(capture_window_name, 1) | |
source_frame = None | |
gol_frame = None | |
binary_frame = None | |
gol_kernel = cv.CreateMat(3, 3, cv.CV_8UC1) | |
for y in range(0, 3): | |
for x in range(0, 3): | |
gol_kernel[y, x] = 1 | |
gol_kernel[1, 1] = 9 | |
gol_lut = cv.CreateMat(256, 1, cv.CV_8UC1) | |
for c in range(0,255): | |
gol_lut[c, 0] = 0 | |
# Classical rules of Convay implemented as filter | |
gol_lut[ 3, 0] = 255 | |
gol_lut[11, 0] = 255 | |
gol_lut[12, 0] = 255 | |
while True: | |
captured_frame = cv.QueryFrame(capture) | |
if captured_frame: | |
destWidth = 320 | |
destHeight = 240 | |
# destWidth = 1920 | |
# destHeight = 1080 | |
frame = cv.CreateMat(destHeight, destWidth, cv.CV_8UC3) | |
cv.Resize(captured_frame, frame, cv.CV_INTER_AREA) | |
cv.ShowImage(capture_window_name, frame) | |
binary_frame = cv.CreateImage(cv.GetSize(frame), cv.IPL_DEPTH_8U, 1) | |
cv.CvtColor(frame, binary_frame, cv.CV_RGB2GRAY) | |
image1 = cv.fromarray(numpy.ones((destHeight, destWidth, 1))) | |
cv.AdaptiveThreshold(binary_frame, binary_frame, 1) | |
cv.Sub(image1, binary_frame, binary_frame) | |
cv.Threshold(binary_frame, binary_frame, 0, 255, cv.CV_THRESH_BINARY) | |
cv.ShowImage(binary_window_name, binary_frame) | |
key = cv.WaitKey(1); | |
if key == 1048603 or key == 27: # Escape? | |
break; | |
elif key != -1: | |
frameNum = 0; | |
cv.SaveImage('GameOfLifeFrame.png', frame) | |
cv.SaveImage('GameOfLifeBinaryFrame.png', binary_frame) | |
source_frame = binary_frame; | |
cv.ShowImage(snap_window_name, source_frame) | |
gol_frame = source_frame | |
if gol_frame: | |
old_gol_frame = gol_frame | |
cv.Threshold(gol_frame, gol_frame, 0, 1, cv.CV_THRESH_BINARY) | |
cv.Filter2D(old_gol_frame, gol_frame, gol_kernel) | |
cv.LUT(gol_frame, gol_frame, gol_lut) | |
# cv.SaveImage('output/'+str(frameNum)+'.png', gol_frame) | |
frameNum+=1 | |
cv.ShowImage(gol_window_name, gol_frame) | |
cv.DestroyWindow(capture_window_name) | |
cv.DestroyWindow(binary_window_name) | |
cv.DestroyWindow(snap_window_name) | |
cv.DestroyWindow(gol_window_name) | |
if __name__ == "__main__": | |
exit(game_of_life()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment