fix: switch sdk roi for full-frame capture

This commit is contained in:
Ben 2026-08-26 11:54:17 +08:00
parent 7bf543ec0a
commit 756bd1d64a
2 changed files with 208 additions and 67 deletions

View File

@ -116,11 +116,17 @@ std::mutex& OutputRoiModeMutex()
return mutex; return mutex;
} }
class OutputRoiModeRestoreGuard class FullFrameRoiRestoreGuard
{ {
public: public:
explicit OutputRoiModeRestoreGuard(VZNLHANDLE deviceHandle) FullFrameRoiRestoreGuard(VZNLHANDLE deviceHandle,
: m_guardDeviceHandle(deviceHandle) const SVzNLROIRect& initialLeftROI,
const SVzNLROIRect& initialRightROI,
unsigned int frameWidth,
unsigned int frameHeight)
: m_guardDeviceHandle(deviceHandle),
m_initialLeftROI(initialLeftROI),
m_initialRightROI(initialRightROI)
{ {
int queryError = 0; int queryError = 0;
m_previousCalibRoi = VzNL_IsEnableCalibROI(m_guardDeviceHandle, &queryError); m_previousCalibRoi = VzNL_IsEnableCalibROI(m_guardDeviceHandle, &queryError);
@ -132,23 +138,43 @@ public:
return; return;
} }
if (m_previousCalibRoi == VzTrue) { VzNL_SetOutputROIImage(VzTrue);
const int ret = VzNL_EnableCalibROI(m_guardDeviceHandle, VzFalse); int ret = VzNL_EnableCalibROI(m_guardDeviceHandle, VzTrue);
if (ret != 0) { if (ret != 0) {
char errorMsg[256] = {0}; char errorMsg[256] = {0};
VzNL_GetErrorInfo(ret, errorMsg); VzNL_GetErrorInfo(ret, errorMsg);
LOG_ERROR("[VizumCamera] Cannot disable CalibROI for true full-frame pixels: {} " LOG_ERROR("[VizumCamera] Cannot enable CalibROI before configuring full-frame ROI: {} "
"(error code: {})", errorMsg, ret); "(error code: {})", errorMsg, ret);
return; return;
} }
m_calibRoiChanged = true; m_restoreRequired = true;
SVzNLROIRect fullLeftROI{};
fullLeftROI.right = frameWidth;
fullLeftROI.bottom = frameHeight;
SVzNLROIRect fullRightROI{};
fullRightROI.right = frameWidth;
fullRightROI.bottom = frameHeight;
ret = VzNL_ConfigDetectROI(m_guardDeviceHandle, &fullLeftROI, &fullRightROI);
if (ret != 0) {
char errorMsg[256] = {0};
VzNL_GetErrorInfo(ret, errorMsg);
LOG_ERROR("[VizumCamera] Cannot configure full-frame detection ROI: {} "
"(error code: {}), requested=[0, {}, 0, {}]",
errorMsg, ret, frameWidth, frameHeight);
RestoreNow();
return;
} }
VzNL_SetOutputROIImage(VzFalse);
m_outputModeChanged = true;
m_ready = true; m_ready = true;
LOG_INFO("[VizumCamera] Full-frame 2D mode applied: CalibROI=false, " LOG_INFO("[VizumCamera] Temporary full-frame ROI configured through SDK: "
"OutputROI=false; configured detection ROI and point-cloud ROI unchanged"); "left/right=[0, {}, 0, {}]; initial left=[{}, {}, {}, {}], "
"right=[{}, {}, {}, {}]",
frameWidth, frameHeight,
m_initialLeftROI.left, m_initialLeftROI.right,
m_initialLeftROI.top, m_initialLeftROI.bottom,
m_initialRightROI.left, m_initialRightROI.right,
m_initialRightROI.top, m_initialRightROI.bottom);
} }
bool IsReady() const bool IsReady() const
@ -158,50 +184,75 @@ public:
bool RestoreNow() bool RestoreNow()
{ {
if (m_restoreAttempted) if (m_restoreAttempted && m_restoreSucceeded)
return m_restoreSucceeded; return m_restoreSucceeded;
m_restoreAttempted = true; m_restoreAttempted = true;
if (!m_outputModeChanged && !m_calibRoiChanged) { if (!m_restoreRequired) {
m_restoreSucceeded = true; m_restoreSucceeded = true;
return true; return true;
} }
if (m_outputModeChanged)
VzNL_SetOutputROIImage(VzTrue);
m_restoreSucceeded = true; m_restoreSucceeded = true;
if (m_calibRoiChanged) { int ret = VzNL_EnableCalibROI(m_guardDeviceHandle, VzFalse);
const int ret = VzNL_EnableCalibROI(m_guardDeviceHandle, m_previousCalibRoi); if (ret != 0) {
if (ret != 0) { char errorMsg[256] = {0};
char errorMsg[256] = {0}; VzNL_GetErrorInfo(ret, errorMsg);
VzNL_GetErrorInfo(ret, errorMsg); LOG_ERROR("[VizumCamera] Failed to disable CalibROI before restoring initial ROI: {} "
LOG_ERROR("[VizumCamera] Failed to restore CalibROI after full-frame capture: {} " "(error code: {})", errorMsg, ret);
"(error code: {})", errorMsg, ret); m_restoreSucceeded = false;
m_restoreSucceeded = false;
}
} }
ret = VzNL_ConfigDetectROI(m_guardDeviceHandle, &m_initialLeftROI, &m_initialRightROI);
if (ret != 0) {
char errorMsg[256] = {0};
VzNL_GetErrorInfo(ret, errorMsg);
LOG_ERROR("[VizumCamera] Failed to restore initial detection ROI: {} "
"(error code: {}), left=[{}, {}, {}, {}], right=[{}, {}, {}, {}]",
errorMsg, ret,
m_initialLeftROI.left, m_initialLeftROI.right,
m_initialLeftROI.top, m_initialLeftROI.bottom,
m_initialRightROI.left, m_initialRightROI.right,
m_initialRightROI.top, m_initialRightROI.bottom);
m_restoreSucceeded = false;
}
ret = VzNL_EnableCalibROI(m_guardDeviceHandle, m_previousCalibRoi);
if (ret != 0) {
char errorMsg[256] = {0};
VzNL_GetErrorInfo(ret, errorMsg);
LOG_ERROR("[VizumCamera] Failed to restore CalibROI mode after full-frame capture: {} "
"(error code: {})", errorMsg, ret);
m_restoreSucceeded = false;
}
VzNL_SetOutputROIImage(VzTrue);
if (m_restoreSucceeded) { if (m_restoreSucceeded) {
LOG_INFO("[VizumCamera] ROI detection image mode restored: CalibROI={}, " LOG_INFO("[VizumCamera] Initial detection ROI restored after full-frame capture: "
"OutputROI=true", m_previousCalibRoi == VzTrue); "left=[{}, {}, {}, {}], right=[{}, {}, {}, {}], CalibROI={}",
m_initialLeftROI.left, m_initialLeftROI.right,
m_initialLeftROI.top, m_initialLeftROI.bottom,
m_initialRightROI.left, m_initialRightROI.right,
m_initialRightROI.top, m_initialRightROI.bottom,
m_previousCalibRoi == VzTrue);
} }
return m_restoreSucceeded; return m_restoreSucceeded;
} }
~OutputRoiModeRestoreGuard() ~FullFrameRoiRestoreGuard()
{ {
RestoreNow(); RestoreNow();
} }
OutputRoiModeRestoreGuard(const OutputRoiModeRestoreGuard&) = delete; FullFrameRoiRestoreGuard(const FullFrameRoiRestoreGuard&) = delete;
OutputRoiModeRestoreGuard& operator=(const OutputRoiModeRestoreGuard&) = delete; FullFrameRoiRestoreGuard& operator=(const FullFrameRoiRestoreGuard&) = delete;
private: private:
VZNLHANDLE m_guardDeviceHandle = nullptr; VZNLHANDLE m_guardDeviceHandle = nullptr;
SVzNLROIRect m_initialLeftROI{};
SVzNLROIRect m_initialRightROI{};
VzBool m_previousCalibRoi = VzFalse; VzBool m_previousCalibRoi = VzFalse;
bool m_calibRoiChanged = false; bool m_restoreRequired = false;
bool m_outputModeChanged = false;
bool m_ready = false; bool m_ready = false;
bool m_restoreAttempted = false; bool m_restoreAttempted = false;
bool m_restoreSucceeded = false; bool m_restoreSucceeded = false;
@ -1032,10 +1083,15 @@ bool VizumCamera::GetRightImage(SLImage& image)
return !image.empty(); return !image.empty();
} }
bool VizumCamera::CaptureFullFrameLeftImage(SLImage& image, std::string& frameId) bool VizumCamera::CaptureFullFrameLeftImage(SLImage& image,
std::string& frameId,
const ROIRect& initialLeftROI,
const ROIRect& initialRightROI,
bool& cameraStateRestored)
{ {
image.clear(); image.clear();
frameId.clear(); frameId.clear();
cameraStateRestored = false;
#ifdef ENABLE_VIZUM_CAMERA #ifdef ENABLE_VIZUM_CAMERA
const std::lock_guard<std::mutex> outputModeLock(OutputRoiModeMutex()); const std::lock_guard<std::mutex> outputModeLock(OutputRoiModeMutex());
const std::lock_guard<std::mutex> deviceLock(m_deviceAccessMutex); const std::lock_guard<std::mutex> deviceLock(m_deviceAccessMutex);
@ -1044,9 +1100,13 @@ bool VizumCamera::CaptureFullFrameLeftImage(SLImage& image, std::string& frameId
return false; return false;
} }
SVzNLROIRect oldLeftROI{}; const SVzNLROIRect expectedLeftROI{initialLeftROI.left, initialLeftROI.right,
SVzNLROIRect oldRightROI{}; initialLeftROI.top, initialLeftROI.bottom};
int ret = VzNL_GetConfigDetectROI(m_deviceHandle, &oldLeftROI, &oldRightROI); const SVzNLROIRect expectedRightROI{initialRightROI.left, initialRightROI.right,
initialRightROI.top, initialRightROI.bottom};
SVzNLROIRect currentLeftROI{};
SVzNLROIRect currentRightROI{};
int ret = VzNL_GetConfigDetectROI(m_deviceHandle, &currentLeftROI, &currentRightROI);
if (ret != 0) { if (ret != 0) {
char errorMsg[256] = {0}; char errorMsg[256] = {0};
VzNL_GetErrorInfo(ret, errorMsg); VzNL_GetErrorInfo(ret, errorMsg);
@ -1055,9 +1115,81 @@ bool VizumCamera::CaptureFullFrameLeftImage(SLImage& image, std::string& frameId
return false; return false;
} }
OutputRoiModeRestoreGuard outputModeGuard(m_deviceHandle); SVzVideoResolution resolution{};
if (!outputModeGuard.IsReady()) ret = VzNL_GetResolution(m_deviceHandle, &resolution);
if (ret != 0 || resolution.nFrameWidth == 0 || resolution.nFrameHeight == 0) {
char errorMsg[256] = {0};
VzNL_GetErrorInfo(ret, errorMsg);
LOG_ERROR("[VizumCamera] Full-frame capture cannot read a valid resolution: {} "
"(error code: {}), size={}x{}",
errorMsg, ret, resolution.nFrameWidth, resolution.nFrameHeight);
return false; return false;
}
if (!SameSdkRoi(currentLeftROI, expectedLeftROI)
|| !SameSdkRoi(currentRightROI, expectedRightROI)) {
LOG_ERROR("[VizumCamera] Current ROI differs from the connection-time point-cloud ROI; "
"restoring expected state and rejecting this capture. current left=[{}, {}, {}, {}], "
"right=[{}, {}, {}, {}]; expected left=[{}, {}, {}, {}], right=[{}, {}, {}, {}]",
currentLeftROI.left, currentLeftROI.right,
currentLeftROI.top, currentLeftROI.bottom,
currentRightROI.left, currentRightROI.right,
currentRightROI.top, currentRightROI.bottom,
expectedLeftROI.left, expectedLeftROI.right,
expectedLeftROI.top, expectedLeftROI.bottom,
expectedRightROI.left, expectedRightROI.right,
expectedRightROI.top, expectedRightROI.bottom);
FullFrameRoiRestoreGuard recoveryGuard(m_deviceHandle, expectedLeftROI, expectedRightROI,
resolution.nFrameWidth, resolution.nFrameHeight);
const bool recoverySucceeded = recoveryGuard.RestoreNow();
ret = VzNL_GetConfigDetectROI(m_deviceHandle, &currentLeftROI, &currentRightROI);
cameraStateRestored = recoverySucceeded && ret == 0
&& SameSdkRoi(currentLeftROI, expectedLeftROI)
&& SameSdkRoi(currentRightROI, expectedRightROI);
return false;
}
FullFrameRoiRestoreGuard roiGuard(m_deviceHandle, expectedLeftROI, expectedRightROI,
resolution.nFrameWidth, resolution.nFrameHeight);
if (!roiGuard.IsReady()) {
const bool restoreSucceeded = roiGuard.RestoreNow();
ret = VzNL_GetConfigDetectROI(m_deviceHandle, &currentLeftROI, &currentRightROI);
cameraStateRestored = restoreSucceeded && ret == 0
&& SameSdkRoi(currentLeftROI, expectedLeftROI)
&& SameSdkRoi(currentRightROI, expectedRightROI);
return false;
}
SVzNLROIRect configuredLeftROI{};
SVzNLROIRect configuredRightROI{};
ret = VzNL_GetConfigDetectROI(m_deviceHandle, &configuredLeftROI, &configuredRightROI);
const auto isFullFrameRoi = [&resolution](const SVzNLROIRect& roi) {
return roi.left == 0 && roi.top == 0
&& roi.right == resolution.nFrameWidth
&& roi.bottom == resolution.nFrameHeight;
};
if (ret != 0 || !isFullFrameRoi(configuredLeftROI) || !isFullFrameRoi(configuredRightROI)) {
char errorMsg[256] = {0};
VzNL_GetErrorInfo(ret, errorMsg);
LOG_ERROR("[VizumCamera] SDK full-frame ROI readback verification failed: {} "
"(error code: {}), left=[{}, {}, {}, {}], right=[{}, {}, {}, {}], "
"expected=[0, {}, 0, {}]",
errorMsg, ret,
configuredLeftROI.left, configuredLeftROI.right,
configuredLeftROI.top, configuredLeftROI.bottom,
configuredRightROI.left, configuredRightROI.right,
configuredRightROI.top, configuredRightROI.bottom,
resolution.nFrameWidth, resolution.nFrameHeight);
const bool restoreSucceeded = roiGuard.RestoreNow();
ret = VzNL_GetConfigDetectROI(m_deviceHandle, &currentLeftROI, &currentRightROI);
cameraStateRestored = restoreSucceeded && ret == 0
&& SameSdkRoi(currentLeftROI, expectedLeftROI)
&& SameSdkRoi(currentRightROI, expectedRightROI);
return false;
}
LOG_INFO("[VizumCamera] SDK full-frame ROI readback verified before 2D capture: "
"left/right=[0, {}, 0, {}]",
resolution.nFrameWidth, resolution.nFrameHeight);
SVzNLImageData* leftImage = nullptr; SVzNLImageData* leftImage = nullptr;
SVzNLImageData* rightImage = nullptr; SVzNLImageData* rightImage = nullptr;
@ -1080,36 +1212,36 @@ bool VizumCamera::CaptureFullFrameLeftImage(SLImage& image, std::string& frameId
const bool rightReleased = rightImageGuard.ReleaseNow(); const bool rightReleased = rightImageGuard.ReleaseNow();
const bool imagesReleased = leftReleased && rightReleased; const bool imagesReleased = leftReleased && rightReleased;
// CalibROI=false returns the raw eye frame (for example 1216x1920), while
// VzNL_GetResolution may report the calibrated output canvas (for example
// 1336x2115). They are different coordinate spaces and must not be compared.
const bool validFullFrame = converted && !image.empty() const bool validFullFrame = converted && !image.empty()
&& image.originX == 0 && image.originY == 0; && image.originX == 0 && image.originY == 0
&& image.width == resolution.nFrameWidth
&& image.height == resolution.nFrameHeight;
if (converted && !validFullFrame) { if (converted && !validFullFrame) {
LOG_ERROR("[VizumCamera] Raw full-frame image has an invalid origin or empty payload: " LOG_ERROR("[VizumCamera] Configured full-frame image contract failed: "
"image={}x{} origin=({}, {})", "image={}x{} origin=({}, {}), expected={}x{} origin=(0, 0)",
image.width, image.height, image.originX, image.originY); image.width, image.height, image.originX, image.originY,
resolution.nFrameWidth, resolution.nFrameHeight);
} }
const bool imageModesRestored = outputModeGuard.RestoreNow(); const bool initialRoiRestored = roiGuard.RestoreNow();
SVzNLROIRect currentLeftROI{};
SVzNLROIRect currentRightROI{};
ret = VzNL_GetConfigDetectROI(m_deviceHandle, &currentLeftROI, &currentRightROI); ret = VzNL_GetConfigDetectROI(m_deviceHandle, &currentLeftROI, &currentRightROI);
const bool roiUnchanged = ret == 0 const bool roiUnchanged = ret == 0
&& SameSdkRoi(currentLeftROI, oldLeftROI) && SameSdkRoi(currentLeftROI, expectedLeftROI)
&& SameSdkRoi(currentRightROI, oldRightROI); && SameSdkRoi(currentRightROI, expectedRightROI);
if (ret != 0) { if (ret != 0) {
char errorMsg[256] = {0}; char errorMsg[256] = {0};
VzNL_GetErrorInfo(ret, errorMsg); VzNL_GetErrorInfo(ret, errorMsg);
LOG_ERROR("[VizumCamera] Failed to query detection ROI after full-frame capture: {} " LOG_ERROR("[VizumCamera] Failed to query detection ROI after full-frame capture: {} "
"(error code: {})", errorMsg, ret); "(error code: {})", errorMsg, ret);
} else if (!roiUnchanged) { } else if (!roiUnchanged) {
LOG_ERROR("[VizumCamera] Detection ROI changed during output-only full-frame capture; " LOG_ERROR("[VizumCamera] Detection ROI was not restored after full-frame capture; "
"before left=[{}, {}, {}, {}], right=[{}, {}, {}, {}]; " "before left=[{}, {}, {}, {}], right=[{}, {}, {}, {}]; "
"after left=[{}, {}, {}, {}], right=[{}, {}, {}, {}]", "after left=[{}, {}, {}, {}], right=[{}, {}, {}, {}]",
oldLeftROI.left, oldLeftROI.right, oldLeftROI.top, oldLeftROI.bottom, expectedLeftROI.left, expectedLeftROI.right,
oldRightROI.left, oldRightROI.right, oldRightROI.top, oldRightROI.bottom, expectedLeftROI.top, expectedLeftROI.bottom,
expectedRightROI.left, expectedRightROI.right,
expectedRightROI.top, expectedRightROI.bottom,
currentLeftROI.left, currentLeftROI.right, currentLeftROI.left, currentLeftROI.right,
currentLeftROI.top, currentLeftROI.bottom, currentLeftROI.top, currentLeftROI.bottom,
currentRightROI.left, currentRightROI.right, currentRightROI.left, currentRightROI.right,
@ -1123,15 +1255,17 @@ bool VizumCamera::CaptureFullFrameLeftImage(SLImage& image, std::string& frameId
currentRightROI.top, currentRightROI.bottom); currentRightROI.top, currentRightROI.bottom);
} }
if (!validFullFrame || !imagesReleased || !imageModesRestored || !roiUnchanged) { cameraStateRestored = initialRoiRestored && roiUnchanged;
if (!validFullFrame || !imagesReleased || !cameraStateRestored) {
image.clear(); image.clear();
return false; return false;
} }
const uint64_t id = ++m_frameCounter; const uint64_t id = ++m_frameCounter;
frameId = "VZ_FULL_" + std::to_string(id); frameId = "VZ_FULL_" + std::to_string(id);
LOG_INFO("[VizumCamera] True full-frame 2D capture completed without writing detection ROI: " LOG_INFO("[VizumCamera] Full-frame 2D transaction completed: "
"frameId={}, output={}x{} origin=(0, 0), CalibROI restored", "frameId={}, output={}x{} origin=(0, 0), initial ROI restored",
frameId, image.width, image.height); frameId, image.width, image.height);
return true; return true;
#else #else

View File

@ -128,13 +128,20 @@ public:
bool GetRightImage(SLImage& image); bool GetRightImage(SLImage& image);
/** /**
* @brief 不修改检测 ROI,临时关闭 CalibROI/ROI输出并采集真实全幅左目图像 * @brief 临时把左右检测 ROI 配置为全幅,采集左目全图后恢复初始 ROI
* @param[out] image SDK 原始返回的全幅眼图(CalibROI=false 坐标系,原点必须为(0,0)) * @param[out] image SDK 返回的矫正全幅左目图(尺寸等于设备分辨率,原点必须为(0,0))
* @param[out] frameId 本次采集帧编号 * @param[out] frameId 本次采集帧编号
* @return 采集、SDK 图像释放、全幅原点校验、模式恢复及检测 ROI 未变化校验是否全部成功 * @param[in] initialLeftROI 连接时缓存的左目点云 ROI,也是唯一恢复目标
* @note 检测 ROI 始终只读;任何失败路径都会恢复原 CalibROI 和 ROI 图输出模式 * @param[in] initialRightROI 连接时缓存的右目点云 ROI,也是唯一恢复目标
* @param[out] cameraStateRestored 返回前是否确认初始 ROI、CalibROI 和输出模式均已恢复
* @return 全幅 ROI 配置、采集、SDK 图像释放、初始 ROI 恢复及校验是否全部成功
* @note 该事务按 SDK 官方示例调用 ConfigDetectROI;任何失败路径都会尝试恢复初始左右 ROI
*/ */
bool CaptureFullFrameLeftImage(SLImage& image, std::string& frameId); bool CaptureFullFrameLeftImage(SLImage& image,
std::string& frameId,
const ROIRect& initialLeftROI,
const ROIRect& initialRightROI,
bool& cameraStateRestored);
/** /**
* @brief 启用/禁用矫正 ROI 图像输出(VzNL_EnableCalibROI) * @brief 启用/禁用矫正 ROI 图像输出(VzNL_EnableCalibROI)