fix: camera downscale — centered crop, explicit PSRAM frame buffer

This commit is contained in:
2026-04-14 09:30:20 -07:00
parent e19ae22915
commit 36f4becbe9

View File

@@ -25,6 +25,7 @@
bool camera_init() {
camera_config_t cfg = {};
cfg.fb_location = CAMERA_FB_IN_PSRAM;
cfg.ledc_channel = LEDC_CHANNEL_0;
cfg.ledc_timer = LEDC_TIMER_0;
cfg.pin_d0 = CAM_PIN_D0;
@@ -62,21 +63,22 @@ bool camera_init() {
return true;
}
// Box-filter downscale from QVGA (320x240) to 96x96 grayscale
// Box-filter downscale to CV_W x CV_H.
// Input region: center-cropped to (CV_W*bx) x (CV_H*by) before downscaling.
// For QVGA (320x240) → 96x96: bx=3, by=2, crops to 288x192, offset x=16 y=24.
static void downscale(const uint8_t* src, int src_w, int src_h, uint8_t* dst) {
int bx = src_w / CV_W;
int by = src_h / CV_H;
int bx = src_w / CV_W; // 3 for QVGA
int by = src_h / CV_H; // 2 for QVGA
// Center the crop region
int x_off = (src_w - CV_W * bx) / 2; // 16 for QVGA
int y_off = (src_h - CV_H * by) / 2; // 24 for QVGA
for (int dy = 0; dy < CV_H; dy++) {
for (int dx = 0; dx < CV_W; dx++) {
int sum = 0, cnt = 0;
int sum = 0;
for (int ky = 0; ky < by; ky++)
for (int kx = 0; kx < bx; kx++) {
int sx = dx * bx + kx;
int sy = dy * by + ky;
sum += src[sy * src_w + sx];
cnt++;
}
dst[dy * CV_W + dx] = (uint8_t)(sum / cnt);
for (int kx = 0; kx < bx; kx++)
sum += src[(y_off + dy*by + ky)*src_w + (x_off + dx*bx + kx)];
dst[dy*CV_W + dx] = (uint8_t)(sum / (bx * by));
}
}
}