基于esp32s3使用opencv算法的本地颜色识别代码

  1. Opencv简介

       OpenCV是一个基于Apache2.0许可(开源)发行的跨平台计算机视觉和机器学习软件库,可以运行在LinuxWindowsAndroidMac OS操作系统上它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了PythonRubyMATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

       这就使我们在esp32上实现OpenCV,进行图像处理以及计算机视觉成为了现实。

        可以使用S2W-M03进行验证,OpenCV运行在S2W-M03上使用OV2640摄像头。可以某宝搜索S2W-M03或者OpenCV开发板。https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-4347241106.24.29763994SKDegm&id=737333747158

    2、Esp32中的代码

void find_color(void *arg)

{

    ESP_LOGI(TAG, "Starting find_color");

    sensor_t *s = esp_camera_sensor_get();    ///读取摄像头

    lvCameraImage = lv_img_create(lv_disp_get_scr_act(nullptr), nullptr);///使用LVGL屏幕显示颜色识别结果。

    lv_obj_move_foreground(lvCameraImage);

    while (true)

    {

        auto start = esp_timer_get_time();

        camera_fb_t *fb = esp_camera_fb_get(); 读取摄像头一帧数据

        if (!fb)

        {

            ESP_LOGE(TAG, "Camera capture failed");

        }

        else

        {

            if (s->pixformat == PIXFORMAT_JPEG)///JPEG格式,直接显示在屏幕

            {

                TFT_jpg_image(CENTER, CENTER, 0, -1, NULL, fb->buf, fb->len);

                esp_camera_fb_return(fb);

                fb = NULL;

            }

            else

            {  // RGB565 pixformat

                Mat inputImage(fb->height, fb->width, CV_8UC2, fb->buf); // rgb565 is 2 channels of 8-bit unsigned

                cvtColor(inputImage, inputImage, COLOR_BGR5652BGR);

                int pos_x = fb->width / 2;

                int pos_y = fb->height / 2;

                int blue = inputImage.at(pos_x, pos_y)[0];  // getting the pixel values//

                int green = inputImage.at(pos_x, pos_y)[1]; // getting the pixel values//

                int red = inputImage.at(pos_x, pos_y)[2];   // getting the pixel values//

                updateColorCode(red, green, blue);

                std::vector bgr_planes;

                split(inputImage, bgr_planes);

                float range[] = {0, 240};

                const float *histRange = {range};

                bool uniform = true;

                bool accumulate = false;

                //printf("\r\ndraw\r\n");  计算RGB数据,并在LCD上面显示

                Mat b_hist, g_hist, r_hist;

                calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize,

                         &histRange, uniform, accumulate);

                calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize,

                         &histRange, uniform, accumulate);

                calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize,

                         &histRange, uniform, accumulate);

                drawHistogram(b_hist, g_hist, r_hist, inputImage);

                drawCenterMark(inputImage);

                updateCameraImage(inputImage);

            }

           esp_camera_fb_return(fb);

        }

     ESP_LOGI(TAG, "Around %f fps", 1.0f / ((esp_timer_get_time() - start) / 1000000.0f));

    }

}

以上代码已经在S2W-M03模块验证,可以提供整个工程源码。

你可能感兴趣的:(opencv,算法,esp32s3,源码,颜色识别,图像识别,OpenCV)