Sipeed M1 MicroPython: Converting an image to grayscale

In this tutorial we will learn how to convert an image to grayscale using a Sipeed M1 based board. We are going to obtain the original image from a camera and display the resulting grayscale version in a LCD.

Introduction

In this tutorial we will learn how to convert an image to grayscale using a Sipeed M1 based board. We are going to obtain the original image from a camera and display the resulting grayscale version in a LCD.

I’ll be using a Sipeed M1 dock suit board, which is sold with a OV2640 camera and a 2.4 inches LCD. The board also contains the connectors for both these devices, making it much easier to get started.

We will be using MicroPython to program the board. At the time of writing, the Sipeed M1 dock suit board comes flashed with MicroPython. Nonetheless, if you need to do it manually, please check this guide.

Note that the Sipeed M1 module is based on the powerful Kendryte K210, a SoC containing a dual core RISC-V processor and a Neural Network processing unit, amongst many other features.

The code

As usual, we start our code by importing the modules we need to develop our application. We will need the sensor module, to be able to get images from the camera, and the lcd module, so we can output the results to the LCD.

import sensor
import lcd

After this we will initialize the LCD with a call to the init function from the lcd module.

lcd.init()

We will also take care of the camera initialization with a call to the reset function from the sensor module.

sensor.reset()

Then, before we obtain images from the camera, we will set the pixel format and the frame size. We will use the RGB565 pixel format and the QVGA frame size.

sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)

To start the image capture, we call the run function from the sensor module, passing as argument the value 1.

sensor.run(1)

Then we can obtain a picture from the camera by calling the snapshot function from the sensor module. This function call will return an object of class Image.

To convert the obtained image to grayscale, we need to call the to_grayscale method on our Image object.

This method takes an optional Boolean argument indicating if the original image can be modified (false) or if a copy should be created to apply the modification (true). This parameter defaults to false, which means that if we don’t specify anything the original image will be modified.

In our case, since we are just going to display the resulting grayscale image on the LCD, we don’t need to preserve the original image and thus we don’t need to pass any parameter to this function.

As output, an object of class Image will be returned. We can pass it directly to the display function of the lcd module, which will show the grayscale image.

lcd.display(sensor.snapshot().to_grayscale())

Naturally, you can repeat this last command to obtain and display more images in the LCD. The final code can be seen below.

import sensor
import lcd

lcd.init()

sensor.reset()

sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)

sensor.run(1)
 
lcd.display(sensor.snapshot().to_grayscale())

Testing the code

To test the code, simply run it in your device, assuming that you already have the LCD and camera connected to the board.

You can use a serial tool of your choice to connect to the device and have access to the MicroPython prompt. In my case I’ll be using uPyCraft, a MicroPython IDE.

After running the code, you should obtain a result similar to figure 1. As can be seen, the grayscale image is displayed on the LCD.

Displaying the grayscale version of the image captured from the camera.
Figure 1 – Displaying the grayscale image on the LCD.

Leave a Reply

%d bloggers like this: