考虑您的图像是 N*M BW 位图。为简单起见char Letter[N][M]
,当允许的值为0
和时,我们将考虑它1
。现在考虑您要将其缩小到unsigned char letter[n][m]
. 这意味着每个灰度像素letter
将被计算为大位图中的白色像素数:
char Letter[N][M];
unsigned char letter[n][m];
int rect_sz_X = N / n; // the size of rectangle that will map to a single pixel
int rect_sz_Y = M / m; // in the downscaled image
int i, j, x, y;
for (i = 0; i < n; i++) for (j = 0; j < m; j++){
int sum = 0;
for (x = 0; x < rect_sz_X; x++) for (y = 0; y < rect_sz_Y; y++)
sum += Letter[i*rect_sz_X + x][j*rect_sz_Y + y];
letter[n][m] = ( sum * 255) / (rect_sz_X * rect_sz_Y);
};
请注意,创建像素的矩形可能会重叠(以防大小不可分割)。您的原始位图越大越好。