使用OpenCvSharp包装器,我一直在使用此功能来调整图像大小(保留长宽比)
public static void Resize_PreserveAspectRatio(this Mat mat, Mat dst, int length, InterpolationFlags st = InterpolationFlags.Cubic, bool changeMaxLength = true)
{
double w = mat.Width;
double h = mat.Height;
double div = changeMaxLength ? Math.Max(w, h) : Math.Min(w, h);
double w1 = (w / div) * length;
double h1 = (h / div) * length;
Cv2.Resize(mat, dst, new Size(w1, h1), 0d, 0d, st);
}
当将宽度为1920像素的图像调整为200像素的大小时,我意识到即使使用三次插值,结果看起来也很糟糕。我已经尝试过不直接使用OpenCV调整大小的代码,而是先使用PyrDown:
public static void Resize_PreserveAspectRatio(this Mat mat, Mat dst, int length, InterpolationFlags st = InterpolationFlags.Cubic, bool changeMaxLength = true)
{
double w = mat.Width;
double h = mat.Height;
double len2x = length * 2d;
double div = changeMaxLength ? Math.Max(w, h) : Math.Min(w, h);
if (div > len2x)
{
using (Mat mat1 = mat.Clone())
{
while (div > len2x)
{
Cv2.PyrDown(mat1, mat1);
w = mat1.Width;
h = mat1.Height;
div = changeMaxLength ? Math.Max(w, h) : Math.Min(w, h);
}
double w1 = (w / div) * length;
double h1 = (h / div) * length;
Cv2.Resize(mat1, dst, new Size(w1, h1), 0d, 0d, st);
}
}
else
{
double w1 = (w / div) * length;
double h1 = (h / div) * length;
Cv2.Resize(mat, dst, new Size(w1, h1), 0d, 0d, st);
}
}
结果是:
这是正常现象吗,还是OpenCV Resize函数(或包装器)有问题?
编辑:
我实际上要问的是,这些结果是否正常?
Edit2