You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python设置Basler相机参数报AccessException节点不可写异常咨询

问题解答

现有参数设置方式的问题与报错原因

你遇到的_genicam.AccessException: Node is not writable报错由不规范操作和相机硬件特性共同导致:

  • 代码存在冗余调用:已经执行过camera.Open()打开相机后,StartGrabbing()之后多余的camera.Open()属于无效操作,建议删除。
  • 核心触发原因:Basler工业相机启动连续采集后,和帧尺寸、数据格式相关的参数(Height/Width/OffsetX/OffsetY/PixelFormat等)默认会被锁定为不可写——因为采集启动时相机会按照当前参数配置帧缓存、传输带宽,运行中直接修改这类参数会破坏数据流配置,因此直接赋值就会触发权限异常。而Gain/ExposureTime这类成像效果参数本身支持采集过程中实时修改,你代码里这俩参数也报错是因为前面修改Height时就抛出异常中断了执行流程,后续代码根本没有运行到。
  • 额外逻辑bug:代码里i和j两个计数器同步累加,计数到500时j对应的分支会先触发,i==700的分支永远不会被执行。
  • 资源释放逻辑错误:grabResult.Release()写在循环外部,会导致帧缓存持续堆积最终内存泄漏。

StartGrabbing()启动采集后修改参数的正确方法

按照参数类型区分操作方式即可:

  • 实时可调参数:Gain、ExposureTime、BlackLevel、Gamma这类不改变帧尺寸、传输格式的成像效果类参数,采集运行过程中可以直接调用SetValue()或属性赋值修改,只要赋值在参数的Min~Max范围内、符合步长Inc要求就可以正常生效,不需要额外操作。
  • 需停采修改的参数:Height、Width、ROI偏移、像素格式、触发模式这类会改变帧结构、传输配置的参数,必须按照「停采集→改参数→重启采集」的流程操作,不能在采集运行中直接修改。

修正后的可运行代码

import cv2
from pypylon import pylon

camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
converter = pylon.ImageFormatConverter()
# 配置转换输出格式适配OpenCV
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned

camera.Open()
camera.CenterX = False
camera.CenterY = False

# 初始分辨率配置
new_width = camera.Width.GetValue() - camera.Width.GetInc()
if new_width >= camera.Width.GetMin():
    camera.Width.SetValue(new_width)

# 打印参数可调范围
print("Max gain", camera.Gain.Max)
print("Min gain", camera.Gain.Min)
print("Max ExposureTime", camera.ExposureTime.Max)
print("Min ExposureTime", camera.ExposureTime.Min)

i = 0
j = 0
# 使用最新帧抓取策略,避免缓存堆积旧帧
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)

while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
    
    if grabResult.GrabSucceeded():
        i += 1
        j += 1
        if i == 700:
            print(i)
            # 修改尺寸类参数前先停止采集
            camera.StopGrabbing()
            camera.Height.SetValue(200)
            camera.Width.SetValue(300)
            camera.Gain.SetValue(5)
            camera.ExposureTime.SetValue(8000)
            # 参数修改完成后重启采集
            camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
            i = 0
        if j == 500:
            print(j)
            # 修改尺寸类参数前先停止采集
            camera.StopGrabbing()
            camera.Height.SetValue(1088)
            camera.Width.SetValue(2048)
            camera.Gain.SetValue(20)
            camera.ExposureTime.SetValue(8000)
            # 参数修改完成后重启采集
            camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
            j = 0

        print("Camera open status:", camera.IsOpen())
        image = converter.Convert(grabResult)
        img = image.GetArray()
        cv2.imshow("image", img)
        cv2.waitKey(1)
    
    # 每次处理完帧结果立刻释放,避免内存泄漏
    grabResult.Release()

camera.Close()
cv2.destroyAllWindows()

注意:修改Height/Width时要确认赋值符合相机要求的步长、最小值、最大值约束,比如部分Basler相机高度要求是16的整数倍,如果赋值不符合约束即使停采修改也会报错。

内容的提问来源于stack exchange,提问作者REDDY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 16:09:10