1

Using OpenCvSharp wrapper, I've been using this function to resize the images (preserving the aspect ratio)

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);
}

When resizing an image having a width of 1920 pixels to a size of 200 pixels I realized that the result looks bad even if I use Cubic interpolation. I have tried this code that does not use OpenCV resize directly, but use PyrDown first:

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);
            }
        }

The result is:

enter image description here

Is this normal, or is there something wrong with the OpenCV Resize function (or the wrapper)?

Edit:

What I'm actually asking is, are these results normal?

enter image description here

Original image: enter image description here

Edit2

According to this and this my downsampling results are normal.

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Koray
  • 1,768
  • 1
  • 27
  • 37
  • 1
    `pyrDown` applies a smoothing and that's what you are seeing here. Without using `resize`, you can try `pyrDown` alone to resize the image to the desired size by specifying the `dstsize` and assess the quality. See the [pyrDown documentation](https://docs.opencv.org/2.4/modules/imgproc/doc/filtering.html?highlight=pyrdown#pyrdown). You can resize this way as long as the mentioned conditions are satisfied. – dhanushka Apr 01 '19 at 04:25
  • @dhanushka I neither realized dstsize nor know about the kernel being used. Thank you. I was thinking it just averages 4 pixels to 1. Do you think the aliasing on the cubic resize result is normal? The side lines of the road, or the bright yellow part on the right bottom part. Shouldn't any kind of averaging decrease that yellow brightness. I feel this look like a nearest neighbor. – Koray Apr 01 '19 at 08:52
  • PyrDown dstsize cannot be used for a "desired size". It can be 1/2, 1/4.. etc. ratio of the src. – Koray Apr 01 '19 at 09:27
  • I think [this answers](https://stackoverflow.com/a/51042104/1266873) my question. – Koray Apr 05 '19 at 13:01
  • PyrDown dstsize can only be 1/2 (+-1 px) not 1/4. I have no idea why it is like that, why dstsize param exists. – Koray Apr 05 '19 at 18:53
  • I think it is there to adjust the size by that 1-pixel margin if you don't want the function to use its default dstsize calculation. If you check the documentation, it says it first convolves the image, then rejects the even rows and columns, so you can only achieve 1/2 at a time. Read more on [image pyramids](https://en.wikipedia.org/wiki/Pyramid_(image_processing)). Btw, In your solution, what you are doing is something like [mipmapping](https://en.wikipedia.org/wiki/Mipmap). – dhanushka Apr 07 '19 at 02:10

0 Answers0