Image Processing

The Image Processing library connects the GR-KAEDE camera option board, acquires images from the CMOS camera OV7740, generates JPEG files, and detects people and motion. To use it, describe #include <image.h>.

Image()

Description
Constructor for the image processing library.
Syntax
Image image
Parameters
None
Returns
None

begin

Description
Perform camera module and SDRAM initialization.
Syntax
image.begin()
Parameters
None
Returns
None
Note
Even if you create multiple instances, you can call it once and it's OK.

captureStart

Description
Take a photo. Check with isCaptureFinished to determine if the capture from the camera is complete.
Syntax
image.captureStart()
Parameters
None
Returns
None

isCaptureFinished

Description
Returns whether a capture from the camera has ended.
Syntax
image.isCaptureFinished()
Parameters
None
Returns
True: Capture has ended. False: Capturing.

createRgb565

Description
Generates 320 x 240 size RGB565 data from the captured image.
Syntax
uint8_t* image.createRgb565()
Parameters
None
Returns
Data storage pointer.

createGrayImage

Description
Generates a gray image from data in the 320 x 240 size RGB565 format. Gray images are required for human and motion detection.
Syntax
uint8_t* image.createGrayImage()
Parameters
None
Returns
Data storage pointer.

createJpg

Description
A file is generated by compressing data of 320 x 240 size RGB565 format into JPEG format.
Syntax
uint8_t* image.createJpg()
Parameters
None
Returns
Data storage pointer.

getCreatedRgb565

Description
Acquires the generated data storage destination of the RGB565 format.
Syntax
uint8_t* image.getCreatedRgb565()
Parameters
None
Returns
Data storage pointer.

getCreatedGrayImage

Description
Acquires the data storage destination of the generated gray image.
Syntax
uint8_t* image.getCreatedGrayImage()
Parameters
None
Returns
Data storage pointer.

getCreatedJpg

Description
Acquires the data storage destination of the generated JPEG image.
Syntax
uint8_t* image.getCreatedJpg()
Parameters
None
Returns
Data storage pointer.

getCreatedJpgSize

Description
Gets the size of the generated JPEG image.
Syntax
int32_t image.getCreatedJpgSize()
Parameters
None
Returns
Data size.

personDetection

Description
Perform processing for human detection. You need to generate a gray image in advance.
Syntax
bool image.personDetection()
Parameters
None
Returns
True: Success, False: Failure
Note
When this processing is performed, the detection result is reflected on the RGB565 image.

getPersonNumber

Description
As the result of personDetection, returns the number of persons detected in the area.
Syntax
bool image.getPersonNumber(area)
Parameters
area: Areas 1 to 9 shown below. If 0 is specified, all areas (default).
1 2 3
4 5 6
7 8 9
Returns
The number of people in the specified area
Note
The maximum number of people in one area is five people. The detection distance is optimized at 4m.

movingDetection

Description
Performs processing for motion detection. It is necessary to generate three gray images in advance.
Syntax
bool image.movingDetection(uint8_t* before_gray_image1, uint8_t* before_gray_image2)
Parameters
before_gray_image1: Two previous gray images
before_gray_image2: The previous gray image.
Returns
True: Success, False: Failure
Note
When this processing is performed, the result of motion detection is reflected in the RGB565 image.

getMovingNumber

Description
Returns the number of moving objects detected as a result of movingDetection.
Syntax
bool image.getMovingNumber()
Parameters
None
Returns
The number of moving objects.

getMovingArea

Description
Returns the moving object detection area as a result of movingDetection.
Syntax
bool image.getMovingArea(moving)
Parameters
moving: Specify the motion number. 0 is the object with the largest movement. The numbers will be in descending order.
Returns
The detection area is shown below.
1 2 3
4 5 6
7 8 9

Sample Program

This sample detects motion and saves it to an SD card.


/*
 This sample enables a photo to be taken that includes moving detection. 
 After taking a photo, the photo will be compressed to a JPEG file and 
 then saved to an SD card. 
 Hardware: GR-KAEDE, camera option and SD. Also USB cable and PC.
 Note: This sample needs to install a USB driver for GR-KAEDE
       The USB driver is in the project folder you created.
       Also you need to install a terminal software like Teraterm.
*/
#include <Arduino.h>
#include "Image.h"
#include "SD.h"
#include "RTC.h"
 
Image image[3];
RTC_TIMETYPE t;
void dateTime(uint16_t* date, uint16_t* time);
 
void setup(){
    Serial.begin(9600);
    while(!Serial.available()); // wait to press key
    Serial.read(); //dummy
    Serial.println("start");
 
    if(!SD.begin()){
        Serial.println("Failed to access SD.");
    } else {
        Serial.println("Success to access SD.");
    }
 
    // RTC for time stamp in SD
    rtc_init();
    t.year = 15;
    t.mon = 9;
    t.day = 2;
    t.weekday = RTC_WEEK_WEDNESDAY;
    t.hour = 17;
    t.min = 0;
    t.second = 0;
    rtc_set_time(&t);
 
    SdFile::dateTimeCallback( &dateTime );
 
    // Initialize Image library
    image[0].begin();
 
}
 
void loop(){
 
    static int cnt = 0;
 
    while(cnt < 2){ //necessary at least two image in the past.
        image[cnt].captureStart();
        while(!image[cnt].isCaptureFinished());
        image[cnt].createGrayImage();
        cnt++;
    }
     
    Serial.println("Capturing image");
    image[cnt%3].captureStart();
    while(!image[cnt%3].isCaptureFinished());
    image[cnt%3].createGrayImage();
     
    // Moving detection
    if( 0 == image[cnt%3].movingDetection(image[(cnt-2)%3].getCreatedGrayImage(), image[(cnt-1)%3].getCreatedGrayImage())){
        Serial.println("finish detection");
        Serial.print("Moving Number is:");
        Serial.println(image[cnt%3].getMovingNumber());
        Serial.print("The biggest moving is in area:");
        Serial.println(image[cnt%3].getMovingArea(0));
    }
    image[cnt%3].createJpg();
 
    Serial.println("Write jpg to SD:");
 
    char fn[20];
    sprintf (fn, "test%d.jpg", (cnt-2));
    File file = SD.open(fn, FILE_WRITE);
    uint8_t* adr = image[cnt%3].getCreatedJpg();
    int32_t size = image[cnt%3].getCreatedJpgSize();
    for (int i = 0; i < size; i++){
        file.write(*adr);
        adr++;
    }
    file.close();
    Serial.println("Done.");
    cnt++;
}
 
void dateTime(uint16_t* date, uint16_t* time)
{
  rtc_get_time(&t);
 
  *date = FAT_DATE(t.year+2000, t.mon, t.day);
  *time = FAT_TIME(t.hour, t.min, t.second);
}