IC Imaging Control (Python)

  • サンプルプログラムトップページ
  • デバイスのオープンとハンドリング
    • デバイスを開く
    • ダイアログボックスを使用してデバイスを開く
    • デバイスプロパティ設定の保存と復元
    • プロパティの設定方法
    • 接続しているカメラ一覧を表示
    • ROIの設定方法
    • オートフォーカスで焦点を調整する範囲を指定
    • デバイスロストの検出と再接続方法
  • イメージの取得
    • 8bit静止画保存
    • 16bit静止画保存
    • Enterキーを押下したときに画像保存
    • メモリーレコーディング
    • AVIファイル保存
  • 画像処理
    • コールバック関数の設定方法(OpenCVで二値化)
    • OpenCVで画像処理して表示する方法
    • ソフトウェアトリガー・外部トリガーの使用方法
    • ステレオカメラで表示
    • 2つのカメラで取得した画像の輝度値を平均してバーに表示する
  • IC Imaging Control3.5(pythonnet編)
    • Qtを使ったデモアプリ(pythonnet編)
    • カメラで取得した画像の輝度値を平均してバーに表示する
      (pythonnet編)
    • 動画保存(MediaStreamSink コーデック:H.264)
    • 露光時間・ゲインを設定し、静止画保存をする(pythonnet編)
  • リンク集

    ICImagingControl WEB APIリファレンスマニュアル/サンプルプログラム

    :日本語対応済み :日本語化準備中
    開発言語 APIリファレンスマニュアル サンプルプログラム
    C 4.0
    ()
    - - 4.0
    ()
    - -
    C++ 4.0
    ()
    3.5
    ()
    3.4
    ()
    4.0
    ()
    3.5 3.4
    ()
    C#/VB.NET 4.0
    ()
    3.5
    ()
    3.4
    ()
    4.0
    ()
    3.5
    ()
    3.4
    ()
    Python 4.0
    ()
    3.5 3.4
    ()
    4.0
    ()
    3.5 3.4
    ()

プロパティの設定方法(露光時間・ゲイン・ホワイトバランスの設定方法)

概要

カメラには露光時間・ゲイン・ホワイトバランスなどの機能があります。
ここでは、それらの機能を簡単に設定する方法について説明します。

サンプルプログラム

サンプル(Python) setting_propertyies_python.zip

サンプル元:05-properties.py
https://github.com/TheImagingSource/IC-Imaging-Control-Samples

サンプルの出力

コード全体

#########
# 解説1
#########
import ctypes 
import tisgrabber as tis
import time

#tisgrabber_x64.dllをインポートする
ic = ctypes.cdll.LoadLibrary("./tisgrabber_x64.dll")
tis.declareFunctions(ic)

#ICImagingControlクラスライブラリを初期化します。
#この関数は、このライブラリの他の関数が呼び出される前に1回だけ呼び出す必要があります。
ic.IC_InitLibrary(0)


#########
# 解説2
#########
#ダイアログ画面を表示
hGrabber = ic.IC_ShowDeviceSelectionDialog(None)


#########
# 解説3
#########
#デバイスが有効か確認
if(ic.IC_IsDevValid(hGrabber)):
    ##### 解説3-1
    ##################
    # 露光時間
    ##################
    #自動露光時間をオフにする(露光時間を固定化するのに必須)
    exposureauto = ctypes.c_long()
    ic.IC_SetPropertySwitch(hGrabber, tis.T("Exposure"), tis.T("Auto"),
                            exposureauto)
    
    print("自動露光時間の設定: {0}".format(exposureauto.value))
    ic.IC_SetPropertySwitch(hGrabber, tis.T("Exposure"), tis.T("Auto"), 0)

    #露光時間を0.0303secに設定する
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("Exposure"), tis.T("Value"),
                                   ctypes.c_float(0.0303))
    expmin = ctypes.c_float()
    expmax = ctypes.c_float()
    exposure = ctypes.c_float()
    #露光時間を0.0303secになっているか確認
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("Exposure"), tis.T("Value"),
                                   exposure)
    ic.IC_GetPropertyAbsoluteValueRange(hGrabber, tis.T("Exposure"), tis.T("Value"),
                                        expmin, expmax)
    print("露光時間 :{0}, 範囲: {1} ~ {2}".format(exposure.value,
                                                       expmin.value, expmax.value))
    
    ##### 解説3-2
    ##################
    # ゲイン
    ##################
    #自動ゲインをオフにする(ゲインを固定化するのに必須)
    gainauto = ctypes.c_long()
    ic.IC_SetPropertySwitch(hGrabber, tis.T("Gain"), tis.T("Auto"),
                            gainauto)
    
    print("自動ゲインの設定: {0}".format(gainauto.value))
    ic.IC_SetPropertySwitch(hGrabber, tis.T("Gain"), tis.T("Auto"), 0)

    #ゲインを10に設定する
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("Gain"), tis.T("Value"),
                                   ctypes.c_float(10.0))
    gainmin = ctypes.c_long()
    gainmax = ctypes.c_long()
    gain = ctypes.c_float()
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("Gain"), tis.T("Value"), gain)
    ic.IC_GetPropertyValueRange(hGrabber, tis.T("Gain"), tis.T("Value"),
                                gainmin, gainmax)
    print("ゲイン {0} 範囲: {1} ~ {2}".format(gain.value, gainmin.value, gainmax.value))

    ##### 解説3-3
    ##################
    # ホワイトバランス
    ##################
     #自動ホワイトバランスをオフにする(ホワイトバランスを固定化するのに必須)
    whiteBalanceauto = ctypes.c_long()
    ic.IC_SetPropertySwitch(hGrabber, tis.T("WhiteBalance"), tis.T("Auto"),
                            whiteBalanceauto)
    
    print("自動ホワイトバランスの設定: {0}".format(whiteBalanceauto.value))
    ic.IC_SetPropertySwitch(hGrabber, tis.T("WhiteBalance"), tis.T("Auto"), 0)

    #ホワイトバランスを10に設定する
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Red"),
                                   ctypes.c_float(2))
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Green"),
                                   ctypes.c_float(1))
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Blue"),
                                   ctypes.c_float(0.1))
    whiteBalanceRed = ctypes.c_float()
    whiteBalanceGreen = ctypes.c_float()
    whiteBalanceBlue = ctypes.c_float()
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Red"),
                                   whiteBalanceRed)
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Green"),
                                   whiteBalanceGreen)
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Blue"),
                                   whiteBalanceBlue)
    print("ホワイトバランス赤: {0} 緑: {1} 青: {2}".format(whiteBalanceRed.value, whiteBalanceGreen.value, whiteBalanceBlue.value))
    
    ##### 解説3-4
    # ソフトウェアトリガー
    ic.IC_PropertyOnePush(hGrabber, tis.T("Trigger"), tis.T("Software Trigger"))

    ##### 解説3-5
    #トリガーのGPIO読み取り
    inn = ctypes.c_long()
    inn_count=0
    while inn_count<3 :
        ic.IC_PropertyOnePush(hGrabber,tis.T("GPIO"),tis.T("Read") )
        ic.IC_GetPropertyValue(hGrabber,tis.T("GPIO"),tis.T("GP In"),inn )
        print("GPIO: %d" %(inn.value) )
        inn_count=inn_count+1
        time.sleep(1)

    ##### 解説3-6
    #フォーカス調整機能(ワンプッシュ)を実行する(オートフォーカスカメラのみ)
    ret = ic.IC_PropertyOnePush(hGrabber, tis.T("Focus"), tis.T("One Push"))
    if ret == -4:
        print("このカメラはフォーカス機能をサポートしていません。")

else:
    print("デバイスを開けませんでした。")

ic.IC_ReleaseGrabber(hGrabber)

解説

解説1:importで宣言する

#########
# 解説1
#########
import ctypes 
import tisgrabber as tis
import time

#tisgrabber_x64.dllをインポートする
ic = ctypes.cdll.LoadLibrary("./tisgrabber_x64.dll")
tis.declareFunctions(ic)

#ICImagingControlクラスライブラリを初期化します。
#この関数は、このライブラリの他の関数が呼び出される前に1回だけ呼び出す必要があります。
ic.IC_InitLibrary(0)

TISのIC Imaging Controlを使用するために、tisgrabber_x64.dllをインポートし、カメラの映像を取得するためのIC_CreateGrabberでグラバーハンドルを作成します。

解説2:ダイアログ画面を表示

#########
# 解説2
#########
#ダイアログ画面を表示
hGrabber = ic.IC_ShowDeviceSelectionDialog(None)

ここではICImagingControlが予め用意しているダイアログボックスを使用する方法を示しています。ダイアログでデバイスを選択し、解像度やフレームレートなどの設定を行います。[Device Settings]ダイアログ画面でOKボタンをクリックするとライブスタートが開始されます。["Click OK to stop"]のメッセージボックスの[OK]ボタンがクリックされるとプログラムが終了します。

ちなみにtisgrabber.pyではopenDeviceメソッドにてデバイス情報のxmlファイルがあれば読み込んで、無ければIC_ShowDeviceSelectionDialogメソッドでデバイスダイアログ表示するようになっているので、IC_ShowDeviceSelectionDialogメソッドの代わりにopenDeviceメソッドを使っていただくことも可能です。

解説3:各種プロパティの設定

#########
# 解説3
#########
#デバイスが有効か確認
if(ic.IC_IsDevValid(hGrabber)):

    ##### 解説3-1
    ##################
    # 露光時間
    ##################
    #自動露光時間をオフにする(露光時間を固定化するのに必須)
    exposureauto = ctypes.c_long()
    ic.IC_SetPropertySwitch(hGrabber, tis.T("Exposure"), tis.T("Auto"),
                            exposureauto)
    
    print("自動露光時間の設定: {0}".format(exposureauto.value))
    ic.IC_SetPropertySwitch(hGrabber, tis.T("Exposure"), tis.T("Auto"), 0)

    #露光時間を0.0303secに設定する
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("Exposure"), tis.T("Value"),
                                   ctypes.c_float(0.0303))
    expmin = ctypes.c_float()
    expmax = ctypes.c_float()
    exposure = ctypes.c_float()
    #露光時間を0.0303secになっているか確認
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("Exposure"), tis.T("Value"),
                                   exposure)
    ic.IC_GetPropertyAbsoluteValueRange(hGrabber, tis.T("Exposure"), tis.T("Value"),
                                        expmin, expmax)
    print("露光時間 :{0}, 範囲: {1} ~ {2}".format(exposure.value,
                                                       expmin.value, expmax.value))
    
    ##### 解説3-2
    ##################
    # ゲイン
    ##################
    #自動ゲインをオフにする(ゲインを固定化するのに必須)
    gainauto = ctypes.c_long()
    ic.IC_SetPropertySwitch(hGrabber, tis.T("Gain"), tis.T("Auto"),
                            gainauto)
    
    print("自動ゲインの設定: {0}".format(gainauto.value))
    ic.IC_SetPropertySwitch(hGrabber, tis.T("Gain"), tis.T("Auto"), 0)

    #ゲインを10に設定する
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("Gain"), tis.T("Value"),
                                   ctypes.c_float(10.0))
    gainmin = ctypes.c_long()
    gainmax = ctypes.c_long()
    gain = ctypes.c_float()
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("Gain"), tis.T("Value"), gain)
    ic.IC_GetPropertyValueRange(hGrabber, tis.T("Gain"), tis.T("Value"),
                                gainmin, gainmax)
    print("ゲイン {0} 範囲: {1} ~ {2}".format(gain.value, gainmin.value, gainmax.value))

    ##### 解説3-3
    ##################
    # ホワイトバランス
    ##################
     #自動ホワイトバランスをオフにする(ホワイトバランスを固定化するのに必須)
    whiteBalanceauto = ctypes.c_long()
    ic.IC_SetPropertySwitch(hGrabber, tis.T("WhiteBalance"), tis.T("Auto"),
                            whiteBalanceauto)
    
    print("自動ホワイトバランスの設定: {0}".format(whiteBalanceauto.value))
    ic.IC_SetPropertySwitch(hGrabber, tis.T("WhiteBalance"), tis.T("Auto"), 0)

    #ホワイトバランスを10に設定する
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Red"),
                                   ctypes.c_float(2))
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Green"),
                                   ctypes.c_float(1))
    ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Blue"),
                                   ctypes.c_float(0.1))
    whiteBalanceRed = ctypes.c_float()
    whiteBalanceGreen = ctypes.c_float()
    whiteBalanceBlue = ctypes.c_float()
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Red"),
                                   whiteBalanceRed)
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Green"),
                                   whiteBalanceGreen)
    ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("WhiteBalance"), tis.T("White Balance Blue"),
                                   whiteBalanceBlue)
    print("ホワイトバランス赤: {0} 緑: {1} 青: {2}".format(whiteBalanceRed.value, whiteBalanceGreen.value, whiteBalanceBlue.value))
    
    ##### 解説3-4
    # ソフトウェアトリガー
    ic.IC_PropertyOnePush(hGrabber, tis.T("Trigger"), tis.T("Software Trigger"))

    ##### 解説3-5
    #トリガーのGPIO読み取り
    inn = ctypes.c_long()
    inn_count=0
    while inn_count<3 :
        ic.IC_PropertyOnePush(hGrabber,tis.T("GPIO"),tis.T("Read") )
        ic.IC_GetPropertyValue(hGrabber,tis.T("GPIO"),tis.T("GP In"),inn )
        print("GPIO: %d" %(inn.value) )
        inn_count=inn_count+1
        time.sleep(1)

    ##### 解説3-6
    #フォーカス調整機能(ワンプッシュ)を実行する(オートフォーカスカメラのみ)
    ret = ic.IC_PropertyOnePush(hGrabber, tis.T("Focus"), tis.T("One Push"))
    if ret == -4:
        print("このカメラはフォーカス機能をサポートしていません。")

else:
    print("デバイスを開けませんでした。")

ic.IC_ReleaseGrabber(hGrabber)

ic.IC_IsDevValid(hGrabber)でデバイスが有効か確認した後に、カメラのプロパティを設定していきます。 このプログラムでは露光時間(解説3-1の箇所)、ゲイン(解説3-2の箇所)、ホワイトバランス(解説3-3の箇所)、ソフトウェアトリガー(解説3-4の箇所)、トリガーのGPIO読み取り(解説3-5の箇所)、フォーカス調整(解説3-6の箇所)のプロパティを設定しています。

設定できるプロパティ値の確認するにはスタートメニューから"VCD Property Inspector"を開き、VCD プロパティがどのように構成されているかをご覧ください。下記の通り、VCD Property Inspectorが起動してください。 VCD プロパティはツリー構造で構成されており、下図の赤枠にある通り、名前と要素(値やスイッチなど)とインターフェースから構成されています。

まず、プロパティを設定するためには【名前】と【要素(値やスイッチなど)】と【インターフェース】の3つを確認します。上図にあるVCD Property Inspectorの赤枠内から3つの項目を判別できます。

名前 ItemIDのアンダーバー以降の文字(上図の例では【Gain】)
要素(値やスイッチなど) ElementIDのアンダーバー以降の文字(上図の例では【Value】)
インターフェース InterfaceIDのアンダーバー以降の文字(上図の例では【AbsoluteValue】)

SetPropertyメソッドの種類

IC_GetPropertySwitch Auto設定などのBoolean値(True/Falseあるいは0/1)を変更する場合に使用
IC_GetPropertyValue ゲインなどの整数値を制御するときに使用
IC_GetPropertyAbsoluteValue 露光時間などの絶対値を制御するときに使用
IC_PropertyOnePush ソフトウェアトリガーやフォーカスのワンプッシュなどを実行するときに使用

まず、InterfaceIDにはSwitch、Value、AbsoluteValue、OnePush(Button)のいずれかが入っていますので、上記の4つの中から使用するメソッドを選びます。上図の例ではインターフェースは【AbsoluteValue】ですので、IC_SetPropertyAbsoluteValueを選択することになります。

第1引数 GrabberHandlePtrのポインタ
第2引数 名前(Item) "VCD Property Inspector"の中にあるItemIDの[VCDID_]を除く部分
(上図の例では【Gain】)
第3引数 要素 "VCD Property Inspector"の中にあるElementIDの[VCDElement_]を除く部分
(上図の例では【Value】)
第4引数 設定する値

次にメソッドの各引数に上記のように指定します。

ゲインの設定例
ic.IC_SetPropertyAbsoluteValue(hGrabber, tis.T("Gain"), tis.T("Value"), ctypes.c_float(10.0))

例えば、ゲインの値を取得するには下記のように設定されます。

GetPropertyメソッドの種類

IC_GetPropertySwitch Auto設定などのBoolean値(True/Falseあるいは0/1)を変更する場合に使用
IC_GetPropertyValue ゲインなどの整数値を制御するときに使用
IC_GetPropertyAbsoluteValue 露光時間などの絶対値を制御するときに使用

プロパティの値を取得するためのメソッドも同様に上記のメソッドが用意されています。

第1引数 GrabberHandlePtrのポインタ
第2引数 "VCD Property Inspector"の中にあるItemIDの[VCDID_]を除く部分
第3引数 "VCD Property Inspector"の中にあるElementIDの[VCDElement_]を除く部分
第4引数 取得した値を格納する変数

次にメソッドの各引数に上記のように指定します。

ゲインの値を取得する例
gain = ctypes.c_float()
ic.IC_GetPropertyAbsoluteValue(hGrabber, tis.T("Gain"), tis.T("Value"), gain)

例えば、ゲインの値を取得するには上記のように設定されます。

▲ このページの先頭に戻る

Copyright © ARGO Corporation. All Rights Reserved.