c# - 图像缩小存在使用最近邻算法 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();
在计算我相信的丢失像素时,我在计算中遗漏了一些东西。
解决方案
我相信你是对的,只是在那条线上有问题
int incLine = (downscalingFactor - 1) * srcWidth * pixelFormat.GetBytesPerPixel();
请试试
int incLine = (downscalingFactor - 1) * srcWidth * pixelFormat.GetBytesPerPixel() + (srcWidth - width * downscalingFactor) * pixelFormat.GetBytesPerPixel();
推荐阅读
- angular - 没有找到 exportAs 'ngModel' 的指令。对于垫选择?
- go - 服务器中通常如何处理上下文取消错误?
- c++ - 默认初始化模板中的指针
- sas - SAS:将数据导出到特定范围的 Excel 文件
- python - python jupyter中的SUMIFS
- mongodb - mongodb等待连接
- c - st9720 LCD SPI 与 ATmega328P 通信
- scala - 使用 ZIO 测试套件集成测试 HTTP 服务器
- database - psql:致命:剩余的连接槽保留用于非复制超级用户连接
- flutter - 如何在未来延迟期间禁用自定义底部表单中的所有控件