Created
June 22, 2014 05:59
-
-
Save daxanya2/7254b2b0ecf90232c3a3 to your computer and use it in GitHub Desktop.
OS X に OpenCV を Homebrew で入れて python で動画入出力をする ref: http://qiita.com/daxanya1/items/4709ad8454760e17148c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: UTF-8 | |
import numpy as np | |
import cv2 | |
# 実装の参考 http://www.beechtreetech.com/opencv-exercises-in-python | |
# cv2を使うように変更 | |
updatelock = False # トラックバー処理中のロックフラグ | |
windowname = 'frame' # Windowの名前 | |
trackbarname = 'Position' # トラックバーの名前 | |
# AVIファイルを読む | |
# aviは適当な長さのサンプルをインターネットから拾ってくる | |
# 参考:http://www.engr.colostate.edu/me/facil/dynamics/avis.htm | |
cap = cv2.VideoCapture('drop.avi') | |
# トラックバーを動かしたときに呼び出されるコールバック関数の定義 | |
def onTrackbarSlide(pos): | |
updatelock = True | |
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos) | |
updatelock = False | |
# 名前付きWindowを定義する | |
cv2.namedWindow(windowname, cv2.WINDOW_NORMAL) | |
# AVIファイルのフレーム数を取得する | |
frames = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) | |
# フレーム数が1以上ならトラックバーにセットする | |
if (frames > 0): | |
cv2.createTrackbar(trackbarname, windowname, 0, frames, onTrackbarSlide) | |
# AVIファイルを開いている間は繰り返し(最後のフレームまで読んだら終わる) | |
while(cap.isOpened()): | |
# トラックバー更新中は描画しない | |
if (updatelock): | |
continue | |
# 1フレーム読む | |
ret, frame = cap.read() | |
# 読めなかったら抜ける | |
if ret == False: | |
break | |
# 画面に表示 | |
cv2.imshow(windowname,frame) | |
# 現在のフレーム番号を取得 | |
curpos = int(cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) | |
# トラックバーにセットする(コールバック関数が呼ばれる) | |
cv2.setTrackbarPos(trackbarname, windowname, curpos) | |
# qを押したら抜ける | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# AVIファイルを解放 | |
cap.release() | |
# Windowを閉じる | |
cv2.destroyAllWindows() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: UTF-8 | |
import numpy as np | |
import cv2 | |
import cv2.cv as cv | |
updatelock = False # トラックバー処理中のロックフラグ | |
windowname_in = 'inframe' # Window(元画像)の名前 | |
windowname_out = 'outframe' # Window(変換画像)の名前 | |
trackbarname = 'Position' # トラックバーの名前 | |
# AVIファイルを読む | |
cap = cv2.VideoCapture('drop.avi') | |
# トラックバーを動かしたときに呼び出されるコールバック関数の定義 | |
def onTrackbarSlide(pos): | |
updatelock = True | |
cap.set(cv.CV_CAP_PROP_POS_FRAMES, pos) | |
updatelock = False | |
# Cannyエッジ検出 | |
def doCanny(img): | |
# カラー画像をグレー画像に変換 | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
return cv2.Canny(gray, 100, 200) | |
# 名前付きWindowを定義する | |
cv2.namedWindow(windowname_in, cv2.WINDOW_NORMAL) | |
cv2.namedWindow(windowname_out, cv2.CV_WINDOW_AUTOSIZE) | |
# AVIファイルのフレーム数を取得する | |
frames = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) | |
# フレーム数が1以上ならトラックバーにセットする | |
if (frames > 0): | |
cv2.createTrackbar(trackbarname, windowname_in, 0, frames, onTrackbarSlide) | |
# AVIファイルを開いている間は繰り返し | |
while(cap.isOpened()): | |
# トラックバー更新中は描画しない | |
if (updatelock): | |
continue | |
# 1フレーム読む | |
ret, frame = cap.read() | |
# 読めなかったら抜ける | |
if ret == False: | |
break | |
# 画面に表示 | |
cv2.imshow(windowname_in,frame) | |
cv2.imshow(windowname_out,doCanny(frame)) | |
# 現在のフレーム番号を取得 | |
curpos = int(cap.get(cv.CV_CAP_PROP_POS_FRAMES)) | |
# トラックバーにセットする(コールバック関数が呼ばれる) | |
cv2.setTrackbarPos(trackbarname, windowname_in, curpos) | |
# qを押したら抜ける | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# AVIファイルを解放 | |
cap.release() | |
# Windowを閉じる | |
cv2.destroyAllWindows() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: UTF-8 | |
import numpy as np | |
import cv2 | |
# カメラからのキャプチャ指定 | |
cap = cv2.VideoCapture(0) | |
# コーデックの指定 | |
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') | |
# 保存ファイルとフレームレートとサイズの指定 | |
out = cv2.VideoWriter('output.m4v',fourcc, 30, (640,480)) | |
# カメラオープン中はずっと処理する | |
while(cap.isOpened()): | |
# キャプチャ画像を取り出す | |
ret, frame = cap.read() | |
# データがなければ抜ける | |
if ret==False: | |
break | |
# 画像をファイルに書き込む | |
out.write(frame) | |
# 画像を画面に出力する | |
cv2.imshow('frame',frame) | |
# "q"が押されたら抜ける | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# 後始末 | |
cap.release() | |
out.release() | |
cv2.destroyAllWindows() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ brew tap homebrew/science | |
$ brew install python | |
$ pip install numpy | |
$ brew install cmake automake celt faac fdk-aac git lame libass libtool libvorbis libvpx libvo-aacenc opencore-amr openjpeg opus sdl schroedinger shtool speex texi2html theora wget x264 xvid yasm | |
$ brew install ffmpeg --with-fdk-aac --with-libvo-aacenc --with-libvorbis --with-libvpx --with-openjpeg --with-theora --with-opencore-amr | |
$ brew install eigen | |
$ brew install jasper | |
$ brew install tbb | |
$ brew install qt | |
$ brew install opencv --with-eigen --with-jasper --with-libtiff --with-qt --with-tbb --with-ffmpeg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment