Overview #
This example explains how to align depth and color streams captured from different viewpoints, and highlights potential issues such as sampling artifacts and occlusion.
Expect Output #

Prerequisites #
Tutorial #
C++
Mat colorAligned, depthAligned;
if (align_mode == 0) {
if (depthColor.size() != colorBGR.size()) {
resize(depthColor, depthAligned, colorBGR.size(), 0, 0, INTER_NEAREST);
}
else {
depthAligned = depthColor;
}
colorAligned = colorBGR.clone();
}
else {
if (colorBGR.size() != depthColor.size()) {
resize(colorBGR, colorAligned, depthColor.size(), 0, 0, INTER_NEAREST);
}
else {
colorAligned = colorBGR;
}
depthAligned = depthColor.clone();
} - Implements two alignment strategies controlled by
align_mode:- Mode 0 (Depth→Color): resize the depth image to match the color resolution.
- Mode 1 (Color→Depth): resize the color image to match the depth resolution.
- Uses
INTER_NEARESTfor resizing to avoid creating non-existent values in depth-derived data. - If hardware registration (
regEnabled) is ON and resolutions already match, the resize becomes a no-op (identity mapping).
C++
double alpha = alpha_slider / 100.0; // color weight
double beta = 1.0 - alpha; // depth weight
Mat blended;
addWeighted(colorAligned, alpha, depthAligned, beta, 0.0, blended);
// ----------- Add text overlay -----------
string modeText = (align_mode == 0) ?
"Mode: Depth -> Color (output size = Color)" :
"Mode: Color -> Depth (output size = Depth)";
putText(blended, modeText, Point(10, 25),
FONT_HERSHEY_SIMPLEX, 0.7, Scalar(0, 255, 0), 2);
char alphaTxt[64];
snprintf(alphaTxt, sizeof(alphaTxt), "Color Alpha: %.0f %%", alpha * 100.0);
putText(blended, alphaTxt, Point(10, 55),
FONT_HERSHEY_SIMPLEX, 0.7, Scalar(0, 255, 255), 2);
if (!regEnabled) {
putText(blended, "Warning: Registration OFF (simple resize only)",
Point(10, 85),
FONT_HERSHEY_SIMPLEX, 0.6, Scalar(0, 0, 255), 2);
}- Uses
addWeightedto overlay color and depth:alpha= contribution of the color image.beta= contribution of the depth image.
- Displays the current alignment mode and alpha value as on-screen text.
