work on camshift.py
authorAlexander Mordvintsev <no@email>
Sat, 13 Aug 2011 08:54:11 +0000 (08:54 +0000)
committerAlexander Mordvintsev <no@email>
Sat, 13 Aug 2011 08:54:11 +0000 (08:54 +0000)
samples/python2/camshift.py [new file with mode: 0644]

diff --git a/samples/python2/camshift.py b/samples/python2/camshift.py
new file mode 100644 (file)
index 0000000..669b06a
--- /dev/null
@@ -0,0 +1,83 @@
+import numpy as np\r
+import cv2\r
+import video\r
+\r
+\r
+\r
+class App(object):\r
+    def __init__(self, video_src):\r
+        self.cam = video.create_capture(video_src)\r
+        ret, self.frame = self.cam.read()\r
+        cv2.namedWindow('camshift')\r
+        cv2.setMouseCallback('camshift', self.onmouse)\r
+\r
+        self.selection = None\r
+        self.drag_start = None\r
+        self.tracking_state = 0\r
+\r
+    def onmouse(self, event, x, y, flags, param):\r
+        x, y = np.int16([x, y]) # BUG\r
+        if event == cv2.EVENT_LBUTTONDOWN:\r
+            self.drag_start = (x, y)\r
+            self.tracking_state = 0\r
+        if self.drag_start: \r
+            if flags & cv2.EVENT_FLAG_LBUTTON:\r
+                h, w = self.frame.shape[:2]\r
+                xo, yo = self.drag_start\r
+                x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y]))\r
+                x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y]))\r
+                self.selection = None\r
+                if x1-x0 > 0 and y1-y0 > 0:\r
+                    self.selection = (x0, y0, x1, y1)\r
+            else:\r
+                self.drag_start = None\r
+                if self.selection is not None:\r
+                    self.tracking_state = 1\r
+\r
+    def show_hist(self):\r
+        bin_count = self.hist.shape[0]\r
+        bin_w = 24\r
+        img = np.zeros((256, bin_count*bin_w, 3), np.uint8)\r
+        for i in xrange(bin_count):\r
+            h = int(self.hist[i])\r
+            cv2.rectangle(img, (i*bin_w+2, 255), ((i+1)*bin_w-2, 255-h), (int(180.0*i/bin_count), 255, 255), -1)\r
+        img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR)\r
+        cv2.imshow('hist', img)\r
+\r
+    def run(self):\r
+        while True:\r
+            ret, self.frame = self.cam.read()\r
+            self.frame = self.frame.copy()\r
+            \r
+            hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV)\r
+\r
+            if self.selection:\r
+                x0, y0, x1, y1 = self.selection\r
+                hsv_roi = hsv[y0:y1, x0:x1]\r
+                hist = cv2.calcHist( [hsv_roi], [0], None, [16], [0, 180] )\r
+                cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX);\r
+                self.hist = hist.reshape(-1)\r
+                self.show_hist()\r
+                \r
+                roi = self.frame[y0:y1, x0:x1]\r
+                cv2.bitwise_not(roi, roi)\r
+\r
+            if self.tracking_state == 1:\r
+                self.selection = None\r
+                prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1)\r
+                term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )\r
+                print cv2.CamShift(prob, term_crit)\r
+                #cv2.imshow('back', back)\r
+                \r
+                \r
+            cv2.imshow('camshift', self.frame)\r
+            if cv2.waitKey(5) == 27:\r
+                break\r
+\r
+\r
+if __name__ == '__main__':\r
+    import sys\r
+    try: video_src = sys.argv[1]\r
+    except: video_src = video.presets['chess']\r
+    App(video_src).run()\r
+\r