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.
The following is an example of “GrabImage.py”.
Python-OpenCV uses numpy, and the image data is treated as a numpy array. In GrabImage.py, we use MV_CC_GetOneFrameTimeout() within work_thread() to get the image, but the image data is passed in pData.
However, this is a pointer in C and cannot be used in OpenCV as 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_StartGrabbing () line, add the following line to set the image data format output from the camera to monochrome.
ret = cam. MV_CC_SetEnumValueByString(“PixelFormat”,” Mono8″)
3) Inside the work_thread(), add code to convert pData to numpy array. (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)) cv2.imshow(“show”, image) cv2.waitKey(1) else: print (“no data[0x%x]” % ret) if g_bExit == True: break |