From 03eee013484ea28df9c25870650aa58ade0359a5 Mon Sep 17 00:00:00 2001 From: hesuicong Date: Fri, 14 Aug 2026 15:36:48 +0800 Subject: [PATCH] =?UTF-8?q?rect=E6=B7=BB=E5=8A=A0=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/MVS/SceneTexture.cpp | 60 ++++++++++++++------------------------- 1 file changed, 22 insertions(+), 38 deletions(-) diff --git a/libs/MVS/SceneTexture.cpp b/libs/MVS/SceneTexture.cpp index 2e11dd4..11dd0ce 100644 --- a/libs/MVS/SceneTexture.cpp +++ b/libs/MVS/SceneTexture.cpp @@ -10935,47 +10935,31 @@ void MeshTexture::GenerateTexture(bool bGlobalSeamLeveling, bool bLocalSeamLevel ASSERT(imageData.image.isInside(Point2f(aabb.ptMin))); ASSERT(imageData.image.isInside(Point2f(aabb.ptMax))); - if (bOriginFaceview) - { - texturePatch.rect.x = FLOOR2INT(aabb.ptMin[0])-border; - texturePatch.rect.y = FLOOR2INT(aabb.ptMin[1])-border; - texturePatch.rect.width = CEIL2INT(aabb.ptMax[0]-aabb.ptMin[0])+border*2; - texturePatch.rect.height = CEIL2INT(aabb.ptMax[1]-aabb.ptMin[1])+border*2; - } - else - { - texturePatch.rect.x = std::max(0, FLOOR2INT(aabb.ptMin[0])-border); - texturePatch.rect.y = std::max(0, FLOOR2INT(aabb.ptMin[1])-border); - // 限制尺寸不超过图像实际范围 + // ===== 替换 GenerateTexture 中第一个循环里设置 rect 的代码 ===== + if (bOriginFaceview) { + texturePatch.rect.x = FLOOR2INT(aabb.ptMin[0]) - border; + texturePatch.rect.y = FLOOR2INT(aabb.ptMin[1]) - border; + texturePatch.rect.width = CEIL2INT(aabb.ptMax[0] - aabb.ptMin[0]) + border * 2; + texturePatch.rect.height = CEIL2INT(aabb.ptMax[1] - aabb.ptMin[1]) + border * 2; + } else { + texturePatch.rect.x = std::max(0, FLOOR2INT(aabb.ptMin[0]) - border); + texturePatch.rect.y = std::max(0, FLOOR2INT(aabb.ptMin[1]) - border); const cv::Mat& img = images[texturePatch.label].image; - texturePatch.rect.width = std::min( - CEIL2INT(aabb.ptMax[0]-aabb.ptMin[0])+border*2, - img.cols - texturePatch.rect.x - ); - texturePatch.rect.height = std::min( - CEIL2INT(aabb.ptMax[1]-aabb.ptMin[1])+border*2, - img.rows - texturePatch.rect.y - ); + texturePatch.rect.width = CEIL2INT(aabb.ptMax[0] - aabb.ptMin[0]) + border * 2; + texturePatch.rect.height = CEIL2INT(aabb.ptMax[1] - aabb.ptMin[1]) + border * 2; + // 裁剪到图像范围内 + texturePatch.rect.width = std::min(texturePatch.rect.width, img.cols - texturePatch.rect.x); + texturePatch.rect.height = std::min(texturePatch.rect.height, img.rows - texturePatch.rect.y); } - /* - // 设置 texturePatch.rect 后添加边界检查 - texturePatch.rect.width = std::max(1, texturePatch.rect.width); // 宽度至少为1 - texturePatch.rect.height = std::max(1, texturePatch.rect.height); // 高度至少为1 - - // 检查ROI是否超出图像实际范围 - if (texturePatch.rect.x < 0) { - texturePatch.rect.width += texturePatch.rect.x; // 修正负起点 - texturePatch.rect.x = 0; - } - if (texturePatch.rect.y < 0) { - texturePatch.rect.height += texturePatch.rect.y; - texturePatch.rect.y = 0; - } - // 确保ROI不超出图像右/下边界 - texturePatch.rect.width = std::min(texturePatch.rect.width, imageData.image.cols - texturePatch.rect.x); - texturePatch.rect.height = std::min(texturePatch.rect.height, imageData.image.rows - texturePatch.rect.y); - //*/ + // ★ 关键修复:确保 rect 至少 1x1,防止后续空 Mat 写越界 + if (texturePatch.rect.width <= 0 || texturePatch.rect.height <= 0) { + // 退化成一个 1x1 的 patch,避免后续崩溃 + texturePatch.rect.width = 1; + texturePatch.rect.height = 1; + texturePatch.rect.x = std::min(texturePatch.rect.x, images[texturePatch.label].image.cols - 1); + texturePatch.rect.y = std::min(texturePatch.rect.y, images[texturePatch.label].image.rows - 1); + } ASSERT(imageData.image.isInside(texturePatch.rect.tl())); ASSERT(imageData.image.isInside(texturePatch.rect.br()));