8.2 Color Recognition

Preparation for development

  • Complete the device link
  • Complete firmware burning
  • Complete software environment

Example code

import sensor
import image
import lcd
import time

from Maix import GPIO
from fpioa_manager import fm, board_info
from machine import UART

clock = time.clock()

fm.register(34, fm.fpioa.UART2_TX, force=True)
fm.register(35, fm.fpioa.UART2_RX, force=True)
uart_Port = UART(UART.UART2, 115200,8,0,0, timeout=1000, read_buf_len= 4096)

lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
lcd.rotation(2)

#blue,red,green,yellow

colour = ['blue','red','green','yellow']

colour_threshold =([27, 55, -18, 4, -39, -20],
                    [55, 64, 14, 52, -9, 3],
                    [29, 65, -109, -18, 36, 59],
                    [30, 68, -25, -10, 52, 87])
blobs = [0,0,0,0]

def blobs_output(blobs):
    for b in blobs:
        tmp=img.draw_rectangle(b[0:4])
        tmp=img.draw_cross(b[5], b[6])
        img.draw_string(b[5], b[6], colour[i],color=(255,0,0), scale=2)
        c=img.get_pixel(b[5], b[6])
        _colour = {}
        _colour['colour'] = colour[i]
        data_ = []
        data_.append(_colour)
        data_.append(blobs[0])
    uart_Port.write(str(blobs))

def show_fps():
    fps =clock.fps()
    img.draw_string(200, 1, ("%2.2ffps" %(fps)),color=(255,0,0), scale=2)

while True:
    clock.tick()
    img=sensor.snapshot()
    show_fps()

    for i in range(4):
        blobs[i] = img.find_blobs([colour_threshold[i]],area_threshold=100,pixels_threshold=500)
        if blobs[i]:
            blobs_output(blobs[i])

    lcd.display(img)

Functional Assignment


  • Identify four colors
  • The serial port outputs data
  • The display screen displays the recognized color blocks

Define the color you want to recognize

Open the MaixPy IDE and select Tool -- Machine Vision -- Curve Editor

Open source image location and select frame buffer.

Adjust the Lab tie down value mainly in the binary image field, and white pixel is the tracked pixel

MaixPy 机械视觉 API

Full Knowledge of Lab color space

  • Name Before we begin, let's clarify the name of the Lab Color Space: The full name of Lab is CIELAB, sometimes also written CIE Lab*. CIE stands for the International Commission on Illumination, an International authority on lighting, colour, etc..
  • Channel Lab is composed of a brightness channel and two color channels. In Lab color space, each color is represented by three numbers, L, a, b, and the meaning of each component is as follows: L stands for brightness a is the component from green to red b is the component from blue to yellow
  • Perceptual uniform Lab is designed based on people's perception of colors. More specifically, it is perceptual uniform. Perceptual uniform means that if the number (L, a, b) changes equally, then it brings about a similar degree of visual change. Lab is more consistent with human vision than RGB and CMYK color space, and is easier to adjust. If you want to adjust the brightness (regardless of the Helmholtz -- Kohlrausch effect, refer to the note below), adjust the L channel, and if you want to adjust only the color balance, adjust a and b respectively. Note: Helmholtz–Kohlrausch effect is an illusion in the human eyes -- that colors appear brighter when they are saturated.
  • Device-independent Lab has a nice feature -- Device-independent. That is, given a white point in the color space (the figure below represents a white spot in a color space), the color space can clearly determine how each color is created and displayed, regardless of the display medium used. For example, when you want to convert an RGB image on the screen to a CMYK image for printing, you can first convert it from RGB to Lab and then convert the Lab image to CMYK mode. Because gamut of Lab is larger than RGB and CMYK (The gamut of Lab is so large that a large part of it is beyond the range of human vision that it cannot be called "color"). It is important to note that Lab defines the color relative to the white point. We will not know the other colors until we define the color of the white point (e.g. it is defined as CIE Standard Illuminant D50).
  • Range of value In theory, L, a, and b are all real numbers, but in practice they are confined to a range of integers: The larger the L is, the higher the brightness is. When L is 0, it represents black, and when L is 100, it represents white. a and b are both gray when they are 0. When a goes from a negative number to a positive number, the corresponding color will change from green to red. When b goes from a negative number to a positive number, the corresponding color will change from blue to yellow. In practical application, we often use the range of color channel between -100~+100 or -128127.
  • Visualize Lab* has three components in total, so it can be presented in three dimensional space. In two-dimensional space, the Chromaticity Diagram is often used to visualize it, that is, to fix the brightness L, to see the change of a and b. Note that these visualizations are not accurate, it’s just helpful to understand.
  • CIELUV There is a color space similar to Cielab, called Cie 1976 (L, U, V), also known as Cieluv. The L of the color space is the same as the CIELAB, but the color component is different.
  • Conversion between LAB and RGB and CMYK Since RGB and CMYK are both device related, they cannot be directly converted to Lab. So before the conversion, you must define an absolute color space, such as sRGB or Adobe RGB. Conversion from RGB to SrGB is device independent, but the subsequent conversion are device independent.

results matching ""

    No results matching ""