編集履歴一覧に戻る
eMoi-picのアイコン画像

eMoi-pic が 2024年05月30日16時03分35秒 に編集

コメント無し

本文の変更

-

# RGB2次元配列に格納する関数

+

# RGBやYUVの2次元配列で画像処理

EMOIC-PIC用にSpresense-Arduinoスケッチを開発する際のサンプルコードです。 - camInit(); //カメラを初期化する。 - camCap128(Y[128][128], U[128][128], V[128][128], CamImage); //128*128YUV422画像を2次元配列に格納する。 - camCapRGB128(R[128][128], G[128][128], B[128][128], CamImage); //128*128RGB565画像を2次元配列に格納する。 - camJPG(filename); //画像を1枚撮影し、1024*1024のJPG画像でSDカードに格納する。 - camTake128(filename, Y_2次元配列, U_2次元配列, V_2次元配列); //画像を1枚撮影し、フルサイズでJPG画像でSDカードに格納する。128*128画像に縮小し、2次元配列に格納する。 関数はヘッダファイルに記述しており、書籍「SPRESENSEではじめるローパワーエッジAI」サポートページのサンプルコードをベースに作成しています。 https://github.com/TE-YoshinoriOota/Spresense-LowPower-EdgeAI/tree/main/Chap07/sketches ### displayUtil.ino 液晶表示サンプル ``` /* * displayUtil.ino - Graphic procedure for image_collection.ino * Copyright 2022 Sony Semiconductor Solutions Corporation * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ // ディスプレイの縦横の大きさ #define DISPLAY_WIDTH (320) #define DISPLAY_HEIGHT (240) // 記録した学習データ用画像を表示する位置 #define GRAY_OFFSET_X (290) #define GRAY_OFFSET_Y (100) // 液晶ディスプレイの下部に文字列を表示する void putStringOnLcd(String str, int color) { int len = str.length(); display.fillRect(0,224, 320, 240, ILI9341_BLACK); display.setTextSize(2); int sx = 160 - len/2*12; if (sx < 0) sx = 0; display.setCursor(sx, 225); display.setTextColor(color); display.println(str); } // 液晶ディスプレイにLINE_THICKNESSの太さの四角形を描画する void drawBox(uint16_t* imgBuf, int offset_x, int offset_y, int width, int height, int thickness, int color) { /* Draw target line */ for (int x = offset_x; x < offset_x+width; ++x) { for (int n = 0; n < thickness; ++n) { *(imgBuf + DISPLAY_WIDTH*(offset_y+n) + x) = color; *(imgBuf + DISPLAY_WIDTH*(offset_y+height-1-n) + x) = color; } } for (int y = offset_y; y < offset_y+height; ++y) { for (int n = 0; n < thickness; ++n) { *(imgBuf + DISPLAY_WIDTH*y + offset_x+n) = color; *(imgBuf + DISPLAY_WIDTH*y + offset_x + width-1-n) = color; } } } // 生成した学習データ用画像を表示する void drawGrayImg(uint16_t* imgBuf, uint8_t* grayImg) { int j = 0; for (int y = GRAY_OFFSET_Y; y < GRAY_OFFSET_Y + DNN_HEIGHT; ++y, ++j) { int i = 0; for (int x = GRAY_OFFSET_X; x < GRAY_OFFSET_X + DNN_WIDTH; ++x, ++i) { uint16_t gray8 = grayImg[j*DNN_WIDTH + i]; uint16_t gray16 = ((gray8 & 0xf8) << 8) | ((gray8 & 0xfc) << 3) | ((gray8 & 0xf8) >> 3); *(imgBuf + DISPLAY_WIDTH*y + x) = gray16; } } drawBox(imgBuf, GRAY_OFFSET_X, GRAY_OFFSET_Y, DNN_WIDTH, DNN_HEIGHT, 3, ILI9341_GREEN); } ``` ### ImageProcess_for_PBL.ino 画像処理開発用 ``` #include <Camera.h> #include <Adafruit_ILI9341.h> #include <SDHCI.h> #include <BmpImage.h> #include "PBL_library.h" #define TFT_DC 9 #define TFT_CS 10 Adafruit_ILI9341 display = Adafruit_ILI9341(TFT_CS, TFT_DC); #define DNN_HEIGHT (28) #define DNN_WIDTH (28) uint8_t Y[RESIZED_LENGTH][RESIZED_LENGTH]; uint8_t U[RESIZED_LENGTH][RESIZED_LENGTH]; uint8_t V[RESIZED_LENGTH][RESIZED_LENGTH]; BmpImage bmp; char fname[16]; int take_picture_count = 0; int test; void CamCB(CamImage img) { putStringOnLcd("save jpeg", ILI9341_GREEN); sprintf(fname, "PICT%03d.JPG", take_picture_count); if(take_picture_count++<=10){ test = camTake128(fname, Y,U,V,img); for (int n = 0; n < RESIZED_LENGTH*RESIZED_LENGTH; ++n) { if(n%RESIZED_LENGTH==0 && (n/RESIZED_LENGTH)%32 == 0){ Serial.print(Y[(n/RESIZED_LENGTH)][n%RESIZED_LENGTH]); Serial.print(','); Serial.print(U[(n/RESIZED_LENGTH)][n%RESIZED_LENGTH]); Serial.print(','); Serial.println(V[(n/RESIZED_LENGTH)][n%RESIZED_LENGTH]); } } Serial.println(); } test = camCapRGB128(Y,U,V,img); // RGB565の表示 for (int n = 0; n < RESIZED_LENGTH*RESIZED_LENGTH; ++n) { if(n%RESIZED_LENGTH==0 && (n/RESIZED_LENGTH)%32 == 0){ Serial.print(Y[(n/RESIZED_LENGTH)][n%RESIZED_LENGTH]); Serial.print(','); Serial.print(U[(n/RESIZED_LENGTH)][n%RESIZED_LENGTH]); Serial.print(','); Serial.println(V[(n/RESIZED_LENGTH)][n%RESIZED_LENGTH]); } } Serial.println(); img.convertPixFormat(CAM_IMAGE_PIX_FMT_RGB565); display.drawRGBBitmap(0, 0, (uint16_t *)img.getImgBuff(), LENGTH, LENGTH); } void setup() { Serial.begin(115200); display.begin(); display.setRotation(3); if(!camInit()){ putStringOnLcd("Failed to initialize camera", ILI9341_RED); } if (theCamera.startStreaming(true, CamCB) != CAM_ERR_SUCCESS){ putStringOnLcd("Failed to stream camera", ILI9341_RED); } } void loop() { } ``` ### PBL_library.h  関連ライブラリ ``` #include <Camera.h> #include <SDHCI.h> #include <stdio.h> #include <Adafruit_ILI9341.h> #define LENGTH (128) #define RESIZED_LENGTH (128) SDClass SD; bool camInit(){ while (!SD.begin()) { Serial.println("Insert SD card"); } CamErr err; err = theCamera.begin(1,CAM_VIDEO_FPS_30,LENGTH,LENGTH,CAM_IMAGE_PIX_FMT_YUV422,7); if (err != CAM_ERR_SUCCESS) { return false; } Serial.println("Set Auto white balance parameter"); err = theCamera.setAutoWhiteBalanceMode(CAM_WHITE_BALANCE_DAYLIGHT); if (err != CAM_ERR_SUCCESS) { return false; } Serial.println("Set still picture format"); err = theCamera.setStillPictureImageFormat(1024,1024,CAM_IMAGE_PIX_FMT_JPG); if (err != CAM_ERR_SUCCESS) { return false; } return true; } // YUV422を出力 bool camCap128(uint8_t Y[RESIZED_LENGTH][RESIZED_LENGTH], uint8_t U[RESIZED_LENGTH][RESIZED_LENGTH], uint8_t V[RESIZED_LENGTH][RESIZED_LENGTH], CamImage img){ CamErr err = img.convertPixFormat(CAM_IMAGE_PIX_FMT_YUV422); uint16_t* imgbuf = (uint16_t*)img.getImgBuff(); for (int n = 0; n < RESIZED_LENGTH*RESIZED_LENGTH; ++n) { Y[(int)(n/RESIZED_LENGTH)][n%RESIZED_LENGTH] = (uint8_t)(((imgbuf[n] & 0xf000) >> 8) | ((imgbuf[n] & 0x00f0) >> 4)); U[(int)(n/RESIZED_LENGTH)][n%RESIZED_LENGTH] = (uint8_t)((imgbuf[n] & 0x0f00) >> 8); V[(int)(n/RESIZED_LENGTH)][n%RESIZED_LENGTH] = (uint8_t)(imgbuf[n] & 0x000f); } return true; } // RGB565を出力 bool camCapRGB128(uint8_t R[RESIZED_LENGTH][RESIZED_LENGTH], uint8_t G[RESIZED_LENGTH][RESIZED_LENGTH], uint8_t B[RESIZED_LENGTH][RESIZED_LENGTH], CamImage img){ CamErr err = img.convertPixFormat(CAM_IMAGE_PIX_FMT_RGB565); uint16_t* imgbuf = (uint16_t*)img.getImgBuff(); for (int n = 0; n < RESIZED_LENGTH*RESIZED_LENGTH; ++n) { R[(int)(n/RESIZED_LENGTH)][n%RESIZED_LENGTH] = (uint8_t)((imgbuf[n] & 0xf800) >> 11); G[(int)(n/RESIZED_LENGTH)][n%RESIZED_LENGTH] = (uint8_t)((imgbuf[n] & 0x07e0) >> 5 ); B[(int)(n/RESIZED_LENGTH)][n%RESIZED_LENGTH] = (uint8_t)(imgbuf[n] & 0x001f); } return true; } bool camJPG(char filename[16]){ CamImage img = theCamera.takePicture(); SD.remove(filename); File myFile = SD.open(filename, FILE_WRITE); myFile.write(img.getImgBuff(), img.getImgSize()); myFile.close(); return true; } bool camTake128(char filename[16], uint8_t Y[128][128], uint8_t U[128][128], uint8_t V[128][128],CamImage img){ if(!camCap128(Y,U,V,img)){ return false; }; if(!camJPG(filename)){ return false; }; return true; } ```