系列文章目录
第一章 国惠光电相机SDK调用——python实现
文章目录
- 系列文章目录
- 前言
- 一、必备python知识
- 二、相机SDK调用实现
- 1.创建设备信息结构体
- 2.导入python模块
- 3.引入相机SDK
- 4.查找连接设备
- 5.获取图像数据
- 6.采用cv2模块显示
- 7.相机参数控制
前言
国惠相机SDK采用c语言开发,以动态库(IRSDK.dll和IRSDK.lib)形式发布,在利用相机SDK进行二次开发时,请先详细了解SDK相关功能。本文主要引入python对国惠相机SDK的调用,实现用python对相机的二次开发应用。本例程内容使用python3.9版本,在window10 64位机上编译实现。
一、必备python知识
进行相关开发之前建议熟悉ctypes模块,推荐阅读
https://www.cnblogs.com/night-ride-depart/p/4907613.html
https://blog.csdn.net/lingyu_me/article/details/106837084
二、相机SDK调用实现
1. 创建设备信息结构体(照搬SDK,api.h中的DeviceInformation结构体)
class DeviceInformation(ctypes.Structure):_fields_ = [("name", ctypes.c_char*16), # 定义一维数组("transport", ctypes.c_char*16),("url", ctypes.c_char * 32),("address", ctypes.c_char * 16),("interfaceName", ctypes.c_char * 16),("serial_num", ctypes.c_char * 16),("pid", ctypes.c_int),("width", ctypes.c_short),("height", ctypes.c_short)]
2.导入python模块
import ctypes
from ctypes import *
import numpy as np
import cv2
3.引入相机SDK
fileName = "../BinX64/IRSDK.dll" # 此处根据dll实际所在位置设置路径
GHOPTO_SDK = ctypes.WinDLL(fileName)
4.查找连接设备
m_deviceCount = c_int(0)#枚举查找设备
GHOPTO_SDK.IR_EnumerateDevices.argtypes = [c_void_p, c_void_p, c_int]
GHOPTO_SDK.IR_EnumerateDevices.restype = c_int
GHOPTO_SDK.IR_EnumerateDevices(None, byref(m_deviceCount), 0x00000020)
#USB设备0x00000020 #千兆网口设备0x00000008#获取设备信息打开设备
m_deviceList = DeviceInformation()
GHOPTO_SDK.IR_EnumerateDevices(pointer(m_deviceList), byref(m_deviceCount), 0x01000000)m_DeviceHandle = None #设备号
GHOPTO_SDK.IR_OpenDevice.argtypes = [c_void_p, c_char_p]
GHOPTO_SDK.IR_OpenDevice.restype = c_void_p
m_DeviceHandle = GHOPTO_SDK.IR_OpenDevice(None, m_deviceList.url)#获取相机幅面大小
iWidth = c_int(0)
iHeight = c_int(0)
GHOPTO_SDK.IR_GetDigitArrayPara.argtypes = [c_void_p, c_void_p, c_void_p]
GHOPTO_SDK.IR_GetDigitArrayPara(m_DeviceHandle, byref(iWidth), byref(iHeight))
5.获取图像数据
GHOPTO_SDK.IR_GetNewArray.argtypes = [c_void_p, ]
GHOPTO_SDK.IR_GetNewArray(m_DeviceHandle) #每次获取新图像都需调用一次该函数len = iWidth.value * iHeight.value
pRawShortData = (c_int16 * len)(*[0])
cast(pRawShortData, POINTER(c_int16))
pBmpData = (c_uint8 * len)(*[0])
cast(pBmpData, POINTER(c_int8))#获取14位原始数据,按两字节存放
GHOPTO_SDK.IR_GetDigitArray.argtypes = [c_void_p, POINTER(c_int16)]
GHOPTO_SDK.IR_GetDigitArray(m_DeviceHandle, pRawShortData) #原始数据转8位数据,方便显示
GHOPTO_SDK.IR_DigitArrayToBmp.argtypes = [c_void_p, POINTER(c_int16), POINTER(c_uint8), c_int, c_void_p]
GHOPTO_SDK.IR_DigitArrayToBmp(m_DeviceHandle, pRawShortData, pBmpData, 0, None)
6.采用cv2模块显示
#一维数据转二维矩阵
np_image = np.array(pBmpData).reshape(iHeight.value, iWidth.value)
np_image = np.flipud(np_image) #对数据进行上下反转,实现正常图像数据#调用cv2显示
cv2.imshow('Camera Video', np_image)
key=cv2.waitKey(50) #50ms刷新一次
7.相机参数控制(以快门校正为例,展示IR_SendSeriaPortCmd函数的应用)
cmd = c_uint8*16
SHUTTER = cmd(0xFF, 0xFF, 0xAA, 0xFF, 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
for i in range(3, len(SHUTTER) - 1):SHUTTER[15] = SHUTTER[15] ^ SHUTTER[i]GHOPTO_SDK.IR_SendSeriaPortCmd.argtypes = [c_void_p, c_void_p, c_int]
GHOPTO_SDK.IR_SendSeriaPortCmd.restype = c_bool
GHOPTO_SDK.IR_SendSeriaPortCmd(m_DeviceHandle, byref(SHUTTER), len(SHUTTER))
总结
以上主要实现相机的连接和实现视频显示,关于SDK的其他功能和应用,暂时未实现,待续。谢谢!