首页 > 解决方案 > 图像缩小存在使用最近邻算法 c# 缩小奇数的问题

问题描述

我正在缩小 [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 到 10],图像对象是从自定义 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#algorithmimage-processingimage-resizingnearest-neighbor

解决方案


我相信你是对的,只是在那条线上有问题

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

请试试

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

推荐阅读