C: grayscale histogram and a global threshold
A histogram counts, for each brightness level, how many pixels have that value. It summarizes an image's tonal range without storing its layout. A cluster of dark values and a cluster of bright values show up as two peaks, which suggests a natural place to separate foreground from background.
C
#include <stdio.h>
int main(void) {
unsigned char image[4][4] = {
{12, 20, 200, 210},
{15, 25, 220, 205},
{10, 30, 215, 230},
{18, 22, 225, 240},
};
int bins[8] = {0};
int total = 0;
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
bins[image[r][c] / 32]++;
total += image[r][c];
}
}
int mean = total / 16;
printf("histogram:");
for (int b = 0; b < 8; b++) {
printf(" %d", bins[b]);
}
printf("\nmean brightness: %d\n", mean);
for (int r = 0; r < 4; r++) {
for (int c = 0; c < 4; c++) {
putchar(image[r][c] > mean ? '#' : '.');
}
putchar('\n');
}
return 0;
}