2012/07/15

python-opencvチュートリアル(3)

python-opencvチュートリアルその3


GitHubにリポジトリを作ってみました
https://github.com/kyatou/python-opencv_tutorial



今日はガウシアンピラミッドの作成とWebカメラからの画像取り込みです。

ガウシアンピラミッド
800x800ピクセルの画像を作成し、半分のサイズ、4分の1のサイズのものを作成します。

'''
python-opencv tutorial
Make gaussian pyramid
Usage:
 05_GaussianPyramid.py
'''

#import opencv library
import cv2
import numpy

imgwidth=800
imgheight=800
img_original= numpy.zeros((imgheight,imgwidth,1),numpy.uint8)

#draw some circle
for i in range(1,6):
 cv2.circle(img_original,(20*2**i,20*2**i),i*10,(255,255,255),2)

#pyrDown(...)
#   pyrDown(src[, dst[, dstsize[, borderType]]]) -> dst

#make half size and quater size
img_half=cv2.pyrDown(img_original)
img_quat=cv2.pyrDown(img_half)

cv2.imshow('original',img_original)
cv2.imshow('Half size',img_half)
cv2.imshow('Quater size',img_quat)

cv2.waitKey()
cv2.destroyAllWindows() 



Webカメラからの画像取り込み

'''
python-opencv tutorial
Get image from web camera
Usage:
 06_webcamera.py
'''

#import opencv library
import cv2


#VideoCapture(...)
#      VideoCapture() -> 
#  or  VideoCapture(filename) -> 
#  or  VideoCapture(device) -> 

#get capture object
cameraid=0
capture=cv2.VideoCapture(cameraid)

isopen=capture.isOpened()
if(False==isopen):
    #retry to open camera
    capture.open(cameraid)
    isopen=capture.isOpened()
    if(False==isopen):
        #could not open camera...
        print 'Could not open camera.' 
 exit()

#get camera attribute
imwidth=capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
imheight=capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)
print 'img width %d height %d ' % (imwidth,imheight)

print 'press ESC or q to quit'

#capture image and display to monitor
while True:
    ret,frame=capture.read()
    if not ret:
        print 'Could not capture frame.'
        exit()

    cv2.imshow('Captured Frame',frame)
    inputkey=cv2.waitKey(100)
    c=chr(inputkey & 255)
    if c in ['q',chr(27)]:
 break

#release capture
if(capture.isOpened()):
    capture.release()

cv2.destroyAllWindows() 






ではまた。

0 件のコメント:

コメントを投稿