1. 准备工作
准备一台海康威视工业相机。
下载 MVS 软件及 SDK: 海康机器人 - 机器视觉 - 下载中心
2. Python MVS 示例
MVS 安装路径下包含 Python 示例,但部分未集成 OpenCV。
打开安装目录下的 Samples 文件夹:
D:\soft\MVS\MVS\Development\Samples\Python\BasicDemo
D:\soft\MVS\MVS\Development\Samples\Python\MvImport
复制 BasicDemo.py 到新建项目文件夹中。若缺少依赖,需安装 PyQt5:
pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple
运行后可成功输出画面。
3. Python OpenCV 调用
创建新文件夹并编写脚本,整合 MVS SDK 与 OpenCV。
完整代码如下:
import cv2
import numpy as np
from ctypes import cast, POINTER, c_ubyte, c_uint, c_void_p
from MvCameraControl_class import *
from CameraParams_header import *
# ------------------- opencv 操作部分 --------------------------------------
def opencv_action(img):
# 自定义图像处理逻辑
result_img = img
return result_img
# ----------------------- 海康相机设置部分 ---------------------------------------
ret = MvCamera.MV_CC_Initialize()
if ret != 0:
print(f"初始化 SDK 失败,错误码:{ret}")
exit()
# 枚举设备
deviceList = MV_CC_DEVICE_INFO_LIST()
n_layer_type = MV_GIGE_DEVICE | MV_USB_DEVICE | MV_GENTL_CAMERALINK_DEVICE
ret = MvCamera.MV_CC_EnumDevices(n_layer_type, deviceList)
if ret != 0:
print("枚举设备失败")
exit()
print(f"找到 {deviceList.nDeviceNum} 台设备")
if deviceList.nDeviceNum == :
exit()
stDeviceList = cast(deviceList.pDeviceInfo[], POINTER(MV_CC_DEVICE_INFO)).contents
camera = MvCamera()
ret = camera.MV_CC_CreateHandle(stDeviceList)
ret = camera.MV_CC_OpenDevice()
ret != :
()
exit()
width = c_uint()
height = c_uint()
pixel_format = c_uint()
payload_size = c_uint()
stParam = MVCC_INTVALUE()
ret = camera.MV_CC_GetIntValue(, stParam)
ret != :
()
exit()
payload_size.value = stParam.nCurValue
ret = camera.MV_CC_GetIntValue(, stParam)
ret != :
()
exit()
width.value = stParam.nCurValue
ret = camera.MV_CC_GetIntValue(, stParam)
ret != :
()
exit()
height.value = stParam.nCurValue
(width.value, height.value)
exposure_time =
ret = camera.MV_CC_SetFloatValue(, exposure_time)
ret = camera.MV_CC_StartGrabbing()
ret != :
()
exit()
data_buf = (c_ubyte * payload_size.value)()
data_size = c_uint(payload_size.value)
stFrameInfo = MV_FRAME_OUT_INFO_EX()
cv2.namedWindow(, cv2.WINDOW_NORMAL)
:
:
data_buf = (c_ubyte * payload_size.value)()
ret = camera.MV_CC_GetOneFrameTimeout(
byref(data_buf), payload_size.value, stFrameInfo,
)
ret == :
frame = np.frombuffer(data_buf, dtype=np.uint8)
actual_width = stFrameInfo.nWidth
actual_height = stFrameInfo.nHeight
stFrameInfo.enPixelType == :
expected_size = actual_width * actual_height *
(frame) != expected_size:
frame = frame.reshape((actual_height, actual_width, ))
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
stFrameInfo.enPixelType == :
expected_size = actual_width * actual_height
(frame) != expected_size:
frame = frame.reshape((actual_height, actual_width))
stFrameInfo.enPixelType == :
expected_size = actual_width * actual_height
(frame) != expected_size:
frame = frame.reshape((actual_height, actual_width))
frame = cv2.cvtColor(frame, cv2.COLOR_BayerGB2BGR)
:
()
frame = opencv_action(frame)
cv2.imshow(, frame)
cv2.waitKey() & == ():
:
()
:
camera.MV_CC_StopGrabbing()
camera.MV_CC_CloseDevice()
camera.MV_CC_DestroyHandle()
cv2.destroyAllWindows()


