/// <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 = 0; for (int y = 0; y < height; y = y + scale) { for (int x = 0; x < width; x = x + scale) { int index = y * width + x; int sum = 0; for (int i = 0; i < scale; i++) { for (int j = 0; j < scale; j++) { sum += source[index + i * width + j]; } } dest[n] = Convert.ToUInt16(sum / (scale * scale)); n++; } } return dest; }
原图是灰度数据,最终的目的是将图像缩小了N倍,然后取周围N*N个点的灰度平均值作为新图像的值,目前这个实现复杂度有点高,有更优解请评论回复