Skip to content

Commit e69eba5

Browse files
committed
Fix runtime calibration change in Align node
1 parent 950aa40 commit e69eba5

1 file changed

Lines changed: 45 additions & 29 deletions

File tree

src/pipeline/node/ImageAlign.cpp

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,43 @@ void ImageAlign::run() {
353353
auto latestConfig = initialConfig;
354354

355355
int previousShiftFactor = 0;
356+
bool refreshAlignToTransform = false;
356357

357358
ImgTransformation inputAlignToTransform;
358359
ImgFrame inputAlignToImgFrame;
359360
uint32_t currentEepromId = getParentPipeline().getEepromId();
360361

362+
auto updateAlignToData = [&](const std::shared_ptr<ImgFrame>& inputAlignToImg) {
363+
inputAlignToImgFrame = *inputAlignToImg;
364+
365+
auto alignTransform = inputAlignToImg->transformation;
366+
const auto alignToDistortion = alignTransform.getDistortionCoefficients();
367+
const bool hasDistortion = std::any_of(alignToDistortion.begin(), alignToDistortion.end(), [](float value) { return std::abs(value) > 0.0f; });
368+
if(hasDistortion) {
369+
logger->warn(
370+
"The input connected to inputAlignTo is distorted. The aligned image will still be undistorted, meaning it won't be perfectly "
371+
"aligned.");
372+
}
373+
374+
alignTo = static_cast<CameraBoardSocket>(inputAlignToImg->getInstanceNum());
375+
if(alignWidth == 0 || alignHeight == 0) {
376+
alignWidth = inputAlignToImg->getWidth();
377+
alignHeight = inputAlignToImg->getHeight();
378+
}
379+
380+
auto alignTransformForIntrinsics = alignTransform;
381+
auto [alignTransformWidth, alignTransformHeight] = alignTransformForIntrinsics.getSize();
382+
if(static_cast<int>(alignTransformWidth) != alignWidth || static_cast<int>(alignTransformHeight) != alignHeight) {
383+
float scaleX = static_cast<float>(alignWidth) / static_cast<float>(alignTransformWidth);
384+
float scaleY = static_cast<float>(alignHeight) / static_cast<float>(alignTransformHeight);
385+
alignTransformForIntrinsics.addScale(scaleX, scaleY);
386+
alignTransformForIntrinsics.setSize(alignWidth, alignHeight);
387+
}
388+
389+
alignSourceIntrinsics = alignTransformForIntrinsics.getIntrinsicMatrix();
390+
inputAlignToTransform = alignTransformForIntrinsics;
391+
};
392+
361393
while(mainLoop()) {
362394
std::shared_ptr<ImgFrame> inputImg = nullptr;
363395
std::shared_ptr<ImageAlignConfig> inConfig = nullptr;
@@ -371,35 +403,7 @@ void ImageAlign::run() {
371403
initialized = true;
372404

373405
auto inputAlignToImg = inputAlignTo.get<ImgFrame>();
374-
375-
inputAlignToImgFrame = *inputAlignToImg;
376-
377-
inputAlignToTransform = inputAlignToImg->transformation;
378-
const auto alignToDistortion = inputAlignToTransform.getDistortionCoefficients();
379-
const bool hasDistortion = std::any_of(alignToDistortion.begin(), alignToDistortion.end(), [](float value) { return std::abs(value) > 0.0f; });
380-
if(hasDistortion) {
381-
logger->warn(
382-
"The input connected to inputAlignTo is distorted. The aligned image will still be undistorted, meaning it won't be perfectly "
383-
"aligned.");
384-
}
385-
386-
alignTo = static_cast<CameraBoardSocket>(inputAlignToImg->getInstanceNum());
387-
if(alignWidth == 0 || alignHeight == 0) {
388-
alignWidth = inputAlignToImg->getWidth();
389-
alignHeight = inputAlignToImg->getHeight();
390-
}
391-
392-
auto alignTransformForIntrinsics = inputAlignToTransform;
393-
auto [alignTransformWidth, alignTransformHeight] = alignTransformForIntrinsics.getSize();
394-
if(static_cast<int>(alignTransformWidth) != alignWidth || static_cast<int>(alignTransformHeight) != alignHeight) {
395-
float scaleX = static_cast<float>(alignWidth) / static_cast<float>(alignTransformWidth);
396-
float scaleY = static_cast<float>(alignHeight) / static_cast<float>(alignTransformHeight);
397-
alignTransformForIntrinsics.addScale(scaleX, scaleY);
398-
alignTransformForIntrinsics.setSize(alignWidth, alignHeight);
399-
}
400-
401-
alignSourceIntrinsics = alignTransformForIntrinsics.getIntrinsicMatrix();
402-
inputAlignToTransform = alignTransformForIntrinsics;
406+
updateAlignToData(inputAlignToImg);
403407
}
404408

405409
if(inputConfig.getWaitForMessage()) {
@@ -451,10 +455,22 @@ void ImageAlign::run() {
451455
if(latestEepromId > currentEepromId) {
452456
logger->debug("EEPROM data changed (ID: {} -> {}), reconfiguring ...", currentEepromId, latestEepromId);
453457
calibrationSet = false;
458+
previousShiftFactor = 0;
459+
refreshAlignToTransform = true;
454460
calibHandler = pipeline.getCalibrationData();
455461
currentEepromId = latestEepromId;
456462
}
457463

464+
if(refreshAlignToTransform) {
465+
if(auto latestAlignToImg = inputAlignTo.tryGet<ImgFrame>()) {
466+
updateAlignToData(latestAlignToImg);
467+
refreshAlignToTransform = false;
468+
} else {
469+
logger->trace("Waiting for updated inputAlignTo frame after calibration change.");
470+
continue;
471+
}
472+
}
473+
458474
try {
459475
extractCalibrationData(width, height, alignWidth, alignHeight);
460476
} catch(const std::exception& e) {

0 commit comments

Comments
 (0)