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

树莓派4运行IDS UI-3200SE相机代码时图像显示异常求助

问题描述

我尝试用树莓派4搭配IDS UI-3200SE相机拍摄照片,代码在PC上运行完全正常,拍摄的照片色彩正常、显示良好,但在树莓派上运行相同代码时,得到的图像显示异常。

我通过以下代码加载相机配置文件:

nRet = ueye.is_ParameterSet(hCam, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0)

配置文件set.ini内容如下:

[Versions]
uEye_api_64.dll=4.95.3770
uEye_usb_64.sys=4.95.1119

[Sensor]
Sensor=UI148xSE-C
Sensor bit depth=0
Sensor source gain=0
FPN correction mode=0
Black reference mode=0
Sensor digital gain=0

[Image size]
Start X=0
Start Y=0
Start X absolute=1
Start Y absolute=1
Width=2560
Height=1920
Binning=0
Subsampling=0

[Scaler]
Mode=0
Factor=0.000000

[Multi AOI]
Enabled=0
Mode=0
x1=0
x2=0
x3=0
x4=0
y1=0
y2=0
y3=0
y4=0

[Shutter]
Mode=1
Linescan number=0

[Log Mode]
Mode=3
Manual value=0
Manual gain=0

[Timing]
Pixelclock=30
Extended pixelclock range=0
Framerate=4.400284
Exposure=78.163208
Long exposure=0
Dual exposure ratio=0

[Selected Converter]
IS_SET_CM_RGB32=2
IS_SET_CM_RGB24=2
IS_SET_CM_RGB16=2
IS_SET_CM_RGB15=2
IS_SET_CM_Y8=2
IS_SET_CM_RGB8=2
IS_SET_CM_BAYER=8
IS_SET_CM_UYVY=2
IS_SET_CM_UYVY_MONO=2
IS_SET_CM_UYVY_BAYER=2
IS_CM_CBYCRY_PACKED=0
IS_SET_CM_RGBY=8
IS_SET_CM_RGB30=8
IS_SET_CM_Y12=8
IS_SET_CM_BAYER12=8
IS_SET_CM_Y16=8
IS_SET_CM_BAYER16=8
IS_CM_BGR12_UNPACKED=8
IS_CM_BGRA12_UNPACKED=8
IS_CM_JPEG=0
IS_CM_SENSOR_RAW10=8
IS_CM_MONO10=8
IS_CM_BGR10_UNPACKED=8
IS_CM_RGBA8_PACKED=2
IS_CM_RGB8_PACKED=2
IS_CM_RGBY8_PACKED=8
IS_CM_RGB10V2_PACKED=8
IS_CM_RGB12_UNPACKED=8
IS_CM_RGBA12_UNPACKED=8
IS_CM_RGB10_UNPACKED=8
IS_CM_RGB8_PLANAR=2

[Parameters]
Colormode=0
Gamma=1.000000
Hardware Gamma=0
Blacklevel Mode=0
Blacklevel Offset=195
Hotpixel Mode=2
Hotpixel Threshold=0
Sensor Hotpixel=0
Adaptive hotpixel correction enable=0
Adaptive hotpixel correction mode=0
Adaptive hotpixel correction sensitivity=3
GlobalShutter=0
AllowRawWithLut=0

[Gain]
Master=9
Red=16
Green=5
Blue=27
GainBoost=0

[Processing]
EdgeEnhancementFactor=1
RopEffect=0
Whitebalance=0
Whitebalance Red=1.000000
Whitebalance Green=1.000000
Whitebalance Blue=1.000000
Color correction=1
Color_correction_factor=1.000000
Color_correction_satU=130
Color_correction_satV=130
Bayer Conversion=1
JpegCompression=0
NoiseMode=0
ImageEffect=0
LscModel=0
WideDynamicRange=0

[Auto features]
Auto Framerate control=0
Brightness exposure control=0
Brightness gain control=0
Auto Framerate Sensor control=0
Brightness exposure Sensor control=0
Brightness gain Sensor control=0
Brightness exposure Sensor control photometry=0
Brightness gain Sensor control photometry=0
Brightness control once=0
Brightness reference=128
Brightness speed=50
Brightness max gain=100
Brightness max exposure=226.884288
Brightness Aoi Left=0
Brightness Aoi Top=0
Brightness Aoi Width=2560
Brightness Aoi Height=1920
Brightness Hysteresis=2
AutoImageControlMode=2
AutoImageControlPeakWhiteChannel=0
AutoImageControlExposureMinimum=0.000000
AutoImageControlPeakWhiteChannelMode=0
AutoImageControlPeakWhiteGranularity=0
Auto WB control=0
Auto WB type=2
Auto WB RGB color model=1
Auto WB RGB color temperature=5932
Auto WB offsetR=0
Auto WB offsetB=0
Auto WB gainMin=0
Auto WB gainMax=100
Auto WB speed=50
Auto WB Aoi Left=0
Auto WB Aoi Top=0
Auto WB Aoi Width=2560
Auto WB Aoi Height=1920
Auto WB Once=0
Auto WB Hysteresis=2
Brightness Skip Frames Trigger Mode=4
Brightness Skip Frames Freerun Mode=4
Auto WB Skip Frames Trigger Mode=4
Auto WB Skip Frames Freerun Mode=4

[Trigger and Flash]
Trigger mode=0
Trigger timeout=200
Trigger delay=0
Trigger debounce mode=0
Trigger debounce delay time=0
Trigger burst size=1
Trigger prescaler frame=1
Trigger prescaler line=1
Trigger input=1
Flash strobe=0
Flash delay=0
Flash duration=0
Flash auto freerun=0
PWM mode=0
PWM frequency=20000000
PWM dutycycle=20000000
GPIO state=0
GPIO direction=0
GPIO1 Config=0
GPIO2 Config=0

[Memory]
Camera memory mode=0

运行的代码如下:

#Libraries
from pyueye import ueye
import numpy as np
import cv2
import sys
from time import sleep
from datetime import date
from datetime import datetime
#import pigpio

#---------------------------------------------------------------------------------------------------------------------------------------

#Variables

#Fotocamera
hCam = ueye.HIDS(0)             #0: first available camera;  1-254: The camera with the specified camera ID
sInfo = ueye.SENSORINFO()
cInfo = ueye.CAMINFO()
pcImageMemory = ueye.c_mem_p()
MemID = ueye.int()
rectAOI = ueye.IS_RECT()
pitch = ueye.INT()
nBitsPerPixel = ueye.INT(24)    #24: bits per pixel for color mode; take 8 bits per pixel for monochrome
channels = 3                    #3: channels for color mode(RGB); take 1 channel for monochrome
m_nColorMode = ueye.INT()       # Y8/RGB16/RGB24/REG32
bytes_per_pixel = int(nBitsPerPixel / 8)
#
pParam = ueye.wchar_p()
pParam.value = "set.ini"

#---------------------------------------------------------------------------------------------------------------------------------------

#Inizializzazione della fotocamera

print("START")
print()

# Starts the driver and establishes the connection to the camera
nRet = ueye.is_InitCamera(hCam, None)
if nRet != ueye.IS_SUCCESS:
    print("is_InitCamera ERROR")

# Reads out the data hard-coded in the non-volatile camera memory and writes it to the data structure that cInfo points to
nRet = ueye.is_GetCameraInfo(hCam, cInfo)
if nRet != ueye.IS_SUCCESS:
    print("is_GetCameraInfo ERROR")

# You can query additional information about the sensor type used in the camera
nRet = ueye.is_GetSensorInfo(hCam, sInfo)
if nRet != ueye.IS_SUCCESS:
    print("is_GetSensorInfo ERROR")

nRet = ueye.is_ResetToDefault( hCam)
if nRet != ueye.IS_SUCCESS:
    print("is_ResetToDefault ERROR")

# Set display mode to DIB
nRet = ueye.is_SetDisplayMode(hCam, ueye.IS_SET_DM_DIB)

#---------------------------------------------------------------------------------------------------------------------------------------

#Definire parametri fotocamera
nRet = ueye.is_ParameterSet(hCam, ueye.IS_PARAMETERSET_CMD_LOAD_FILE, pParam, 0)

ueye.is_GetColorDepth(hCam, nBitsPerPixel, m_nColorMode)
bytes_per_pixel = int(nBitsPerPixel / 8)

#---------------------------------------------------------------------------------------------------------------------------------------

# Can be used to set the size and position of an "area of interest"(AOI) within an image
nRet = ueye.is_AOI(hCam, ueye.IS_AOI_IMAGE_GET_AOI, rectAOI, ueye.sizeof(rectAOI))
if nRet != ueye.IS_SUCCESS:
    print("is_AOI ERROR")

width = rectAOI.s32Width
height = rectAOI.s32Height

# Prints out some information about the camera and the sensor
print("Camera model:		", sInfo.strSensorName.decode('utf-8'))
print("Camera serial no.:	", cInfo.SerNo.decode('utf-8'))
print("Maximum image width:	", width)
print("Maximum image height:	", height)
print()

#---------------------------------------------------------------------------------------------------------------------------------------

# Allocates an image memory for an image having its dimensions defined by width and height and its color depth defined by nBitsPerPixel
nRet = ueye.is_AllocImageMem(hCam, width, height, nBitsPerPixel, pcImageMemory, MemID)
if nRet != ueye.IS_SUCCESS:
    print("is_AllocImageMem ERROR")
else:
    # Makes the specified image memory the active memory
    nRet = ueye.is_SetImageMem(hCam, pcImageMemory, MemID)
    if nRet != ueye.IS_SUCCESS:
        print("is_SetImageMem ERROR")
    else:
        # Set the desired color mode
        nRet = ueye.is_SetColorMode(hCam, m_nColorMode)

#---------------------------------------------------------------------------------------------------------------------------------------
#Attiva il trigger -fotocamera in attesa
nret = ueye.is_SetExternalTrigger(hCam, ueye.IS_SET_TRIGGER_SOFTWARE)
FormatID = ueye.uint(33)
#---------------------------------------------------------------------------------------------------------------------------------------

#La fotocamera e il motore sono pronti

#---------------------------------------------------------------------------------------------------------------------------------------

for i in range (5):

    now = datetime.now()
    dt_string = now.strftime("%Y_%m_%d_%H_%M_%S")
    if nRet != ueye.IS_SUCCESS:
        sleep(0,1)
    else:
        #Cattura un immagine e la salva nelle cartella 
        
        nret = ueye.is_FreezeVideo(hCam, ueye.IS_WAIT)
        print(nret)
        FileParams = ueye.IMAGE_FILE_PARAMS()      
        FileParams.pwchFileName = "Foto/python-test-image"+dt_string+str(i)+".png"            #Definisce il nome del file attenzione all'estensione (+dt_string)
        FileParams.nFileType = ueye.IS_IMG_PNG                                               #Definisce l'estensione dell'immagine
        FileParams.ppcImageMem = None
        FileParams.pnImageID = None
        nret = ueye.is_ImageFile(hCam, ueye.IS_IMAGE_FILE_CMD_SAVE, FileParams, ueye.sizeof(FileParams))
        print(nret)

        sleep (1) #Tempo di attesa tra una foto e l'altra

# Releases an image memory that was allocated using is_AllocImageMem() and removes it from the driver management
ueye.is_FreeImageMem(hCam, pcImageMemory, MemID)

# Disables the hCam camera handle and releases the data structures and memory areas taken up by the uEye camera
ueye.is_ExitCamera(hCam)

print()
print("END")
解决方案

1. 修复配置文件兼容性

当前set.ini记录的是Windows平台的64位驱动版本,树莓派是ARM架构Linux系统,直接加载会导致参数不兼容:

  • 删除配置文件中的[Versions]段,避免驱动版本校验干扰参数加载;
  • 或者在树莓派上用IDS官方工具ueyeconfigtool重新配置相机参数,导出适配ARM平台的配置文件替换原文件。

2. 强制指定颜色模式

异常图像大概率是Bayer转RGB的格式转换失败,在代码中显式指定颜色模式,替代依赖配置文件的逻辑:

# 替换原颜色模式相关代码
m_nColorMode = ueye.IS_CM_RGB24  # 直接指定RGB24格式
nBitsPerPixel = ueye.INT(24)
bytes_per_pixel = int(nBitsPerPixel / 8)
nRet = ueye.is_SetColorMode(hCam, m_nColorMode)

3. 优化USB传输与供电

树莓派USB供电能力有限,高分辨率相机易出现传输不稳定:

  • 给树莓派外接5V/3A以上的独立电源,避免供电不足;
  • 降低像素时钟减少带宽压力,在加载配置后添加:
ueye.is_PixelClock(hCam, ueye.IS_PIXELCLOCK_CMD_SET, ueye.c_int(20), ueye.sizeof(ueye.c_int(20)))

4. 手动处理图像后保存

绕开SDK的格式转换问题,读取相机内存数据到numpy数组,用OpenCV处理后保存:

# 替换原循环内的保存代码
array = ueye.get_data(pcImageMemory, width, height, nBitsPerPixel, pitch, copy=False)
frame = np.reshape(array, (height.value, width.value, bytes_per_pixel))
# 树莓派上需转换颜色通道顺序
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
cv2.imwrite(f"Foto/python-test-image{dt_string}{i}.png", frame)

5. 更新IDS驱动

确保树莓派安装的是IDS官网最新的ARM架构uEye Linux驱动,旧版本驱动易存在兼容性问题。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 13:45:27