fumiのアイコン画像
fumi 2024年01月02日作成 © MIT
セットアップや使用方法 セットアップや使用方法 閲覧数 1026
fumi 2024年01月02日作成 © MIT セットアップや使用方法 セットアップや使用方法 閲覧数 1026

Spresense HDRカメラ画像をILI9341液晶に高速表示(LovyanGFXを利用)

Spresense HDRカメラ画像をILI9341液晶に高速表示(LovyanGFXを利用)

目次

課題

Spresense HDRカメラで撮影した画像のLCD表示がおそい。(表示に0.7秒くらい)
下記サイトをほぼそのまま利用。
Spresense Arduino チュートリアル

やったこと

  1. サンプルだとどれくらい遅いのか確認
    撮影前後とLCD表示前後でSerial.printlnを入れて時間を表示してみる。
    →約740ミリ秒
  2. 撮影画像のサイズをQVGA(320x240)に変更してみる
    →約740ミリ秒
  3. 液晶表示ライブラリを変更してみる(LovyanGFX)
    →約32ミリ秒

コードとシリアルモニタ

サンプルそのまま

サンプルコードのディスプレイ表示の前後にシリアルモニタに文字を出力する。

#include <SPI.h> #include <Adafruit_GFX.h> #include <Adafruit_ILI9341.h> #define TFT_CS -1 #define TFT_RST 8 #define TFT_DC 9 Adafruit_ILI9341 tft = Adafruit_ILI9341(&SPI, TFT_DC, TFT_CS, TFT_RST); #include <SDHCI.h> #include <stdio.h> /* for sprintf */ #include <Camera.h> #define BAUDRATE (115200) #define TOTAL_PICTURE_COUNT (3) SDClass theSD; int take_picture_count = 0; /** * Print error message */ void printError(enum CamErr err) { Serial.print("Error: "); switch (err) { case CAM_ERR_NO_DEVICE: Serial.println("No Device"); break; case CAM_ERR_ILLEGAL_DEVERR: Serial.println("Illegal device error"); break; case CAM_ERR_ALREADY_INITIALIZED: Serial.println("Already initialized"); break; case CAM_ERR_NOT_INITIALIZED: Serial.println("Not initialized"); break; case CAM_ERR_NOT_STILL_INITIALIZED: Serial.println("Still picture not initialized"); break; case CAM_ERR_CANT_CREATE_THREAD: Serial.println("Failed to create thread"); break; case CAM_ERR_INVALID_PARAM: Serial.println("Invalid parameter"); break; case CAM_ERR_NO_MEMORY: Serial.println("No memory"); break; case CAM_ERR_USR_INUSED: Serial.println("Buffer already in use"); break; case CAM_ERR_NOT_PERMITTED: Serial.println("Operation not permitted"); break; default: break; } } /** * Callback from Camera library when video frame is captured. */ void CamCB(CamImage img) { /* Check the img instance is available or not. */ if (img.isAvailable()) { /* If you want RGB565 data, convert image data format to RGB565 */ Serial.println("Convert Image Start"); img.convertPixFormat(CAM_IMAGE_PIX_FMT_RGB565); Serial.println("Convert Image End"); /* You can use image data directly by using getImgSize() and getImgBuff(). * for displaying image to a display, etc. */ Serial.println("Display Image Start"); tft.drawRGBBitmap(0, 0, (uint16_t *)img.getImgBuff(), 320, 240); Serial.println("Display Image End"); // Serial.print("Image data size = "); // Serial.print(img.getImgSize(), DEC); // Serial.print(" , "); // Serial.print("buff addr = "); // Serial.print((unsigned long)img.getImgBuff(), HEX); // Serial.println(""); } else { Serial.println("Failed to get video stream image"); } } /** * @brief Initialize camera */ void setup() { CamErr err; /* Open serial communications and wait for port to open */ Serial.begin(BAUDRATE); while (!Serial) { ; /* wait for serial port to connect. Needed for native USB port only */ } /* Initialize SD */ while (!theSD.begin()) { /* wait until SD card is mounted. */ Serial.println("Insert SD card."); } /* begin() without parameters means that * number of buffers = 1, 30FPS, QVGA, YUV 4:2:2 format */ Serial.println("Prepare camera"); err = theCamera.begin(); if (err != CAM_ERR_SUCCESS) { printError(err); } /* Start video stream. * If received video stream data from camera device, * camera library call CamCB. */ Serial.println("Start streaming"); err = theCamera.startStreaming(true, CamCB); if (err != CAM_ERR_SUCCESS) { printError(err); } /* Auto white balance configuration */ Serial.println("Set Auto white balance parameter"); err = theCamera.setAutoWhiteBalanceMode(CAM_WHITE_BALANCE_DAYLIGHT); if (err != CAM_ERR_SUCCESS) { printError(err); } /* Set parameters about still picture. * In the following case, QUADVGA and JPEG. */ Serial.println("Set still picture format"); err = theCamera.setStillPictureImageFormat( CAM_IMGSIZE_QUADVGA_H, CAM_IMGSIZE_QUADVGA_V, CAM_IMAGE_PIX_FMT_JPG); if (err != CAM_ERR_SUCCESS) { printError(err); } tft.begin(40000000); tft.setRotation(3); } /** * @brief Take picture with format JPEG per second */ void loop() { // sleep(1); /* wait for one second to take still picture. */ delay(500); /* You can change the format of still picture at here also, if you want. */ /* theCamera.setStillPictureImageFormat( * CAM_IMGSIZE_HD_H, * CAM_IMGSIZE_HD_V, * CAM_IMAGE_PIX_FMT_JPG); */ /* This sample code can take pictures in every one second from starting. */ if (take_picture_count < TOTAL_PICTURE_COUNT) { /* Take still picture. * Unlike video stream(startStreaming) , this API wait to receive image data * from camera device. */ Serial.println("call takePicture()"); CamImage img = theCamera.takePicture(); /* Check availability of the img instance. */ /* If any errors occur, the img is not available. */ if (img.isAvailable()) { /* Create file name */ char filename[16] = {0}; sprintf(filename, "PICT%03d.JPG", take_picture_count); Serial.print("Save taken picture as "); Serial.print(filename); Serial.println(""); /* Remove the old file with the same file name as new created file, * and create new file. */ theSD.remove(filename); File myFile = theSD.open(filename, FILE_WRITE); myFile.write(img.getImgBuff(), img.getImgSize()); myFile.close(); } else { /* The size of a picture may exceed the allocated memory size. * Then, allocate the larger memory size and/or decrease the size of a picture. * [How to allocate the larger memory] * - Decrease jpgbufsize_divisor specified by setStillPictureImageFormat() * - Increase the Memory size from Arduino IDE tools Menu * [How to decrease the size of a picture] * - Decrease the JPEG quality by setJPEGQuality() */ Serial.println("Failed to take picture"); } } else if (take_picture_count == TOTAL_PICTURE_COUNT) { Serial.println("End."); theCamera.end(); } take_picture_count++; }

シリアルモニタ出力
表示に740msくらい。

11:43:36.288 -> Convert Image Start 11:43:36.288 -> Convert Image End 11:43:36.288 -> Display Image Start 11:43:37.027 -> Display Image End 11:43:37.124 -> Convert Image Start 11:43:37.124 -> Convert Image End 11:43:37.124 -> Display Image Start 11:43:37.864 -> Display Image End 11:43:37.960 -> Convert Image Start 11:43:37.960 -> Convert Image End 11:43:37.960 -> Display Image Start 11:43:38.700 -> Display Image End 11:43:38.796 -> Convert Image Start 11:43:38.796 -> Convert Image End 11:43:38.796 -> Display Image Start 11:43:39.536 -> Display Image End 11:43:39.632 -> Convert Image Start 11:43:39.632 -> Convert Image End 11:43:39.632 -> Display Image Start 11:43:40.370 -> Display Image End

サンプル+撮影画像縮小

162行目の撮影画像フォーマットの設定だけ変更

err = theCamera.setStillPictureImageFormat( CAM_IMGSIZE_QVGA_H, CAM_IMGSIZE_QVGA_V, CAM_IMAGE_PIX_FMT_JPG);

シリアルモニタ出力
変わらない。

11:56:09.933 -> Convert Image Start 11:56:09.933 -> Convert Image End 11:56:09.933 -> Display Image Start 11:56:10.672 -> Display Image End 11:56:10.768 -> Convert Image Start 11:56:10.768 -> Convert Image End 11:56:10.768 -> Display Image Start 11:56:11.508 -> Display Image End 11:56:11.604 -> Convert Image Start 11:56:11.604 -> Convert Image End 11:56:11.604 -> Display Image Start 11:56:12.344 -> Display Image End 11:56:12.440 -> Convert Image Start 11:56:12.440 -> Convert Image End 11:56:12.440 -> Display Image Start 11:56:13.181 -> Display Image End 11:56:13.276 -> Convert Image Start 11:56:13.276 -> Convert Image End 11:56:13.276 -> Display Image Start 11:56:14.016 -> Display Image End

LovyanGFX

lovyan03さんのライブラリ を使ってLCD表示を変更。

  1. LGFX_SPRESENSE_sample.hppLCD.hppとして保存。
  2. LovyanGFX/examples/HowToUse/5_images/5_images.ino at master · lovyan03/LovyanGFX · GitHubを参考に表示部分を変更。
    細かい使い方は上記コードにコメントとして書かれててとても助かりました。
#include <SPI.h> #include "LCD.hpp" #define TFT_CS -1 #define TFT_RST 8 #define TFT_DC 9 static LGFX lcd; #include <SDHCI.h> #include <stdio.h> /* for sprintf */ #include <Camera.h> #define BAUDRATE (115200) #define TOTAL_PICTURE_COUNT (3) SDClass theSD; int take_picture_count = 0; /** * Print error message */ void printError(enum CamErr err) { Serial.print("Error: "); switch (err) { case CAM_ERR_NO_DEVICE: Serial.println("No Device"); break; case CAM_ERR_ILLEGAL_DEVERR: Serial.println("Illegal device error"); break; case CAM_ERR_ALREADY_INITIALIZED: Serial.println("Already initialized"); break; case CAM_ERR_NOT_INITIALIZED: Serial.println("Not initialized"); break; case CAM_ERR_NOT_STILL_INITIALIZED: Serial.println("Still picture not initialized"); break; case CAM_ERR_CANT_CREATE_THREAD: Serial.println("Failed to create thread"); break; case CAM_ERR_INVALID_PARAM: Serial.println("Invalid parameter"); break; case CAM_ERR_NO_MEMORY: Serial.println("No memory"); break; case CAM_ERR_USR_INUSED: Serial.println("Buffer already in use"); break; case CAM_ERR_NOT_PERMITTED: Serial.println("Operation not permitted"); break; default: break; } } /** * Callback from Camera library when video frame is captured. */ void CamCB(CamImage img) { /* Check the img instance is available or not. */ if (img.isAvailable()) { /* If you want RGB565 data, convert image data format to RGB565 */ Serial.println("Convert Image Start"); img.convertPixFormat(CAM_IMAGE_PIX_FMT_RGB565); Serial.println("Convert Image End"); /* You can use image data directly by using getImgSize() and getImgBuff(). * for displaying image to a display, etc. */ Serial.println("Display Image Start"); lcd.pushImage(0, 0, 320, 240, (uint16_t *)img.getImgBuff()); Serial.println("Display Image End"); // Serial.print("Image data size = "); // Serial.print(img.getImgSize(), DEC); // Serial.print(" , "); // Serial.print("buff addr = "); // Serial.print((unsigned long)img.getImgBuff(), HEX); // Serial.println(""); } else { Serial.println("Failed to get video stream image"); } } /** * @brief Initialize camera */ void setup() { CamErr err; /* Open serial communications and wait for port to open */ Serial.begin(BAUDRATE); while (!Serial) { ; /* wait for serial port to connect. Needed for native USB port only */ } /* Initialize SD */ while (!theSD.begin()) { /* wait until SD card is mounted. */ Serial.println("Insert SD card."); } /* begin() without parameters means that * number of buffers = 1, 30FPS, QVGA, YUV 4:2:2 format */ Serial.println("Prepare camera"); err = theCamera.begin(); if (err != CAM_ERR_SUCCESS) { printError(err); } /* Start video stream. * If received video stream data from camera device, * camera library call CamCB. */ Serial.println("Start streaming"); err = theCamera.startStreaming(true, CamCB); if (err != CAM_ERR_SUCCESS) { printError(err); } /* Auto white balance configuration */ Serial.println("Set Auto white balance parameter"); err = theCamera.setAutoWhiteBalanceMode(CAM_WHITE_BALANCE_DAYLIGHT); if (err != CAM_ERR_SUCCESS) { printError(err); } /* Set parameters about still picture. * In the following case, QUADVGA and JPEG. */ Serial.println("Set still picture format"); err = theCamera.setStillPictureImageFormat( CAM_IMGSIZE_QUADVGA_H, CAM_IMGSIZE_QUADVGA_V, CAM_IMAGE_PIX_FMT_JPG); if (err != CAM_ERR_SUCCESS) { printError(err); } lcd.init(); lcd.setSwapBytes(true); lcd.setRotation(1); } /** * @brief Take picture with format JPEG per second */ void loop() { // sleep(1); /* wait for one second to take still picture. */ delay(500); /* You can change the format of still picture at here also, if you want. */ /* theCamera.setStillPictureImageFormat( * CAM_IMGSIZE_HD_H, * CAM_IMGSIZE_HD_V, * CAM_IMAGE_PIX_FMT_JPG); */ /* This sample code can take pictures in every one second from starting. */ if (take_picture_count < TOTAL_PICTURE_COUNT) { /* Take still picture. * Unlike video stream(startStreaming) , this API wait to receive image data * from camera device. */ Serial.println("call takePicture()"); CamImage img = theCamera.takePicture(); /* Check availability of the img instance. */ /* If any errors occur, the img is not available. */ if (img.isAvailable()) { /* Create file name */ char filename[16] = {0}; sprintf(filename, "PICT%03d.JPG", take_picture_count); Serial.print("Save taken picture as "); Serial.print(filename); Serial.println(""); /* Remove the old file with the same file name as new created file, * and create new file. */ theSD.remove(filename); File myFile = theSD.open(filename, FILE_WRITE); myFile.write(img.getImgBuff(), img.getImgSize()); myFile.close(); } else { /* The size of a picture may exceed the allocated memory size. * Then, allocate the larger memory size and/or decrease the size of a picture. * [How to allocate the larger memory] * - Decrease jpgbufsize_divisor specified by setStillPictureImageFormat() * - Increase the Memory size from Arduino IDE tools Menu * [How to decrease the size of a picture] * - Decrease the JPEG quality by setJPEGQuality() */ Serial.println("Failed to take picture"); } } else if (take_picture_count == TOTAL_PICTURE_COUNT) { Serial.println("End."); theCamera.end(); } take_picture_count++; }

シリアルモニタ出力
32ミリ秒くらい。

12:04:48.735 -> Convert Image Start 12:04:48.735 -> Convert Image End 12:04:48.735 -> Display Image Start 12:04:48.767 -> Display Image End 12:04:48.865 -> Convert Image Start 12:04:48.865 -> Convert Image End 12:04:48.865 -> Display Image Start 12:04:48.898 -> Display Image End 12:04:49.026 -> Convert Image Start 12:04:49.026 -> Convert Image End 12:04:49.026 -> Display Image Start 12:04:49.058 -> Display Image End 12:04:49.155 -> Convert Image Start 12:04:49.155 -> Convert Image End 12:04:49.155 -> Display Image Start 12:04:49.187 -> Display Image End 12:04:49.283 -> Convert Image Start 12:04:49.283 -> Convert Image End 12:04:49.283 -> Display Image Start 12:04:49.315 -> Display Image End

ライセンス

Adafruit_ILI9341 ORIGINAL LIBRARY HEADER: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvStartvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv This is our library for the Adafruit ILI9341 Breakout and Shield ----> http://www.adafruit.com/products/1651 Check out the links above for our tutorials and wiring diagrams These displays use SPI to communicate, 4 or 5 pins are required to interface (RST is optional) Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit! Written by Limor Fried/Ladyada for Adafruit Industries. MIT license, all text above must be included in any redistribution ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^End^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Adafruit_GFX ORIGINAL LIBRARY LICENSE: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvStartvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv Software License Agreement (BSD License) Copyright (c) 2012 Adafruit Industries. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^End^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TFT_eSPI ORIGINAL LIBRARY LICENSE: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvStartvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv Software License Agreement (FreeBSD License) Copyright (c) 2020 Bodmer (https://github.com/Bodmer) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^End^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ LovyanGFX ORIGINAL LIBRARY LICENSE: vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvStartvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv Software License Agreement (FreeBSD License) Copyright (c) 2020 lovyan03 (https://github.com/lovyan03) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of the FreeBSD Project. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^End^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1
1
fumiのアイコン画像
電子工作が好きなのではなく、電子工作を夢想するのが好きなのだと最近気づきました。
  • fumi さんが 2024/01/02 に 編集 をしました。 (メッセージ: 初版)
ログインしてコメントを投稿する