-
-
Save 408881465/6c6f5a3363bab5ee29f1b581982da464 to your computer and use it in GitHub Desktop.
ESP32-Cam Pixel Downsampling - read out downsampled RGB pixels pretty pretty fast!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Code based on BITLUNI's ESP32-cam work | |
// the size you want to downsample to | |
int newWidth = 40; | |
int newHeight = 30; | |
int DSF = 160 / newWidth; // Down Sampling Factor - ideally DSF should be 4, 8 or 16.. so that the bitshifting happening below is accurate | |
void downSample(unsigned char *frame) | |
{ | |
for (int y = 0; y < newHeight; y++) | |
{ | |
for (int x = 0; x < newWidth; x++) | |
{ | |
int r = 0; | |
int g = 0; | |
int b = 0; | |
for (int j = 0; j < DSF; j++) | |
for (int i = 0; i < DSF; i++) | |
{ | |
unsigned char p[3]; | |
getPixel(x * DSF + i, y * DSF + j, frame, p); // this summarizes a "DSF" amount of camera pixels into the new down-sampled pixels, and sends it to getPixel which returns p[3] filled with rgb values. | |
r += p[0]; | |
g += p[1]; | |
b += p[2]; | |
} | |
// reducing/averaging the summarized pixels | |
newImage[y][x][0] = r >> DSF; | |
newImage[y][x][1] = g >> DSF; | |
newImage[y][x][2] = b >> DSF; | |
// averaging rgb into one greyscale value | |
grayscale[y][x] = (newImage[y][x][0] + newImage[y][x][1] + newImage[y][x][2]) / 3; | |
} | |
} | |
} | |
void getPixel(int x, int y, unsigned char *image, unsigned char *pixel) | |
{ | |
int p = image[(y * 160 + x) * 2 + 1] | (image[(y * 160 + x) * 2] << 8); | |
pixel[0] = (p >> 11) << 3; | |
pixel[1] = ((p >> 5) & 0b111111) << 2; | |
pixel[2] = (p & 0b11111) << 3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment