0
0

OMRON SENTECカメラ Python Buildup (7) singleconverter_opencv.py

Last updated at Posted at 2024-05-24

こちらの資料を読んでください。

コードの全体説明

このコードは、カメラから取得した画像データをStApiのコンバーターを使用して処理し、その後OpenCVを使用して表示するサンプルです。主な機能は以下の通りです:

  1. StApiの初期化
  2. カメラへの接続
  3. 画像データの取得
  4. StApiコンバーターを使用した画像処理(Y軸反転およびピクセルフォーマットのBGR8への変換)
  5. OpenCVを使った画像のプレビュー

コードの詳細分析

インポートと設定

import cv2
import numpy as np
import stapipy as st

# 取得する画像の枚数
number_of_images_to_grab = 100

# OpenCVを使用して表示する際の画像スケール
DISPLAY_RESIZE_FACTOR = 0.3
  • cv2: OpenCVライブラリをインポートします。
  • numpy: 画像データの処理に使用するため、NumPyライブラリをインポートします。
  • stapipy: StApiライブラリをインポートします。

メイン部分

try:
    # StApiを初期化
    st.initialize()

    # デバイスのスキャンと接続のためのシステムオブジェクトを作成
    st_system = st.create_system()

    # 最初に検出されたデバイスに接続
    st_device = st_system.create_first_device()

    # デバイスの名前を表示
    print('Device=', st_device.info.display_name)

    # 垂直反転用のコンバーターオブジェクトを作成
    st_converter_reverse = st.create_converter(st.EStConverterType.Reverse)
    st_converter_reverse.reverse_y = True

    # ピクセルフォーマットをBGR8に変換するためのコンバーターオブジェクトを作成
    st_converter_pixelformat = st.create_converter(st.EStConverterType.PixelFormat)
    st_converter_pixelformat.destination_pixel_format = st.EStPixelFormatNamingConvention.BGR8

    # 画像ストリームデータを扱うためのデータストリームオブジェクトを作成
    st_datastream = st_device.create_datastream()

    # ホスト側の画像取得を開始
    st_datastream.start_acquisition(number_of_images_to_grab)

    # カメラ側の画像取得を開始
    st_device.acquisition_start()

    # データ取得とステータス確認のためのループ
    while st_datastream.is_grabbing:
        # 'with'を使用してローカル変数st_bufferを作成
        with st_datastream.retrieve_buffer() as st_buffer:
            # 取得したデータに画像データが含まれているか確認
            if st_buffer.info.is_image_present:
                # 画像オブジェクトを作成し、コンバート
                st_image = st_converter_pixelformat.convert(
                    st_converter_reverse.convert(st_buffer.get_image()))

                # 取得した画像データの情報を表示
                print("BlockID={0} Size={1} x {2} First Byte={3}".format(
                      st_buffer.info.frame_id,
                      st_image.width, st_image.height,
                      st_image.get_image_data()[0]))

                # 画像データを取得
                data = st_image.get_image_data()

                # データをNumPy配列に変換
                nparr = np.frombuffer(data, np.uint8)

                # BGR8画像として表示するための処理
                nparr = nparr.reshape(st_image.height, st_image.width, 3)

                # 画像をリサイズして表示
                nparr = cv2.resize(nparr, None,
                                   fx=DISPLAY_RESIZE_FACTOR,
                                   fy=DISPLAY_RESIZE_FACTOR)
                cv2.imshow('image', nparr)
                cv2.waitKey(1)
            else:
                # 取得したデータに画像データが含まれていない場合
                print("Image data does not exist.")

    # カメラ側の画像取得を停止
    st_device.acquisition_stop()

    # ホスト側の画像取得を停止
    st_datastream.stop_acquisition()

except Exception as exception:
    print(exception)

主な処理の流れ

  1. 初期化と接続:

    • st.initialize()でStApiを初期化し、st.create_system()でシステムオブジェクトを作成、st_system.create_first_device()で最初に検出されたデバイスに接続します。
    • デバイスの名前を表示します。
  2. コンバーターの初期化:

    • 垂直反転用のコンバーターを作成し、Y軸反転を有効にします。
    • ピクセルフォーマットをBGR8に変換するためのコンバーターを作成します。
    # 垂直反転用のコンバーターオブジェクトを作成
    st_converter_reverse = st.create_converter(st.EStConverterType.Reverse)
    st_converter_reverse.reverse_y = True
    
    # ピクセルフォーマットをBGR8に変換するためのコンバーターオブジェクトを作成
    st_converter_pixelformat = st.create_converter(st.EStConverterType.PixelFormat)
    st_converter_pixelformat.destination_pixel_format = st.EStPixelFormatNamingConvention.BGR8
    
  3. データストリームの作成と画像取得の開始:

    • st_device.create_datastream()でデータストリームオブジェクトを作成し、st_datastream.start_acquisition()で画像取得を開始します。
  4. 画像データの取得と処理:

    • st_datastream.retrieve_buffer()でバッファを取得し、st_buffer.get_image()で画像データを取得します。
    • コンバーターを使用して、画像データをY軸反転およびBGR8フォーマットに変換します。
    • NumPy配列に変換し、cv2.imshow()で表示します。
    # 画像オブジェクトを作成し、コンバート
    st_image = st_converter_pixelformat.convert(
        st_converter_reverse.convert(st_buffer.get_image()))
    
    # 取得した画像データの情報を表示
    print("BlockID={0} Size={1} x {2} First Byte={3}".format(
          st_buffer.info.frame_id,
          st_image.width, st_image.height,
          st_image.get_image_data()[0]))
    
    # 画像データを取得
    data = st_image.get_image_data()
    
    # データをNumPy配列に変換
    nparr = np.frombuffer(data, np.uint8)
    
    # BGR8画像として表示するための処理
    nparr = nparr.reshape(st_image.height, st_image.width, 3)
    
    # 画像をリサイズして表示
    nparr = cv2.resize(nparr, None,
                       fx=DISPLAY_RESIZE_FACTOR,
                       fy=DISPLAY_RESIZE_FACTOR)
    cv2.imshow('image', nparr)
    cv2.waitKey(1)
    
  5. 画像取得の停止:

    • st_device.acquisition_stop()st_datastream.stop_acquisition()で画像取得を停止します。

まとめ

このコードでは、StApiを使用してカメラから画像データを取得し、StApiコンバーターを使用してY軸反転およびBGR8フォーマットに変換します。その後、OpenCVを使用して画像を表示します。これにより、効率的かつ効果的に画像処理と表示を行うことができます。

関連資料

0
0
0

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
0
0