Basler 工业相机与Python开发

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))
            break
    else:
        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_RBG8packed
converter.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*3
    image = converter.Convert(grabResult)
    weld_img = image.GetArray()

更加详细的代码请查阅参考链接3

可参考:
1.Basler ace 相机软件设置、 硬件连接、软件触发,c++语言,程序硬件触发全套详细资料
2.pylon Viewer 使用说明(Windows下)
3.Basler 工业相机 Python开发采集数据、保存照片

  • 10
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值