1
1

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 3 years have passed since last update.

カメラ制御(python,OpenCV,VisualStudio) グレー化してファイル保管

Posted at

The Imaging Source社 カメラ 画像処理#

目的、概要##

接続しているカメラを検出し、画像のリアルタイム表示を行う。

開発環境##

プログラミング言語:Python
画像処理モジュール:OpenCV
数値計算モジュール:numpy
開発環境ツール:VisualStudio2017

準備##

tis_OpenCV.py
 カメラ制御のメイン処理(pythonで記述)
tisgrabber.py
 カメラ制御のモジュール集(pythonで記述)

tisgrabber.dll
 カメラ制御のモジュール集(DLL)
Tisgrabber_x64.dll
 カメラ制御のモジュール集(DLL:64bit処理系)
TIS_UDSHL.dll
 カメラ制御のモジュール集(DLL)
TIS_UDSHL_x64.dll
 カメラ制御のモジュール集(DLL:64bit処理系)

image.png

開発手順、実行手順##

1)Visuzlstudio2017の準備

 Visuzlstudio2017で下記手順を実施する。

 1-1)Pythonインタプリターを導入。
 1-2)Opencvを導入。
 1-3)Anacondaを導入(numpyを使用するため)
 1-4)ソースファイル、DLLファイル、などを同じフォルダーに配置

2)新規プロジェクトを作成する。
 2-1)tis_OpenCV.py、tisgrabber.py をVisuzlstudio2017に読み込む
 2-2)Tis_OpenCV.py をスタートアップに指定

3)実行(pre)

 → カメラの映像が認識され、接続したカメラの情報が表示されるので控えておく
 → ctrl+c で終了(画像窓の×ボタンは利かない)
 tis_OpenCV.py L36 を接続カメラの情報に修正

4)実行
 → カメラの映像が表示される、ctrl+c で終了(画像窓の×ボタンは利かない)

ソースコード解説##

Pythonでカメラを制御するサンプルです。
画像処理モジュール(OpenCV)、数値計算モジュール(numpy)、カメラ制御モジュール(Imagingsource)、C言語の型(ctypes)を使います。

L1 ~ L7
Pythonで使うモジュールをインポートします。
インポートする際に別名(as)も付けられますので、長い名前のモジュールには短い別名を付けておきます。

import cv2
import numpy as np
import sys
import os
import datetime
import ctypes as C
import tisgrabber as IC

L9
Cameraというオブジェクト(実態)を作成します。

Camera = IC.TIS_CAM()

L10
カメラを指定して稼働させる。

Camera.open("DFK AFUX178-M12 2810513")

L12
captureというオブジェクトを作成する。

capture=cv.VideoCapture(0)

L14 メイン処理
日付と時刻を表す文字列を含んだファイル名のファイルを作成します。
capture.read() で静止画を採取します。
それを表示し、日付と時刻を含んだファイルに保存します。

cv2.cvtcolor() でグレイに変換して表示し保存します。

try:
    while(True):
        date0=datetime.datetime.now()
        date_str_form=date0.strftime("%Y%m%d_%H%M%S_%f")

        ret,frame=capture.read()
        cv2.imshow("frame_orig",frame)
        cv2.imwrite(date_str_form+".jpg",frame)

        cv2.waitKey(1)
        gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

        cv2.imshow("frame_gray",gray)
        cv2.imwrite(date_str_form+".jpg",gray)

        cv2.waitKey(1000)
except:
    Camera.StopLive()
    cv2.destroyWindow("Window")

以上の繰り返しです。

ソースプログラム##

gray.py
import cv2
import numpy as np
import sys
import os
import datetime
import ctypes as C
import tisgrabber as IC

Camera = IC.TIS_CAM()
Camera.open("DFK AFUX178-M12 2810513")

capture = cv2.VideoCapture(0)

try:
    while(True):
        date0=datetime.datetime.now()
        date_str_form=date0.strftime("%Y%m%d_%H%M%S_%f")

        ret,frame=capture.read()
        cv2.imshow("frame_orig",frame)
        cv2.imwrite(date_str_form+".jpg",frame)

        cv2.waitKey(1)
        gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)

        cv2.imshow("frame_gray",gray)
        cv2.imwrite(date_str_form+".jpg",gray)

        cv2.waitKey(1000)
except:
    Camera.StopLive()
    cv2.destroyWindow("Window")
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?