I WANT TO USE OPENCV IN THE PYTHON MR./MS. PROGRAM OF CS/EG SERIES CAMERAS (IN THE CASE OF BAYER)

The Python program for CS/EG series cameras is not coded to rely on OpenCV to be compatible with OpenCV. It is a generic code that does not depend on any specific library.

 

In the following, we will use “GrabImage.py” as an example.

 

Python-OpenCV treats image data as numpy arrays, so you need to adapt to its data format. In GrabImage.py, we use MV_CC_GetOneFrameTimeout() in work_thread() to get the image, and the image data is passed in pData, which is equivalent to a pointer in C.

 

As it is, it cannot be handled by OpenCV, so we need to convert this pData to a numpy array. Follow the steps below to do the conversion.

 

1) Add the following two lines of import statements:

  • import numpy as np
  • import cv2

2) __main__ ret = cam. Before the MV_CC_StartGrabbing () line, add the following line to set the image data format output from the camera.

  • ret = cam.MV_CC_SetEnumValueByString(“PixelFormat”,”BayerRG8“)

The BayerRG8 part varies depending on the camera, so open MVS, connect the camera, and select the appropriate string from Feature Tree → Image Fromat Control → Pixel Format.

 

Display in MVS Corresponding string
 Bayer RG 8

 BayerRG8

 Bayer GR 8

 BayerGR8

 Bayer BG 8

 BayerBG8

 Bayer GB 8

 BayerGB8

 

PythonサンプルプログラムでOpenCVを使いたい件01

 

 

3)Inside work_thread(), add code to convert pData to numpy arrays. (I’ll also add two lines, cv2.imshow() and cv2.waitKey(), so that you can check the image.) 

def work_thread(cam=0, pData=0, nDataSize=0):

    stFrameInfo = MV_FRAME_OUT_INFO_EX()

    memset(byref(stFrameInfo), 0, sizeof(stFrameInfo))

    while True :

        ret = cam.MV_CC_GetOneFrameTimeout(pData, nDataSize, stFrameInfo, 1000)

        if ret == 0:

            print (“get one frame: Width[%d], Height[%d], nFrameNum[%d]”  % (stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nFrameNum))

            image = np.asarray(pData._obj)

            image = image.reshape((stFrameInfo.nHeight, stFrameInfo.nWidth))

            image = cv2.cvtColor(image, cv2.COLOR_BayerBG2BGR)

            cv2.imshow(“show”, image)

            cv2.waitKey(1)

        else:

            print (“no data[0x%x]” % ret)

        if g_bExit == True:

            break

 

cv2. Enter a character string corresponding to the character string you just entered in the COLOR_BayerBG2BGR part.

 

  Corresponding string
 BayerRG8

 cv2.COLOR_BayerBG2BGR

 BayerGR8

 cv2.COLOR_BayerGB2BGR

 BayerBG8

 cv2.COLOR_BayerRG2BGR

 BayerGB8

 cv2.COLOR_BayerGR2BGR

 

The notation of the second argument of cv2.cvtColor() does not match the intuitive Bayer sequence.

This is an OpenCV specification.

 

Specifications can be found below.

 

PythonサンプルプログラムでOpenCVを使いたい件02

 

 

If the color of the video is incorrect, you may have made a mistake in the second argument of cv2.cvtColor().