From: Alexander Mordvintsev Date: Tue, 7 Jun 2011 13:45:31 +0000 (+0000) Subject: python cv2: X-Git-Tag: accepted/tizen/6.0/unified/20201030.111113~7095 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=927b5c88eac5f0414c8d2f4d8205a7821cdae614;p=platform%2Fupstream%2Fopencv.git python cv2: simple video capture and synth framework stub edge detection sample --- diff --git a/samples/python2/edge.py b/samples/python2/edge.py new file mode 100644 index 0000000..998c0c4 --- /dev/null +++ b/samples/python2/edge.py @@ -0,0 +1,29 @@ +import cv2, cv +import video +import sys + +try: fn = sys.argv[1] +except: fn = 'synth:bg=../cpp/lena.jpg:noise=0.1' + +def nothing(*arg): + pass + +cv2.namedWindow('edge') +cv.CreateTrackbar('thrs1', 'edge', 2000, 5000, nothing) +cv.CreateTrackbar('thrs2', 'edge', 4000, 5000, nothing) + +cap = video.create_capture(fn) +while True: + flag, img = cap.read() + gray = cv2.cvtColor(img, cv.CV_BGR2GRAY) + thrs1 = cv2.getTrackbarPos('thrs1', 'edge') + thrs2 = cv2.getTrackbarPos('thrs2', 'edge') + edge = cv2.canny(gray, thrs1, thrs2, apertureSize=5) + vis = img.copy() + vis /= 2 + vis[edge != 0] = (0, 255, 0) + cv2.imshow('edge', vis) + ch = cv2.waitKey(5) + if ch == 27: + break + diff --git a/samples/python2/video.py b/samples/python2/video.py new file mode 100644 index 0000000..5cbcb91 --- /dev/null +++ b/samples/python2/video.py @@ -0,0 +1,59 @@ +import numpy as np +import cv2 + +class VideoSynth(object): + def __init__(self, size=None, noise=0.0, bg = None, **params): + self.bg = None + self.frame_size = (640, 480) + if bg is not None: + self.bg = cv2.imread(bg, 1) + h, w = self.bg.shape[:2] + self.frame_size = (w, h) + + if size is not None: + w, h = map(int, size.split('x')) + self.frame_size = (w, h) + self.bg = cv2.resize(bg, self.frame_size) + + self.noise = float(noise) + + def read(self, dst=None): + w, h = self.frame_size + + buf = np.zeros((h, w, 3), np.uint8) + if self.bg is not None: + buf[:] = self.bg + if self.noise > 0.0: + noise = np.random.normal(scale = 255*self.noise, size=(h, w, 3)) + buf = cv2.add(buf, noise, dtype=cv2.CV_8UC3) + return True, buf + + +def create_capture(source): + ''' + source: or '' or '' or 'synth:' + ''' + try: source = int(source) + except ValueError: pass + else: + return cv2.VideoCapture(source) + source = str(source).strip() + if source.startswith('synth'): + ss = filter(None, source.split(':')) + params = dict( s.split('=') for s in ss[1:] ) + return VideoSynth(**params) + return cv2.VideoCapture(source) + + +if __name__ == '__main__': + import sys + try: fn = sys.argv[1] + except: fn = 'synth:bg=../cpp/lena.jpg:noise=0.1' + + cap = create_capture(fn) + while True: + ret, img = cap.read() + cv2.imshow('img', img) + ch = cv2.waitKey(1) + if ch == 27: + break