图像缩小在使用最近邻算法 c# 缩小奇数时遇到问题

Image downscaling is having problem with odd numbers shrinking using nearest neighbor algorithm c#

我正在缩小 [1/1, 1/2 到 1/10] 我的 ByteImage input.Its 对于像 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到10] 并且 image 对象是来自 custom Image class 的引用,它有自己的宽度、高度属性。 我在下面这样调用主要的图像处理方法。 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();

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

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

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

请尝试

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