ESP32_cam access and resize image

cool.codecoding
Posts: 1
Joined: Fri Oct 09, 2020 6:35 am

ESP32_cam access and resize image

Postby cool.codecoding » Fri Oct 09, 2020 6:53 am

Hey,
i would like to directly access and scale the images taken by a esp32_cam aithinker. (Full code down below)

The main goal is to have the image in a 2D array with (e.g.) 28x28 pixels. Therefore I save the image into the buffer with

Code: Select all

     camera_fb_t * frame = NULL;
     frame = esp_camera_fb_get();
I have declared the format to be PIXFORMAT_GRAYSCALE, and therefore the data shouldn't be converted to jpeg, correct?
When trying to Serial.print() some values with

Code: Select all

for (int i =o; i <5; i++){
Serial.println(frame->buf[i]);
}
i do get some values like

Code: Select all

29
30
32
28
37
I was curious if these were already the grayscale values (between 0 and 255) of each pixel, but the values did neither become 0 nor 255 whenever i covered the whole camera or put a light source close to it.

The questions are:
How do I get the grayscale values from each pixel?
How can I put the values into a 2D array so I can easily Serial.print() the values (to check if things work correctly)?
How can I reduce the size of the image (maybe some code snippets?)

Im not too much into c++, so i would be happy with any advice!

Code: Select all

#define  CAMERA_MODEL_AI_THINKER
#include <esp_camera.h>
 #include "camera_pins.h"

 #define FRAME_SIZE FRAMESIZE_QQVGA
 #define WIDTH 160
 #define HEIGHT 120    

 uint16_t img_array [HEIGHT][WIDTH] = { 0 };


 bool setup_camera(framesize_t);
 void frame_to_array(camera_fb_t * frame);
 void print_image_shape(camera_fb_t * frame);
 bool capture_image();

 void setup() {
     Serial.begin(115200);
     Serial.println(setup_camera(FRAME_SIZE) ? "OK" : "ERR INIT");
 }

 void loop() {
     if (!capture_image()) {
         Serial.println("Failed capture");
         delay(2000);

         return;
     }

     //print_features();
     delay(3000);
 }



 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_QQVGA;
     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;
 }



 bool capture_image() {

     camera_fb_t * frame = NULL;
     frame = esp_camera_fb_get();

     print_image_shape(frame);

    uint8_t ** out = NULL;
    size_t* out_len = 0;

    bool converted = fmt2bmp(frame->buf, frame->len, frame->width, frame->height, frame->format, out, out_len);
    
    //bool converted = fmt2rgb888(frame->buf, frame->len, frame->format, rgb_buf);
    //bool converted = frame2bmp(frame, &buf, &buf_len);

    if(!converted){
        Serial.println("BMP conversion failed");
        return ESP_FAIL;
    }
     esp_camera_fb_return(frame);

     if (!frame)
         return false;

     return true;
 }


 void print_image_shape(camera_fb_t * frame){

     // print shape of image and total length (=heigth*width)
     Serial.print("Width: ");
     Serial.print(frame->width);
     Serial.print("\tHeigth: ");
     Serial.print(frame->height);
     Serial.print("\tLength: ");
     Serial.println(frame->len);
 }

alanesq
Posts: 86
Joined: Thu Dec 14, 2017 8:38 pm

Re: ESP32_cam access and resize image

Postby alanesq » Fri Oct 09, 2020 4:15 pm

Hi,

The 5 numbers you are capturing should be the raw greyscale values as you suspect.
The camera by default I believe has auto gain enabled so as you make the image darker it may well be turning the image brightness up to compensate which may explain why you are not seeing the results you expect.

You may like to have a look at this as I think it is basically doing exactly what you are looking for
https://eloquentarduino.github.io/2020/ ... o-version/

Who is online

Users browsing this forum: No registered users and 84 guests