5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PointGreyカメラをPython(PyCapture2)で使う

Last updated at Posted at 2017-05-29

FlyCapture SDKのPythonラッパーを公式がリリースしてくれていたので、使用してみました。
ソフトウェアは以下からダウンロードできます。(ユーザ登録が必要)
https://www.ptgrey.com/support/downloads

テスト環境

環境・ソフトウェア

  • Windows10 (64bit)
  • Python 3.5
  • OpenCV 3.2
  • FlyCapture 2.11,PyCapture 2.11.121

カメラ

  • Grasshopper3 GS3-U3-51S5M-C

カメラから画像取得

取得した画像はOpenCVで処理したいのでPyCaptureで取り込んだ後~~Numpy配列に変換します。~~保存して開き直します!この方が速い!!!

capture.py
# -*- coding: utf-8 -*-
import os
import cv2
import numpy as np
import PyCapture2

# set Camera
bus = PyCapture2.BusManager()
cam = PyCapture2.Camera()
uid = bus.getCameraFromIndex(0)
cam.connect(uid)

def capIm():
  try:
    img = cam.retrieveBuffer()
  except PyCapture2.Fc2error as fc2Err:
    print("Error retrieving buffer :", fc2Err)
    return False, []

  # Numpy配列に変換
  # data = np.array(img.getData(), dtype=np.uint8)
  # data = data.reshape((img.getRows(), img.getCols()))
  
  # ビットマップで保存してOpenCVで開く
  img.save('tmp.bmp'.encode("utf-8"), PyCapture2.IMAGE_FILE_FORMAT.BMP)
  data = cv2.imread('tmp.bmp', 0)
  return True, data


# 取得画像サイズの指定.
width, height = 2048, 2048
fmt7info, supported = cam.getFormat7Info(0)
offsetX = int((fmt7info.maxWidth-width)/2)
offsetY = int((fmt7info.maxHeight-height)/2)
pxfmt = PyCapture2.PIXEL_FORMAT.RAW8
fmt7imgSet = PyCapture2.Format7ImageSettings(0, offsetX, offsetY, width, height, pxfmt)
fmt7pktInf, isValid = cam.validateFormat7Settings(fmt7imgSet)
cam.setFormat7ConfigurationPacket(fmt7pktInf.recommendedBytesPerPacket, fmt7imgSet)

# カメラプロパティの設定
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.AUTO_EXPOSURE, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHARPNESS, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHUTTER, autoManualMode = False)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAIN, autoManualMode = False)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.FRAME_RATE, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.AUTO_EXPOSURE, onOff = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.FRAME_RATE, onOff = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAMMA, onOff = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHARPNESS, onOff = False)

SHUTTER, GAIN = 500, 100
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHUTTER, absValue = SHUTTER/40)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAIN, absValue = GAIN/20)

cam.startCapture()



def nothing(x):
  pass

cv2.namedWindow('PyCapImg', cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('PyCapImg', 500,550)
cv2.createTrackbar('SHUTTER', 'PyCapImg', SHUTTER, 1000, nothing)
cv2.createTrackbar('GAIN', 'PyCapImg', GAIN, 1000, nothing)


while cv2.waitKey(1)&0xFF != 27:
  ret, im = capIm()
  if not ret:
    break

  ret = cv2.getTrackbarPos('SHUTTER', 'PyCapImg')
  if ret/40 != SHUTTER:
    SHUTTER = ret
    cam.stopCapture()
    cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHUTTER, absValue = SHUTTER/40)
    cam.startCapture()

  ret = cv2.getTrackbarPos('GAIN', 'PyCapImg')
  if ret/20 != GAIN:
    GAIN = ret
    cam.stopCapture()
    cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAIN, absValue = GAIN/20)
    cam.startCapture()

  sh = cam.getProperty(PyCapture2.PROPERTY_TYPE.SHUTTER)
  ga = cam.getProperty(PyCapture2.PROPERTY_TYPE.GAIN)
  cv2.displayOverlay('PyCapImg', 'SHUTTER:{:.2f}, GAIN:{:.2f}'.format(sh.absValue, ga.absValue))

  cv2.imshow('PyCapImg', im)



# 設定をもとに戻しておく
cam.stopCapture()
fmt7imgSet = PyCapture2.Format7ImageSettings(0, 0, 0, fmt7info.maxWidth, fmt7info.maxHeight, pxfmt)
fmt7pktInf, isValid = cam.validateFormat7Settings(fmt7imgSet)
cam.setFormat7ConfigurationPacket(fmt7pktInf.recommendedBytesPerPacket, fmt7imgSet)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHUTTER, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.GAIN, autoManualMode = True)
cam.setProperty(type = PyCapture2.PROPERTY_TYPE.SHARPNESS, onOff = True)
cam.disconnect()

cv2.destroyAllWindows()

try:
  os.remove('tmp.bmp')
except:
  pass

これでcam.read()と同様にPointGreyカメラが使えます。
im.png

本当は、シャッタースピードなどの指定もしたかったのですが、、、
(追記)
SHUTTER,GAINなどのプロパティを変更するには一度カメラキャプチャを止める必要があるようなので,トラックバーで変更されたときのみ処理を行うようにしています。
設定可能なカメラプロパティはFlyCaptureについてる標準のビューワーで確認するとわかると思います。

img.png

5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?