将一幅图像取平均值缩小N倍实现方法

        /// <summary>
/// 将图像缩小N倍
/// </summary>
/// <param name="source">原图数据</param>
/// <param name="height">原图高度</param>
/// <param name="width">原图宽度</param>
/// <param name="scale">缩小倍数</param>
/// <returns></returns>
public static ushort[] ImgNarrow(ushort[] source, int height, int width,int scale)
{
if (Convert.ToBoolean(height % scale) || Convert.ToBoolean(width % scale))
{
throw new Exception("高宽必须为" + scale + "的倍数");
}
ushort[] dest = new ushort[height * width / (scale * scale)];
int n = ;
for (int y = ; y < height; y = y + scale)
{
for (int x = ; x < width; x = x + scale)
{
int index = y * width + x;
int sum = ;
for (int i = ; i < scale; i++)
{
for (int j = ; j < scale; j++)
{
sum += source[index + i * width + j];
}
}
dest[n] = Convert.ToUInt16(sum / (scale * scale));
n++;
}
}
return dest;
}

原图是灰度数据,最终的目的是将图像缩小了N倍,然后取周围N*N个点的灰度平均值作为新图像的值,目前这个实现复杂度有点高,有更优解请评论回复

上一篇:html+css学习笔记 2[标签]


下一篇:用jq移除某一个特定样式