撮影条件変更 + SingleImage(GbE-2 / C++)
This content is not available in your language yet.
GbE-2 の解像度(画像サイズ)、撮影速度(フレームレート)、シャッター速度の各種パラメータを変更し、設定変更後の状態から 1 フレームの圧縮画像データを取得・復号して PGM 形式(Portable Graymap)で保存するプログラムの作成手順です。
-
ソースコードの記述
メインファイル(
GbE2_SingleImage_Settings.cppなど)を開き、以下のコードに書き換えます。#include <stdio.h>#include <stdlib.h>#include <PUCLIB.h> // PUCLIB ヘッダーの読み込みint main() {printf("INFINICAM GbE-2 撮影条件変更 & SingleImage キャプチャ開始...\n");// 1. SDKの初期化PUCRESULT ret = PUC_Initialize();if (ret != PUC_SUCCEEDED) {printf("エラー: SDKの初期化に失敗しました。ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 2. ネットワーク上の GbE-2 デバイスを検索PUC_DETECT_FILTER filter = { 0 };filter.nDeviceTypeFlags = PUC_DETECT_TARGET_HAWK; // GbE-2 を指定PUC_DETECT_INFO detectInfo = { 0 };ret = PUC_DetectDevice2(&filter, &detectInfo);if (ret != PUC_SUCCEEDED || detectInfo.nDeviceCount == 0) {printf("エラー: GbE-2 カメラが見つかりませんでした。ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 3. 検出した最初のデバイスをオープンPUC_HANDLE hDevice = NULL;ret = PUC_OpenDevice2(0, &hDevice);if (ret != PUC_SUCCEEDED) {printf("エラー: デバイスのオープンに失敗しました。ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 4. 解像度(幅・高さ)の変更UINT32 newWidth = 800;UINT32 newHeight = 320;ret = PUC_SetResolution(hDevice, newWidth, newHeight);if (ret != PUC_SUCCEEDED) {printf("エラー: 解像度の設定に失敗しました。ErrorCode: %d\n", ret);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}printf("解像度を変更しました: %u x %u ピクセル\n", newWidth, newHeight);// 5. フレームレートおよびシャッター速度の変更UINT32 newFramerate = 1000; // 撮影速度: 1000 fpsUINT32 newShutterSpeed = 2000; // シャッター速度: 1/2000 秒ret = PUC_SetFramerateShutter(hDevice, newFramerate, newShutterSpeed);if (ret != PUC_SUCCEEDED) {printf("エラー: フレームレート/シャッター速度の設定に失敗しました。ErrorCode: %d\n", ret);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}printf("撮影条件を変更しました: %u fps, シャッター 1/%u 秒\n", newFramerate, newShutterSpeed);// 6. 設定変更後の最新解像度・量子化テーブル・転送データサイズを取得UINT32 width = 0, height = 0;PUC_GetResolution(hDevice, &width, &height);unsigned short qVals[PUC_Q_COUNT] = { 0 };PUC_GetQuantization2(hDevice, qVals);UINT32 xferDataSize = 0;PUC_GetXferDataSize(hDevice, &xferDataSize);// 7. メモリバッファの確保// 圧縮データ格納用バッファunsigned char* pCompressedBuffer = (unsigned char*)malloc(xferDataSize);// 復号(デコード)データ格納用バッファ (横幅は4バイト境界にアラインメント)UINT32 lineBytes = (width + 3) & ~3;unsigned long decodedBufferSize = (unsigned long)lineBytes * height;unsigned char* pDecodedBuffer = (unsigned char*)malloc(decodedBufferSize);if (pCompressedBuffer == NULL || pDecodedBuffer == NULL) {printf("エラー: メモリ割り当てに失敗しました。\n");free(pCompressedBuffer);free(pDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}// 8. 変更後の条件で 1 フレームデータをキャプチャPUC_XFER_DATA_INFO xferDataInfo = { 0 };xferDataInfo.pData = pCompressedBuffer;xferDataInfo.nDataSize = xferDataSize;printf("キャプチャ実行中...\n");ret = PUC_GetSingleXferData(hDevice, &xferDataInfo);if (ret == PUC_SUCCEEDED) {// 9. 圧縮データの復号(デコード処理)ret = PUC_DecodeData(pDecodedBuffer, 0, 0, width, height, lineBytes, pCompressedBuffer, qVals);if (ret == PUC_SUCCEEDED) {// 10. 復号した画像を PGM (P5) フォーマットで保存const char* filename = "custom_setting_image.pgm";FILE* fp = NULL;if (fopen_s(&fp, filename, "wb") == 0 && fp != NULL) {// PGM ヘッダーの書き込みfprintf(fp, "P5\n%u %u\n255\n", width, height);// 有効画像データ部分を 1 行ずつ書き込みfor (UINT32 y = 0; y < height; y++) {fwrite(pDecodedBuffer + (y * lineBytes), sizeof(unsigned char), width, fp);}fclose(fp);printf("SUCCESS: 画像ファイルを保存しました (%s)\n", filename);} else {printf("エラー: ファイルの保存に失敗しました。\n");}} else {printf("エラー: 画像データの復号に失敗しました。ErrorCode: %d\n", ret);}} else {printf("エラー: 画像データの取得に失敗しました。ErrorCode: %d\n", ret);}// 11. メモリ解放とデバイスのクローズfree(pCompressedBuffer);free(pDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_SUCCESS;} -
ビルドと実行
Ctrl+Shift+Bを押してビルドを行います。Ctrl+F5で実行します。- プロジェクトフォルダー直下に設定したサイズ(800x320)の画像ファイル
custom_setting_image.pgmが生成されれば成功です。
主な設定 API とパラメータ
Section titled “主な設定 API とパラメータ”解像度の変更 (PUC_SetResolution)
Section titled “解像度の変更 (PUC_SetResolution)”PUC_SetResolution(hDevice, width, height);- 設定可能な幅・高さ: 幅(800 等)、高さ(600, 480, 320, 160, 80 等)。
- 変更時の挙動: 解像度を変更すると、受信するデータサイズ(
PUC_GetXferDataSize)も自動的に変化します。
フレームレートとシャッター速度の変更 (PUC_SetFramerateShutter)
Section titled “フレームレートとシャッター速度の変更 (PUC_SetFramerateShutter)”PUC_SetFramerateShutter(hDevice, framerate, shutterSpeedFps);- framerate: 撮影速度(例:
1000= 1000 fps)。 - shutterSpeedFps: シャッター速度の分母(例:
2000= 1/2000 秒)。 - 設定の制約: シャッター速度は撮影速度(フレームレート)以上の値を指定する必要があります(例: 1000 fps 撮影時、シャッター速度は 1/1000 秒以上)。
