MaixCAM MaixPy Find Blobs
Update history
Date | Version | Author | Update content |
---|---|---|---|
2024-04-03 | 1.0.0 | neucrack | Initial documentation |
2024-04-03 | 1.0.1 | lxowalle | Added detailed usage for finding blobs |
Before reading this article, make sure you know how to develop with MaixCAM. For details, please read Quick Start.
Introduction
This article will introduce how to use MaixPy to find color blobs and how to use the default application of MaixCam to find color blobs.
In vision applications, finding color blobs is a very common requirement, such as robots finding color blobs, automated production lines finding color blobs, etc., which requires identifying specific color areas in the image and obtaining information such as the position and size of these areas.
Using MaixPy to Find Blobs
The maix.image.Image
module in MaixPy provides the find_blobs
method, which can conveniently find color blobs.
How to Find Blobs
A simple example to find color blobs and draw bounding boxes:
from maix import image, camera, display
cam = camera.Camera(320, 240)
disp = display.Display()
# Select the corresponding configuration based on the color of the blob
thresholds = [[0, 80, 40, 80, 10, 80]] # red
# thresholds = [[0, 80, -120, -10, 0, 30]] # green
# thresholds = [[0, 80, 30, 100, -120, -60]] # blue
while 1:
img = cam.read()
blobs = img.find_blobs(thresholds, pixels_threshold=500)
for blob in blobs:
img.draw_rect(blob[0], blob[1], blob[2], blob[3], image.COLOR_GREEN)
disp.show(img)
Steps:
Import the image, camera, and display modules
from maix import image, camera, display
Initialize the camera and display
cam = camera.Camera(320, 240) # Initialize the camera with an output resolution of 320x240 in RGB format disp = display.Display()
Get the image from the camera and display it
while 1: img = cam.read() disp.show(img)
Call the
find_blobs
method to find color blobs in the camera image and draw them on the screenblobs = img.find_blobs(thresholds, pixels_threshold=500) for blob in blobs: img.draw_rect(blob[0], blob[1], blob[2], blob[3], image.COLOR_GREEN)
img
is the camera image obtained throughcam.read()
. When initialized withcam = camera.Camera(320, 240)
, theimg
object is an RGB image with a resolution of 320x240.img.find_blobs
is used to find color blobs.thresholds
is a list of color thresholds, where each element is a color threshold. Multiple thresholds can be passed in to find multiple colors simultaneously. Each color threshold is in the format[L_MIN, L_MAX, A_MIN, A_MAX, B_MIN, B_MAX]
, whereL
,A
, andB
are the three channels in the LAB color space. TheL
channel represents brightness, theA
channel represents the red-green component, and theB
channel represents the blue-yellow component.pixels_threshold
is a pixel count threshold used to filter out unwanted small blobs.img.draw_rect
is used to draw bounding boxes around the color blobs.blob[0]
,blob[1]
,blob[1]
, andblob[1]
represent the x-coordinate of the top-left corner of the blob, the y-coordinate of the top-left corner of the blob, the width of the blob, and the height of the blob, respectively.
Common Parameter Explanations
Here are explanations of commonly used parameters. If you cannot find parameters that can implement your application, you may need to consider using other algorithms or extending the required functionality based on the current algorithm's results.
Parameter | Description | Example |
---|---|---|
thresholds | Thresholds based on the LAB color space, thresholds=[[l_min, l_max, a_min, a_max, b_min, b_max]], representing: Brightness range [l_min, l_max] Green to red component range [a_min, a_max] Blue to yellow component range [b_min, b_max] Multiple thresholds can be set simultaneously |
Set two thresholds to detect red and greenimg.find_blobs(thresholds=[[0, 80, 40, 80, 10, 80], [0, 80, -120, -10, 0, 30]]) Red threshold is [0, 80, 40, 80, 10, 80] Green threshold is [0, 80, -120, -10, 0, 30] |
invert | Enable threshold inversion, when enabled, the passed thresholds are inverted. Default is False. | Enable threshold inversionimg.find_blobs(invert=True) |
roi | Set the rectangular region for the algorithm to compute, roi=[x, y, w, h], where x and y represent the coordinates of the top-left corner of the rectangle, and w and h represent the width and height of the rectangle, respectively. The default is the entire image. | Compute the region at (50, 50) with a width and height of 100img.find_blobs(roi=[50, 50, 100, 100]) |
area_threshold | Filter out blobs with a pixel area smaller than area_threshold, in units of pixels. The default is 10. This parameter can be used to filter out some useless small blobs. | Filter out blobs with an area smaller than 1000img.find_blobs(area_threshold=1000) |
pixels_threshold | Filter out blobs with fewer valid pixels than pixels_threshold. The default is 10. This parameter can be used to filter out some useless small blobs. | Filter out blobs with fewer than 1000 valid pixelsimg.find_blobs(pixels_threshold=1000) |
This article introduces commonly used methods. For more APIs, please see the image section of the API documentation.
Setting Thresholds Offline
To quickly verify the function of find blobs, you can first use the find blobs application provided by MaixCam to experience the effect of finding color blobs.
Demo
Turn on the device, select Find Blobs
application, then select the colour you want to identify, or customize the colour, then you can identify the corresponding colour, the setting bar
at the bottom will show the threshold range
, and the serial port will also output the coordinates and colour information of the identified coordinates.
Quick use
Using the default threshold
The find blobs app provides four configurations, red
, green
, blue
and user
, where red
, green
and blue
are used to find red
, green
and blue
colour blocks, and user
customized thresholds are saved when the app is exited, and the next time the app is opened the thresholds from the last debugging are loaded. For quick experience, you can switch to the corresponding configuration by clicking
the button
at the bottom of the interface, the app interface is referenced below:
Quick Debug Thresholds
Method of operation:
- Aim the
camera
at theobject
you need tofind
,click
on thetarget
on the screen, then theleft
side will show therectangle
of the corresponding colour of the object, and the LAB value of the object's colour. - Click on the rectangular box, the system will `automatically set' the LAB threshold, then the screen will draw the edge of the object.
The advantage of this method is that it is easy and quick to set the threshold and find the corresponding colour block. The disadvantage is that it is not precise enough, you can fine tuning it manually in the next step.
Manually fine tune the threshold
Method of operation:
Click
on theOptions icon
in the lower left corner to enter configuration modeAim the
camera
at theobject
you need tofind
,click
on thetarget object
on the screen, at this time theleft
side will show therectangular box
of the corresponding colour of the object, and display theLAB value
of the object's colour.Click on the lower option
L Min, L Max, A Min, A Max, B Min, B Max
, and a slider will appear on the right to set the value of this option. These values correspond to the minimum and maximum values of the L, A and B channels of the LAB colour format.Referring to the
LAB value
of the object colour calculated in step 2, adjustL Min, L Max, A Min, A Max, B Min, B Max
to the appropriate value to identify the corresponding colour block.For example,
LAB=(20, 50, 80)
, sinceL=20
, in order to fit a certain range, letL Min=10
,L Max=30
; similarly, sinceA=50
, letA Min=40
,A Max=60
; sinceB=80
, letB Min=70
,B Max=90
.
This method can be more precise to find the right threshold, with the Quick Debug Threshold
method, it is easy to find the desired threshold.
Get recognition results via serial protocol
The find blobs app supports reporting information about detected color blobs via the serial port (default baud rate is 115200).
Since only one report message is sent, we can illustrate the content of the report message with an example.
For instance, if the report message is:
shellCopy code
AA CA AC BB 14 00 00 00 E1 08 EE 00 37 00 15 01 F7 FF 4E 01 19 00 27 01 5A 00 A7 20
AA CA AC BB
: Protocol header, content is fixed14 00 00 00
: Data length, the total length excluding the protocol header and data lengthE1
: Flag, used to identify the serial message flag08
: Command type, for the find blobs app application, this value is fixed at 0x08EE 00 37 00 15 01 F7 FF 4E 01 19 00 27 01 5A 00
: Coordinates of the four vertices of the found color blob, with each value represented by 2 bytes in little-endian format.EE 00
and37 00
represent the first vertex coordinate as (238, 55),15 01
andF7 FF
represent the second vertex coordinate as (277, -9),4E 01
and19 00
represent the third vertex coordinate as (334, 25),27 01
and5A 00
represent the fourth vertex coordinate as (295, 90).A7 20
: CRC checksum value, used to verify if the frame data has errors during transmission.
About the LAB Color Space
The LAB color space, like the RGB color space, is a way to represent colors. LAB can represent all colors visible to the human eye. If you need to learn more about LAB, you can search for relevant articles online, which will provide more details. However, for you, it should be sufficient to understand why LAB is advantageous for MaixPy.
Advantages of LAB for MaixPy:
- The color gamut of the LAB color space is larger than that of RGB, so it can completely replace RGB.
- In the LAB color space, since the L channel is the brightness channel, we often set it to a relatively large range (commonly [0, 80]), and when coding, we mainly focus on the A and B channels. This can save a lot of time spent struggling with how to select color thresholds.
- The color perception in the LAB color space is more uniform and easier to debug with code. For example, if you only need to find red color blobs, you can fix the values of the L and B channels and only adjust the value of the A channel (in cases where high color accuracy is not required). For RGB channels, you generally need to adjust all three R, G, and B channels simultaneously to find suitable thresholds.