脳汁portal

アメリカ在住(だった)新米エンジニアがその日学んだIT知識を書き綴るブログ

openCVでraspberryPiのUSBカメラのキャプチャをとって、サイズを変更する

import cv2

# cameraの設定
c = cv2.VideoCapture(0)

# キャプチャ
boolean, img = c.read()
h, w = img.shape[:2]
print h # 1280
print w # 720

# サイズ変更
half = cv2.resize(img, (320, 240))
hh, hw = half.shape[:2]
print hh  # 320
print hw  # 240

cv2.imwrite('half.jpg', half)
  • VideoCaptureでdeviceを指定して、readでキャプチャをとる
  • resize(source, (heightのsize, widthのsize))でサイズ変更
  • imwriteでimageをファイル出力

うまく行かない場合はlsusbとかでちゃんとUSBカメラが認識されているか確認する