The Python program for CS/EG series cameras is not coded for use with OpenCV. This is because it does not depend on any specific library and is a generic code.
In the following, we will use “GrabImage.py” as an example.
Python-OpenCV uses numpy, and image data is treated as a numpy array. In GrabImage.py, the image is acquired by MV_CC_GetOneFrameTimeout () in the work_thread (), and the image data is passed in pData, but this is a pointer in C language, and it cannot be handled by OpenCV as it is. That’s why we need to convert this pData to a numpy array.
1) Add the following two lines of import statements:
- import numpy as np
- import cv2
2) __main__ ret = cam. Before the MV_CC_GetIntValue(“PayloadSize”, stParam) line, add the following line to set the image data format output from the camera to RGB.
- ret = cam.MV_CC_SetEnumValueByString(“PixelFormat”, “RGB8Packed”)
Display in MVS | Corresponding string |
RGB 8 | RGB8Packed |
BGR 8 | BGR8Packed |
3) Inside the work_thread(), insert the code that converts pData to numpy arrays. (I’ll also add two lines, cv2.imshow() and cv2.waitKey(), so you can see 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,3)) cv2.imshow(“show”, image) cv2.waitKey(1) else: print (“no data[0x%x]” % ret) if g_bExit == True: break |