随着python这们语言不断深入发展,那么python调用工业相机也成为了一项流行的需求;
本文了就简单梳理下海康工业相机python的使用
搬砖了一些写的好的文章做参考
海康机器人工业相机sdk简介
python语言调用海康机器人(hikrobotics)工业相机
利用python加opencv与海康工业相机交互
海康工业相机参数设置与获取
-
先安装MVS,下载地址
海康机器人官网
,SDK简介也可以看看上面的引用博客
-
条件:Python+海康官方的mvs文件下的development/samples下的python文件夹
-
注意:相机连接后不要用官方app打开相机,不然python代码检测不到设备,代码在pycharm会提示报错,亲测能跑并能截取到图片(这个就是说,相机不要被其他软件链接占用啦,同一时刻,一个相机只能被一个软件链接)
-
需要添加模块MvImport的路径: 这是整个代码的核心,文件里面内容如下,具体可以下载MVS里面例程里面有:
import sys
sys.path.append("C:\Program Files (x86)\MVS\Development\Samples\Python\MvImport")
我们可以把工业相机sdk调用,分成两个部分,相机控制与图像数据获取
1. 相机控制
参考其SDK提供的流程图我们可以看出,相机控制分成枚举、打开、参数设置、关闭,销毁句柄五个步骤

那么就分别调用对应的python接口就能实现
ret = MvCamera.MV_CC_EnumDevices(tlayerType, deviceList)
if ret != 0:
print ("enum devices fail! ret[0x%x]" % ret)
sys.exit()
ret = cam.MV_CC_CreateHandle(stDeviceList)
if ret != 0:
print ("create handle fail! ret[0x%x]" % ret)
sys.exit()
ret = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
if ret != 0:
print ("open device fail! ret[0x%x]" % ret)
sys.exit()
ret = cam.MV_CC_SetEnumValue("TriggerMode", MV_TRIGGER_MODE_OFF)
if ret != 0:
print ("set trigger mode fail! ret[0x%x]" % ret)
sys.exit()
ret = cam.MV_CC_StopGrabbing()
if ret != 0:
print ("stop grabbing fail! ret[0x%x]" % ret)
sys.exit()
ret = cam.MV_CC_CloseDevice()
if ret != 0:
print ("close deivce fail! ret[0x%x]" % ret)
sys.exit()
ret = cam.MV_CC_DestroyHandle()
if ret != 0:
print ("destroy handle fail! ret[0x%x]" % ret)
sys.exit()
2. 图像数据获取
图像数据获取,有两种方法,一种是参考Grab_Callback.py,使用回调函数获取,另外一种,参考GrabImage使用主动取流接口获取,两种方法有其不同的使用场景;
以其官方提供的示例程序Grab_Callback.py为例子,它的主要目的是在回调函数中,函数接口数据地址pData,pData是一个C*ubyte的数据类型,是没有办法直接用python-opencv进行直接读取,即不能直接转成numpy array的形式
def image_callback(pData, pFrameInfo, pUser):
stFrameInfo = cast(pFrameInfo, POINTER(MV_FRAME_OUT_INFO_EX)).contents
if stFrameInfo:
print ("get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nFrameNum))
CALL_BACK_FUN = FrameInfoCallBack(image_callback)
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))
如何将pdata转opencv的数据格式呢
第一步,获取图像数据。我们通过上面介绍的两种不同的取流函数可以获取到pdata指针,但是pdata是POINTER(c_ubyte))类型的指针,我们要转化成asarray
第二步,图像格式判断与转化。我们要了解到相机输出pdata的是什么图像格式,以及opencv能够处理什么格式海康彩色工业相机图像格式转换方法(Bayer转RGB)
黑白单通道还好,彩色就需要转成BGR三通道数据啦
第三步,opencv显示
def work_thread_1(cam=0, pData=0, nDataSize=0):
stFrameInfo = MV_FRAME_OUT_INFO_EX()
memset(byref(stFrameInfo), 0, sizeof(stFrameInfo))
print("work_thread_1!\n")
img_buff = None
while True:
ret = cam.MV_CC_GetOneFrameTimeout(pData, nDataSize, stFrameInfo, 1000)
if ret == 0:
print ("MV_CC_GetOneFrameTimeout: Width[%d], Height[%d], nFrameNum[%d]" % (stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nFrameNum))
time_start = time.time()
stConvertParam = MV_CC_PIXEL_CONVERT_PARAM()
memset(byref(stConvertParam), 0, sizeof(stConvertParam))
if IsImageColor(stFrameInfo.enPixelType) == 'mono':
print("mono!")
stConvertParam.enDstPixelType = PixelType_Gvsp_Mono8
nConvertSize = stFrameInfo.nWidth * stFrameInfo.nHeight
elif IsImageColor(stFrameInfo.enPixelType) == 'color':
print("color!")
stConvertParam.enDstPixelType = PixelType_Gvsp_BGR8_Packed
nConvertSize = stFrameInfo.nWidth * stFrameInfo.nHeight* 3
else:
print("not support!!!")
if img_buff is None:
img_buff = (c_ubyte * stFrameInfo.nFrameLen)()
stConvertParam.nWidth = stFrameInfo.nWidth
stConvertParam.nHeight = stFrameInfo.nHeight
stConvertParam.pSrcData = cast(pData, POINTER(c_ubyte))
stConvertParam.nSrcDataLen = stFrameInfo.nFrameLen
stConvertParam.enSrcPixelType = stFrameInfo.enPixelType
stConvertParam.pDstBuffer = (c_ubyte * nConvertSize)()
stConvertParam.nDstBufferSize = nConvertSize
ret = cam.MV_CC_ConvertPixelType(stConvertParam)
if ret != 0:
print("convert pixel fail! ret[0x%x]" % ret)
del stConvertParam.pSrcData
sys.exit()
else:
print("convert ok!!")
if IsImageColor(stFrameInfo.enPixelType) == 'mono':
img_buff = (c_ubyte * stConvertParam.nDstLen)()
cdll.msvcrt.memcpy(byref(img_buff), stConvertParam.pDstBuffer, stConvertParam.nDstLen)
img_buff = np.frombuffer(img_buff,count=int(stConvertParam.nDstLen), dtype=np.uint8)
img_buff = img_buff.reshape((stFrameInfo.nHeight, stFrameInfo.nWidth))
print("mono ok!!")
image_show(image=img_buff)
if IsImageColor(stFrameInfo.enPixelType) == 'color':
img_buff = (c_ubyte * stConvertParam.nDstLen)()
cdll.msvcrt.memcpy(byref(img_buff), stConvertParam.pDstBuffer, stConvertParam.nDstLen)
img_buff = np.frombuffer(img_buff, count=int(stConvertParam.nDstBufferSize), dtype=np.uint8)
img_buff = img_buff.reshape(stFrameInfo.nHeight,stFrameInfo.nWidth,3)
print("color ok!!")
image_show(image=img_buff)
time_end = time.time()
print('time cos:', time_end - time_start, 's')
else:
print ("no data[0x%x]" % ret)
if g_bExit == True:
break
def image_show(image):
image = cv2.resize(image, (600, 400), interpolation=cv2.INTER_AREA)
cv2.imshow('test', image)
k = cv2.waitKey(1) & 0xff
def IsImageColor(enType):
dates = {
PixelType_Gvsp_RGB8_Packed: 'color',
PixelType_Gvsp_BGR8_Packed: 'color',
PixelType_Gvsp_YUV422_Packed: 'color',
PixelType_Gvsp_YUV422_YUYV_Packed: 'color',
PixelType_Gvsp_BayerGR8: 'color',
PixelType_Gvsp_BayerRG8: 'color',
PixelType_Gvsp_BayerGB8: 'color',
PixelType_Gvsp_BayerBG8: 'color',
PixelType_Gvsp_BayerGB10: 'color',
PixelType_Gvsp_BayerGB10_Packed: 'color',
PixelType_Gvsp_BayerBG10: 'color',
PixelType_Gvsp_BayerBG10_Packed: 'color',
PixelType_Gvsp_BayerRG10: 'color',
PixelType_Gvsp_BayerRG10_Packed: 'color',
PixelType_Gvsp_BayerGR10: 'color',
PixelType_Gvsp_BayerGR10_Packed: 'color',
PixelType_Gvsp_BayerGB12: 'color',
PixelType_Gvsp_BayerGB12_Packed: 'color',
PixelType_Gvsp_BayerBG12: 'color',
PixelType_Gvsp_BayerBG12_Packed: 'color',
PixelType_Gvsp_BayerRG12: 'color',
PixelType_Gvsp_BayerRG12_Packed: 'color',
PixelType_Gvsp_BayerGR12: 'color',
PixelType_Gvsp_BayerGR12_Packed: 'color',
PixelType_Gvsp_Mono8: 'mono',
PixelType_Gvsp_Mono10: 'mono',
PixelType_Gvsp_Mono10_Packed: 'mono',
PixelType_Gvsp_Mono12: 'mono',
PixelType_Gvsp_Mono12_Packed: 'mono'}
return dates.get(enType, '未知')
效果展示:
彩色:

黑白:

示例程序下载:GrabImage.py
文章目录前言参考链接环境设置相机控制与数据获取数据格式转换常见问题前言随着python这们语言不断深入发展,那么python调用工业相机也成为了一项流行的需求;本文了就简单梳理下海康工业相机python的使用参考链接搬砖了一些写的好的文章做参考海康机器人工业相机sdk简介海康工业相机Python调用实现连续实时拍照海康工业相机参数设置与获取环境设置先安装MVS,下载地址 海康机器人官网,SDK简介也可以看看上面的引用博客条件:Python+海康官方的mvs文件下的developmen
海康工业相机的底层SDK中提供了两种不同方式的图像数据获取方式,一种是回调方式,一种是主动取流方式。但是官方示例中,只提供了相关数据获取到之后的打印信息,对于图像数据的解析并没有给出,基于以上情况,本文对于这两种取流方式获取到的图像数据进行解析。
一、使用前提..
读取
海康工业相机(gige)数据流
调用海康工业相机(gige),读取数据流并转为
opencv(numpy)矩阵格式
参考官方开发案例GrabImage.py,对数据流相机转换
def work_thread(cam=0, pData=0, nDataSize=0):
stFrameInfo = MV_FRAME_OUT_INFO_EX()
memset(byref(stFrameInfo), 0, sizeof(stFrameInfo))
while True:
海康彩色工业相机图像格式转换方法1.彩色相机是如何变成彩色的-Bayer的由来2.工业相机支持的图像格式3.图像格式转化
1.彩色相机是如何变成彩色的-Bayer的由来
提到工业相机图像格式,尤其是彩色相机的图像格式,不得不先讲一下bayer图像格式,网上有很多介绍这种图像格式的文档,随意引用一篇简单介绍下,引用链接: 图像bayer格式介绍以及bayer插值原理.
大致原理呢,就是相机上面的图像传感器只能感受光强而无法感知光的波长,然而光的颜色却是由波长决定的,因此图像传感器是无法记录颜色的。
虽然可以在
环境:树莓派 Ubuntu系统
编程环境:Python3.7 Node(忘了版本了,都可以,最好稳定版本)
需要安装的模块:Python端:cv2 websockets fastapi等;Node端:主要是ws(用来传输视频流)
安装可以看网上的文章,很多有写,不过树莓派这里避坑:树莓派进入是pi用户,需要才能切换为root用户,安装Python模块时如果在用户下安装,用用户运行是是没有的,会报错误。另外,安装模块时似乎不能用安装,需要用它自带的安装,不过不知道是不是只有我这里才是这样的。......
文章转载:python语言下使用opencv接口cv2.VideoCapture()接口调用海康机器人工业相机
一、Opencv接口
使用 opencv 的官方接口 VideoCapture() 接口直接调用海康工业相机,
接下来简单介绍下 VideoCapture() 这个接口
接口:cv2.VideoCapture()
作用:获取本地目录下的视频文件或者使用本电脑的摄像头获取实时数据;
本文需要使用该接口调用海康工业相机;
参数写入 “0” ,调用的是本地电脑摄像头
参数写入 “1” ,调用的
三、主动
取流( getimagebuffer)对黑白图像数据解析并用
opencv 显示
1.主动
取流( getimagebuffer)数据获取并解析 mono 格式的图像数据
2.图像数据显示
四、主动取...