fix: 修复双边滤波对16位图像执行失败的问题

OpenCV BilateralFilter仅支持8U和32F格式,16位图像(CV_16U)直接传入会抛异常。
修复方式:16位图像先转换为32F执行滤波,再转回原始位深。
This commit is contained in:
wei.lw.li
2026-08-11 16:29:46 +08:00
parent 81f1cfa0d1
commit bd7c8e422a
@@ -69,14 +69,32 @@ public class BilateralFilterProcessor<TDepth> : ImageProcessorBase<TDepth>
double sigmaColor = GetParameter<double>("SigmaColor");
double sigmaSpace = GetParameter<double>("SigmaSpace");
// 16 位时 sigmaColor 按比例放大(原始参数按 8 位语义定义)
double effectiveSigmaColor = sigmaColor;
if (typeof(TDepth) == typeof(ushort))
effectiveSigmaColor = sigmaColor * 256.0;
Image<Gray, TDepth> result;
if (typeof(TDepth) == typeof(ushort))
{
// OpenCV BilateralFilter only supports 8U and 32F.
// For 16-bit images: convert to 32F, apply filter, then convert back.
double effectiveSigmaColor = sigmaColor * 256.0;
using var floatInput = inputImage.Convert<Gray, float>();
using var floatResult = floatInput.CopyBlank();
CvInvoke.BilateralFilter(floatInput, floatResult, diameter, effectiveSigmaColor, sigmaSpace);
result = floatResult.Convert<Gray, TDepth>();
_logger.Debug("Process (16-bit via 32F): Diameter={D}, SigmaColor={SC}, SigmaSpace={SS}",
diameter, effectiveSigmaColor, sigmaSpace);
}
else
{
// 8-bit (byte) path: directly supported by OpenCV
result = inputImage.CopyBlank();
CvInvoke.BilateralFilter(inputImage, result, diameter, sigmaColor, sigmaSpace);
_logger.Debug("Process (8-bit): Diameter={D}, SigmaColor={SC}, SigmaSpace={SS}",
diameter, sigmaColor, sigmaSpace);
}
var result = inputImage.Clone();
CvInvoke.BilateralFilter(inputImage, result, diameter, effectiveSigmaColor, sigmaSpace);
_logger.Debug("Process: Diameter={D}, SigmaColor={SC}, SigmaSpace={SS}", diameter, effectiveSigmaColor, sigmaSpace);
return result;
}
}