首页 > 解决方案 > 我应该在调整大小之前使用 PyrDown 以获得更好的结果吗?

问题描述

使用 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 函数(或包装器)有问题?

编辑:

我实际上要问的是,这些结果正常吗?

在此处输入图像描述

原图: 在此处输入图像描述

编辑2

根据thisthis我的下采样结果是正常的。

标签: opencvinterpolationimage-resizingopencvsharp

解决方案


推荐阅读