六、其他
1、相机
(1)选择视频模式
左右视频帧同步,以并排格式作为单个未压缩视频帧流式传输。
在ZED Explorer或者使用API可以改变视频的分辨率和帧率。
(2)选择输出视图
ZED能以不同的格式输出图像,包括校正、未校正和灰度图像(左视图、右视图、并排视图、左未校正或右未校正、左灰度或右灰度)。
(3)调整相机设置
ZED相机具有板载ISP(图像信号处理器),可以对双图像传感器捕获的原始图像执行各种图像处理算法。ISP的参数可以通过ZED Explorer或者ZED SDK进行调整。
相机调整参数左右是同步的,不能单独调整。
当相机的白平衡、曝光和增益处于“自动”模式时,它们会根据场景中的亮度自动调整。
注意:在低光环境中增加Gamma设置。
使用API
(1)相机配置
创建一个camera对象并指定InitParameters。
初始参数可以调整分辨率、FPS、深度感知参数等等。这些参数只能在打开相机前设置,不能在相机的使用过程中更改。
# Create a ZED camera object
zed = sl.Camera()
# Set configuration parameters
init_params = sl.InitParameters()
init_params.camera_resolution = sl.RESOLUTION.HD1080
init_params.camera_fps = 30
# Open the camera
err = zed.open(init_params)
if err != sl.ERROR_CODE.SUCCESS:
exit(-1)
InitParameters包含默认配置。
(2)捕获图像
指定RuntimeParameters,调用grab()捕获新帧,调用retrieveImage()检索捕获的帧。
retrieveImage()可以在不同的视图之间进行选择,如左、右、未校正和灰度图像。
image = sl.Mat()
if zed.grab() == SUCCESS:
# A new image is available if grab() returns SUCCESS
zed.retrieve_image(image, sl.VIEW.LEFT) # Retrieve the left image
(3)调整相机控制
曝光、白平衡等相机设置可以在运行时使用setcamersettings()手动设置。
要改变相机分辨率和帧率,使用InitParameters()。
# Set exposure to 50% of camera framerate
zed.set_camera_settings(sl.VIDEO_SETTINGS.EXPOSURE, 50)
# Set white balance to 4600K
zed.set_camera_settings(sl.VIDEO_SETTINGS.WHITE_BALANCE, 4600)
# Reset to auto exposure
zed.set_camera_settings(sl.VIDEO_SETTINGS.EXPOSURE, -1)
完整程序
"""
Live camera sample showing the camera information and video in real time and allows to control the different
settings.
"""
import cv2
import pyzed.sl as sl
camera_settings = sl.VIDEO_SETTINGS.BRIGHTNESS
str_camera_settings = "BRIGHTNESS"
step_camera_settings = 1
def main():
print("Running...")
init = sl.InitParameters()
cam = sl.Camera()
if not cam.is_opened():
print("Opening ZED Camera...")
status = cam.open(init)
if status != sl.ERROR_CODE.SUCCESS:
print(repr(status))
exit()
runtime = sl.RuntimeParameters()
mat = sl.Mat()
print_camera_information(cam)
print_help()
key = ''
while key != 113: # for 'q' key
err = cam.grab(runtime)
if err == sl.ERROR_CODE.SUCCESS:
cam.retrieve_image(mat, sl.VIEW.LEFT)
cv2.imshow("ZED", mat.get_data())
key = cv2.waitKey(5)
settings(key, cam, runtime, mat)
else:
key = cv2.waitKey(5)
cv2.destroyAllWindows()
cam.close()
print("\nFINISH")
def print_camera_information(cam):
print("Resolution: {0}, {1}.".format(round(cam.get_camera_information().camera_configuration.resolution.width, 2), cam.get_camera_information().camera_configuration.resolution.height))
print("Camera FPS: {0}.".format(cam.get_camera_information().camera_configuration.fps))
print("Firmware: {0}.".format(cam.get_camera_information().camera_configuration.firmware_version))
print("Serial number: {0}.\n".format(cam.get_camera_information().serial_number))
def print_help():
print("Help for camera setting controls")
print(" Increase camera settings value: +")
print(" Decrease camera settings value: -")
print(" Switch camera settings: s")
print(" Reset all parameters: r")
print(" Record a video: z")
print(" Quit: q\n")
def settings(key, cam, runtime, mat):
if key == 115: # for 's' key
switch_camera_settings()
elif key == 43: # for '+' key
current_value = cam.get_camera_settings(camera_settings)[1]
cam.set_camera_settings(camera_settings, current_value + step_camera_settings)
print(str_camera_settings + ": " + str(current_value + step_camera_settings))
elif key == 45: # for '-' key
current_value = cam.get_camera_settings(camera_settings)[1]
if current_value >= 1:
cam.set_camera_settings(camera_settings, current_value - step_camera_settings)
print(str_camera_settings + ": " + str(current_value - step_camera_settings))
elif key == 114: # for 'r' key
cam.set_camera_settings(sl.VIDEO_SETTINGS.BRIGHTNESS, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.CONTRAST, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.HUE, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.SATURATION, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.SHARPNESS, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.GAIN, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.EXPOSURE, -1)
cam.set_camera_settings(sl.VIDEO_SETTINGS.WHITEBALANCE_TEMPERATURE, -1)
print("Camera settings: reset")
elif key == 122: # for 'z' key
record(cam, runtime, mat)
def switch_camera_settings():
global camera_settings
global str_camera_settings
if camera_settings == sl.VIDEO_SETTINGS.BRIGHTNESS:
camera_settings = sl.VIDEO_SETTINGS.CONTRAST
str_camera_settings = "Contrast"
print("Camera settings: CONTRAST")
elif camera_settings == sl.VIDEO_SETTINGS.CONTRAST:
camera_settings = sl.VIDEO_SETTINGS.HUE
str_camera_settings = "Hue"
print("Camera settings: HUE")
elif camera_settings == sl.VIDEO_SETTINGS.HUE:
camera_settings = sl.VIDEO_SETTINGS.SATURATION
str_camera_settings = "Saturation"
print("Camera settings: SATURATION")
elif camera_settings == sl.VIDEO_SETTINGS.SATURATION:
camera_settings = sl.VIDEO_SETTINGS.SHARPNESS
str_camera_settings = "Sharpness"
print("Camera settings: Sharpness")
elif camera_settings == sl.VIDEO_SETTINGS.SHARPNESS:
camera_settings = sl.VIDEO_SETTINGS.GAIN
str_camera_settings = "Gain"
print("Camera settings: GAIN")
elif camera_settings == sl.VIDEO_SETTINGS.GAIN:
camera_settings = sl.VIDEO_SETTINGS.EXPOSURE
str_camera_settings = "Exposure"
print("Camera settings: EXPOSURE")
elif camera_settings == sl.VIDEO_SETTINGS.EXPOSURE:
camera_settings = sl.VIDEO_SETTINGS.WHITEBALANCE_TEMPERATURE
str_camera_settings = "White Balance"
print("Camera settings: WHITEBALANCE")
elif camera_settings == sl.VIDEO_SETTINGS.WHITEBALANCE_TEMPERATURE:
camera_settings = sl.VIDEO_SETTINGS.BRIGHTNESS
str_camera_settings = "Brightness"
print("Camera settings: BRIGHTNESS")
def record(cam, runtime, mat):
vid = sl.ERROR_CODE.FAILURE
out = False
while vid != sl.ERROR_CODE.SUCCESS and not out:
filepath = input("Enter filepath name: ")
record_param = sl.RecordingParameters(filepath)
vid = cam.enable_recording(record_param)
print(repr(vid))
if vid == sl.ERROR_CODE.SUCCESS:
print("Recording started...")
out = True
print("Hit spacebar to stop recording: ")
key = False
while key != 32: # for spacebar
err = cam.grab(runtime)
if err == sl.ERROR_CODE.SUCCESS:
cam.retrieve_image(mat)
cv2.imshow("ZED", mat.get_data())
key = cv2.waitKey(5)
else:
print("Help: you must enter the filepath + filename + SVO extension.")
print("Recording not started.")
cam.disable_recording()
print("Recording finished.")
cv2.destroyAllWindows()
if __name__ == "__main__":
main()