1.Pylon Viewer 设置
为什么要安装Pylon Viewer ?
为了对basler相机进行参数设置,例如曝光时间调节、ip设置
官网
2.基于python的basler相机获取图像
2.1pylon安装并导入
1).下载pypylon(下载地址)
2).在下载好的文件夹中,打开终端并source activate到要安装的虚拟环境里,使用pip install ***。
注:如果 1.7.2版本安装后,不能使用,尝试安装低版本的(例如1.6.0)
导入代码
from pypylon import pylon
2.2 查找相机函数
def search_get_device():tl_factory = pylon.TlFactory.GetInstance()for dev_info in tl_factory.EnumerateDevices():print("DeviceClass:", dev_info.GetDeviceClass())if dev_info.GetDeviceClass() == 'BaslerGigE': # 千兆网(GigE)print(f"ModelName:{dev_info.GetModelName()}\n"f"IP:{dev_info.GetIpAddress()}")camera = pylon.InstantCamera(tl_factory.CreateDevice(dev_info))breakelse:raise EnvironmentError("no GigE device found")return camera
2.3获取相机图像
cam = search_get_device() #查找相机cam.Open() #将相机打开#以最小延迟连续抓取(视频)cam.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)converter = pylon.ImageFormatConverter()#转换为opencv BGR格式converter.OutputPixelFormat = pylon.PixelType_BGR8packed # 如果需要转换成RBG格式,改为PixelType_RBG8packedconverter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned#等待一个图像,然后检索它。超时时间为5000ms。grabResult = cam.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)# 如果图片获取成功if grabResult.GrabSucceeded():#获取图片的高、宽print("SizeX: ", grabResult.Width)print("SizeY: ", grabResult.Height)#将图片转成class 'numpy.ndarray',shape 1944*2592*2(具体的高宽要根据自己对相机参数的设置)img = grabResult.Array#后转换为图像(ndarray)形状1944*2592*3image = converter.Convert(grabResult)weld_img = image.GetArray()
更加详细的代码请查阅参考链接3
可参考:
1.Basler ace 相机软件设置、 硬件连接、软件触发,c++语言,程序硬件触发全套详细资料
2.pylon Viewer 使用说明(Windows下)
3.Basler 工业相机 Python开发采集数据、保存照片