海康⼯业相机:MV-CE100-30GC摄像头的调⽤及图⽚保存-python实现图像采集
主动取流流程
回调出流流程图
⼿册上是通过C语⾔实现的:
海康调⽤C语⾔的动态链接库实现的python库
这些库⽂件在海康威视的MVS软件安装包⾥⾯,⼀就能到。
本次实现了主动取流
3. 创建句柄cam = MvCamera()
4. 获得设备的句柄cam.MV_CC_CreateHandle(device_info)
5. 打开设备cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
6. 获得设备的参数存在MVCC_INTVALUE()这个结构⾥⾯,cam.MV_CC_GetIntValue(“PayloadSize”, camera_int_param)
7. 获取图⽚的存储⼤⼩,还有长度
8. 开始抓流
9. 图⽚信息存在下⾯的结构中
10. 获取流
11. 将图⽚格式转⼀转image_data_size = (c_ubyte * camera_int_param.nCurValue)()image_data_length = camera_int_param.nCurValue
1grab_ret = cam.MV_CC_StartGrabbing()
1frame_info = MV_FRAME_OUT_INFO_EX()memset(byref(frame_info), 0, sizeof(frame_info))
12cam.MV_CC_GetOneFrameTimeout(image_data_size, image_data_length, frame_info, 1000)
1 nRGBSize = frame_info.nWidth * frame_info.nHeight convert_param = MV_CC_PIXEL_CONVERT_PARAM() memset(byref(convert_param), 0, sizeof(convert_param))
1
2
3
class AccessToImages():
def FindDevices(self):
device_list = MV_CC_DEVICE_INFO_LIST()
find_device_ret = MvCamera.MV_CC_EnumDevices(MV_USB_DEVICE, device_list)
if find_device_ret == 0:
return device_list
else:
return False
def GetImages(self, show_device_num, device_list):
i = 0
if show_device_num <= device_list.nDeviceNum:
device_info = cast(device_list.pDeviceInfo[int(show_device_num)], POINTER(MV_CC_DEVICE_INFO)).contents cam = MvCamera()
handle_ret = cam.MV_CC_CreateHandle(device_info)
if handle_ret != 0:
print("创建句柄失败")
# 打开设备
open_device = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
13. 整体代码如下: memset(byref(convert_param), 0, sizeof(convert_param)) convert_param.nWidth = frame_info.nWidth convert_param.nHeight = frame_info.nHeight convert_param.pSrcData = image_data_size convert_param.nSrcDataLen = frame_info.nFrameLen SrcPixelType = PixelType DstPixelType = PixelType convert_param.pDstBuffer = (c_ubyte * nRGBSize)() convert_param.nDstBufferSize = nRGBSize ret = cam.MV_CC_ConvertPixelType(convert_param)
3
4
柏木由纪种子5
67
8
9
10
11
12img_buff = (c_ubyte * convert_param.nDstLen)()py(byref(img_buff), convert_param.pDstBuffer, convert_param.nDstLen)img = np.array(img_buff, dtype=np.int8)Img = shape(1024, 1280)Img = cv.flip(Img, -1)
Img = cv.flip(Img, -1)
1
2
3
4
5
6# _*_coding:UTF-8_*_
# 当前系统⽤户: LISICHENG
# 当前系统⽇期:2019/7/29
# 当前系统时间:13:55
# 创建⽂件的IDE 名称:PyCharm10条感恩句子
import cv2 as cv
import numpy as np
from MvImport.CameraParams_const import *
from MvImport.CameraParams_header import *
from MvImport.MvCameraControl_header import *
from MvImport.MvCameraControl_class import *
from MvImport.MvErrorDefine_const import *
from MvImport.PixelType_const import *
from MvImport.PixelType_header import *
open_device = cam.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
if open_device != 0:
print('开启失败')
# 获取相机int型节点值 "PayloadSize" 为当前节点得名称,⼀帧节点的⼤⼩
camera_int_param = MVCC_INTVALUE()
int_value_ret = cam.MV_CC_GetIntValue("PayloadSize", camera_int_param)
if int_value_ret != 0:
print('获取图像信息失败')
image_data_size = (c_ubyte * camera_int_param.nCurValue)()
image_data_length = camera_int_param.nCurValue
# 开始抓流
grab_ret = cam.MV_CC_StartGrabbing()
if grab_ret != 0:
print('抓起流失败')
# 获取数据⼤⼩
# 下⾯将获得每⼀帧的图像
while True:
frame_info = MV_FRAME_OUT_INFO_EX()
memset(byref(frame_info), 0, sizeof(frame_info))
frame_ret = cam.MV_CC_GetOneFrameTimeout(image_data_size, image_data_length, frame_info, 1000) if frame_ret != 0:
责任的作文print('获取帧信息失败')
nRGBSize = frame_info.nWidth * frame_info.nHeight
convert_param = MV_CC_PIXEL_CONVERT_PARAM()
memset(byref(convert_param), 0, sizeof(convert_param))
convert_param.nWidth = frame_info.nWidth
convert_param.nHeight = frame_info.nHeight
convert_param.pSrcData = image_data_size
convert_param.nSrcDataLen = frame_info.nFrameLen
SrcPixelType = PixelType
DstPixelType = PixelType
convert_param.pDstBuffer = (c_ubyte * nRGBSize)()
convert_param.nDstBufferSize = nRGBSize
ret = cam.MV_CC_ConvertPixelType(convert_param)
if ret != 0:
print("convert pixel fail! ret[0x%x]" % ret)
del image_data_size
img_buff = (c_ubyte * convert_param.nDstLen)()
高考数学知识点总结>教师节祝福语 简短20字py(byref(img_buff), convert_param.pDstBuffer, convert_param.nDstLen)
img = np.array(img_buff, dtype=np.int8)
Img = shape(1024, 1280)
Img = cv.flip(Img, -1)
Img = cv.flip(Img, -1)森海塞尔麦克风
cv.imshow('img' + str(show_device_num), Img)
cv.waitKey(25)
if __name__ == '__main__':
a = AccessToImages()
device_list = a.FindDevices()
if a:
a.GetImages(0, device_list)
# print(device_list.nDeviceNum)
发布评论