|
最近在学摄像头,想用摄像头做一个循迹的功能,运用的EloquentArduino这个库,连接如下:eloquentarduino · GitHub,我已经将像素点调的很小了,但还是会报错
代码如下
#define CAMERA_MODEL_AI_THINKER
#include "esp_camera.h"
#include "camera_pins.h"
#define FRAME_SIZE FRAMESIZE_QVGA
#define WIDTH 320
#define HEIGHT 240
#define BLOCK_SIZE 20
#define W (WIDTH / BLOCK_SIZE)
#define H (HEIGHT / BLOCK_SIZE)
#define BLOCK_DIFF_THRESHOLD 0.2
#define IMAGE_DIFF_THRESHOLD 0.1
#define DEBUG 1
uint16_t prev_frame[H][W] = { 0 };
uint16_t current_frame[H][W] = { 0 };
bool setup_camera(framesize_t);
bool capture_still();
void print_frame(uint16_t frame[H][W]);
int Xunji();
/**
*
*/
void setup() {
Serial.begin(115200);
Serial.println(setup_camera(FRAME_SIZE) ? "OK" : "ERR INIT");
}
/**
*
*/
void loop() {
if (!capture_still()) {
Serial.println("Failed capture");
delay(3000);
return;
}
Serial.println("=================");
}
/**
*
*/
bool setup_camera(framesize_t frameSize) {
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_GRAYSCALE;
config.frame_size = frameSize;
config.jpeg_quality = 12;
config.fb_count = 1;
bool ok = esp_camera_init(&config) == ESP_OK;
sensor_t *sensor = esp_camera_sensor_get();
sensor->set_framesize(sensor, frameSize);
return ok;
}
/**
* Capture image and do down-sampling
*/
bool capture_still() {
camera_fb_t *frame_buffer = esp_camera_fb_get();
if (!frame_buffer)
return false;
// set all 0s in current frame
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++)
current_frame[y][x] = 0;
// down-sample image in blocks
for (uint32_t i = 0; i < WIDTH * HEIGHT; i++) {
const uint16_t x = i % WIDTH;
const uint16_t y = floor(i / WIDTH);
const uint8_t block_x = floor(x / BLOCK_SIZE);
const uint8_t block_y = floor(y / BLOCK_SIZE);
const uint8_t pixel = frame_buffer->buf[i];
const uint16_t current = current_frame[block_y][block_x];
// average pixels in block (accumulate)
current_frame[block_y][block_x] += pixel;
}
// average pixels in block (rescale)
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++)
current_frame[y][x] /= BLOCK_SIZE * BLOCK_SIZE;
#if DEBUG
Xunji();
//Serial.println("Current frame:");
//print_frame(current_frame);
//Serial.println("---------------");
#endif
esp_camera_fb_return(frame_buffer);
return true;
}
/**
* For serial debugging
* @param frame
*/
void print_frame(uint16_t frame[H][W]) {
Serial.print('[');
for (int y = 0; y < H; y++) {
Serial.print('[');
for (int x = 0; x < W; x++) {
Serial.print(frame[y][x]);
Serial.print(',');
}
Serial.print(']');
Serial.print(',');
Serial.println();
}
Serial.print(']');
}
int Xunji(){
int sumx=0,sumy=0,ave[H],num=0,flg;
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
if(current_frame[y][x]<=100)
{
sumx=sumx+x;
num=num+1;
}
}
ave[y]=sumx/num;
sumx=0;
num=0;
}
for (int y = 0; y < H; y++){
sumy=sumy+ave[y];
}
flg=sumy/H;
if(flg>(W/2)){
Serial.println("左");
}
if(flg<(W/2)){
Serial.println("右");
}
return flg;
}
|
|