CannyCam
Verfasst: Montag 25. Mai 2015, 22:59
Habe mich nach langer Zeit wieder mit python beschäftigt und den Canny-Algorithmus entdeckt. Mit diesem Script lasse ich Bilder enstehen, die abgepaust werden können. Blatt auf Bildschirm mit fester Unterlage legen und mit den Richtungstasten die Auflösung verändern. Ist man mit dem Bild zufrieden, dann kann das Abzeichen beginnen.
Gruß Frank
Code: Alles auswählen
#! /usr/bin/env python
# -*- coding: utf-8
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
from PIL import Image, ImageTk, ImageChops
from functools import partial
import cv
UPDATE_TIME = 100
MODUS = "raw"
BGR = "P"
RGB = "P"
CAM = -1
TITLE ="CANNY CAM"
FONT = "Arial 10 bold"
BG = "white"
NO_CAM_TEXT = "NO CAM FOUND"
NO_CAM_FONT = "Arial 26"
RIGHT_PRESS = "<Right>"
LEFT_PRESS = "<Left>"
RIGHT_RELEASE = "<KeyRelease-Right>"
LEFT_RELEASE = "<KeyRelease-Left>"
UP_PRESS = "<Up>"
DOWN_PRESS = "<Down>"
UP_RELEASE = "<KeyRelease-Up>"
DOWN_RELEASE = "<KeyRelease-Down>"
CANNY_STEP_X = 1
CANNY_STEP_Y = 1
RANGE = 300
START_X = 150
START_Y = 30
class Cam(tk.Label):
def __init__(self, root, cam=CAM, text=TITLE, update_time=UPDATE_TIME, bg=BG):
tk.Label. __init__(self, root, bg=bg)
self.window = tk.Label(self)
self.window.pack()
self.root = root
self.canny_x = START_X
self.canny_y = START_Y
self.update_time = update_time
self.camera = cv.CaptureFromCAM(cam)
self.camera_width = int(cv.GetCaptureProperty(self.camera,
cv.CV_CAP_PROP_FRAME_WIDTH))
self.camera_height = int(cv.GetCaptureProperty(self.camera,
cv.CV_CAP_PROP_FRAME_HEIGHT))
for button, step, function in ((RIGHT_PRESS, CANNY_STEP_X, self.shift_x),
(LEFT_PRESS, -CANNY_STEP_X,self.shift_x),
(RIGHT_RELEASE, 0, self.shift_x),
(LEFT_RELEASE, 0, self.shift_x),
(UP_PRESS, CANNY_STEP_Y, self.shift_y),
(DOWN_PRESS, -CANNY_STEP_Y, self.shift_y),
(UP_RELEASE, 0, self.shift_y),
(DOWN_RELEASE, 0, self.shift_y)):
root.bind(button, partial(function, step))
def shift_x(self, x, event):
if (self.canny_x > - 0 and x < 0 or self.canny_x < RANGE and x > 0):
self.canny_x += x
def shift_y(self, y, event):
if (self.canny_y > - 0 and y < 0 or self.canny_y < RANGE and y > 0):
self.canny_y += y
def run(self):
self.frame = cv.QueryFrame(self.camera)
if self.frame == None:
self.window.config(text=NO_CAM_TEXT, font=NO_CAM_FONT,
width=33, height=10)
else:
frame_grau = cv.CreateImage((self.frame.width, self.frame.height), cv.IPL_DEPTH_8U, 1)
cv.CvtColor(self.frame, frame_grau, cv.CV_BGR2GRAY)
frame_canny = cv.CreateImage((self.frame.width, self.frame.height), cv.IPL_DEPTH_8U, 1)
cv.Canny(frame_grau, frame_canny, self.canny_x, self.canny_y)
self.image = Image.fromstring(RGB, (self.camera_width,
self.camera_height), frame_canny.tostring(), MODUS, BGR)
self.image = ImageChops.invert(self.image)
self.tk_image = ImageTk.PhotoImage(self.resize_image(self.image))
self.window.config(image = self.tk_image)
self.after(self.update_time, self.run)
def resize_image(self, img):
width, height = self.root.winfo_screenwidth(), root.winfo_screenheight()
img_width, img_height = img.size
if height >= width:
div_faktor = (float(str(int(img_width)) + ".0")
/ float(str(width) + ".0"))
img_width = img_width / div_faktor
img_height = img_height / div_faktor
else:
div_faktor = (float(str(int(img_height)) + ".0")
/ float(str(height) + ".0"))
img_width = img_width / div_faktor
img_height = img_height / div_faktor
return img.resize((int(img_width), int(img_height)))
if __name__ == '__main__':
root = tk.Tk()
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
cam = Cam(root, CAM)
root.title(TITLE)
cam.pack(expand=True, fill="both")
cam.run()
root.mainloop()