【问题标题】:Image downscaling is having problem with odd numbers shrinking using nearest neighbor algorithm c#图像缩小存在使用最近邻算法 c# 缩小奇数的问题
【发布时间】:2021-02-15 22:07:48
【问题描述】:

我正在缩小 [1/1, 1/2 到 1/10] 我的 ByteImage 输入。它适用于偶数,如 1/10,1/2 等,但不适用于奇数,如 1/3、1/7 等。随着奇数缩小,右下侧正向右侧移动更多,因为左上侧完好无损。

我将目标宽度、高度作为处理的输入

 int width = Math.Max(image.Image.Width / reductionFactor, 1); //target width
 int height = Math.Max(image.Image.Height / reductionFactor, 1); //target height

其中reductionFactor是从UI输入的,可以是1/[1 to 10] image 对象是来自 custom Image 类 的引用,它有自己的宽度、高度属性。 我在下面调用主要的图像处理方法。 srcWidth 是图像的源或原始宽度。

 var byteImage = new ByteImage(width, height, image.Image.PixelFormat,
                        resizePixels(image.Image.Data, image.Image.Width, width, height, reductionFactor, image.Image.PixelFormat), null);

我的缩小方法如下所示

 public ImmutableArray<byte> resizePixels(ImmutableArray<byte> data, int srcWidth, int width, int height, int downscalingFactor, 
                ByteImagePixelFormat pixelFormat)
            {
                var builder = ImmutableArray.CreateBuilder<byte>(width * height * pixelFormat.GetBytesPerPixel());
                builder.Count = builder.Capacity;

                int srcIndex = 0;
                int dstIndex = 0;
                int incPixel = (downscalingFactor - 1) * pixelFormat.GetBytesPerPixel() + 1;
                int incLine = (downscalingFactor - 1) * srcWidth * pixelFormat.GetBytesPerPixel();

                switch (pixelFormat)
                {
                     case ByteImagePixelFormat.Rgb:
                        for (int i = 0; i < height; i++)
                        {
                            for (int j = 0; j < width; j++)
                            {
                                builder[dstIndex++] = data[srcIndex++];
                                builder[dstIndex++] = data[srcIndex++];
                                builder[dstIndex++] = data[srcIndex];

                                srcIndex += incPixel;
                            }
                            srcIndex += incLine;
                        }
                        break;

                    case ByteImagePixelFormat.Argb:
                        for (int i = 0; i < height; i++)
                        {
                            for (int j = 0; j < width; j++)
                            {
                                builder[dstIndex++] = data[srcIndex++];
                                builder[dstIndex++] = data[srcIndex++];
                                builder[dstIndex++] = data[srcIndex++];
                                builder[dstIndex++] = data[srcIndex];

                                srcIndex += incPixel;
                            }
                            srcIndex += incLine;
                        }
                        break;
                }
                return builder.ToImmutable();
            }
        }

我发现如果我忽略下面这条线,它会为目标图像创造更多角度

 int incLine = (downscalingFactor - 1) * srcWidth * pixelFormat.GetBytesPerPixel();

在计算我认为丢失的像素时,我在计算中遗漏了一些东西。

【问题讨论】:

    标签: c# algorithm image-processing image-resizing nearest-neighbor


    【解决方案1】:

    我相信你是对的,只是在那一行有问题

     int incLine = (downscalingFactor - 1) * srcWidth * pixelFormat.GetBytesPerPixel();
    

    请尝试

    int incLine = (downscalingFactor - 1) * srcWidth * pixelFormat.GetBytesPerPixel() + (srcWidth - width * downscalingFactor) * pixelFormat.GetBytesPerPixel();
    

    【讨论】:

      猜你喜欢
      • 2016-03-06
      • 2017-12-28
      • 1970-01-01
      • 2012-03-23
      • 1970-01-01
      • 2011-03-06
      • 2010-12-16
      • 2012-03-02
      相关资源
      最近更新 更多