File: numpy_warhol.py

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 126,800 kB
  • ctags: 62,729
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 72
file content (48 lines) | stat: -rwxr-xr-x 1,326 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python
import urllib2
import sys
import cv2.cv as cv
import numpy

if __name__ == "__main__":
    cv.NamedWindow("camera", 1)

    capture = cv.CaptureFromCAM(0)

    paste = cv.CreateMat(960, 1280, cv.CV_8UC3)
    topleft = numpy.asarray(cv.GetSubRect(paste, (0, 0, 640, 480)))
    topright = numpy.asarray(cv.GetSubRect(paste, (640, 0, 640, 480)))
    bottomleft = numpy.asarray(cv.GetSubRect(paste, (0, 480, 640, 480)))
    bottomright = numpy.asarray(cv.GetSubRect(paste, (640, 480, 640, 480)))

    while True:
        img = cv.GetMat(cv.QueryFrame(capture))

        n = (numpy.asarray(img)).astype(numpy.uint8)

        red = n[:,:,0]
        grn = n[:,:,1]
        blu = n[:,:,2]

        topleft[:,:,0] = 255 - grn
        topleft[:,:,1] = red
        topleft[:,:,2] = blu

        topright[:,:,0] = blu
        topright[:,:,1] = 255 - red
        topright[:,:,2] = grn

        bottomright[:,:,0] = red
        bottomright[:,:,1] = grn
        bottomright[:,:,2] = 255 - blu

        fgrn = grn.astype(numpy.float32)
        fred = red.astype(numpy.float32)
        bottomleft[:,:,0] = blu
        bottomleft[:,:,1] = (abs(fgrn - fred)).astype(numpy.uint8)
        bottomleft[:,:,2] = red

        cv.ShowImage("camera", paste)
        if cv.WaitKey(6) == 27:
            break
    cv.DestroyAllWindows()