Find blobs
视频教程4 - 颜色识别:https://singtown.com/learn/49993/
find_blobs function
Tracking balls is the most commonly used function of OpenMV. In Quickstart in 10 minutes, we can find blobs through the find_blobs function. Now we come to the details of the function.
image.find_blobs(thresholds, roi=Auto, x_stride=2, y_stride=1, invert=False, area_threshold=10, pixels_threshold=10, merge=False, margin=0, threshold_cb=None, merge_cb=None)
Here we have many parameters.
- Thresholds is the threshold value of colors. Note: The parameters is a list which can include many colors. If you just need one color, then there need no more than one color in the list. And if you need more than one threshold of color, then there need many thresholds in the list. Note: You can use code to judge the color of the blob. ``` red = (xxx,xxx,xxx,xxx,xxx,xxx) blue = (xxx,xxx,xxx,xxx,xxx,xxx) yellow = (xxx,xxx,xxx,xxx,xxx,xxx)
img=sensor.snapshot() red_blobs = img.find_blobs([red])
color_blobs = img.find_blobs([red,blue, yellow])
* ROI means region of interest, which we have introduced in[Use the statistics of the image](/image/statistics.md)
left_roi = \[0,0,160,240\]
blobs = img.find\_blobs\(\[red\],roi=left\_roi\)
* x_stride is the pixel with the minimum width of the blob in the x direction, of which the default is 2. If you want to search blobs of which the width is more than 10 pixels, then set the parameter as 10.
blobs = img.find\_blobs\(\[red\],x\_stride=10\)
* y_stride is the pixel with the minimum width of the blob in the y direction, of which the default is 1. If you want to search blobs of which the width is more than 5 pixels, then set the parameter as 5.
blobs = img.find\_blobs\(\[red\],y\_stride=5\)
* invert
Invert the threshold and search the color beyond the threshold as threshold.
* area_threshold
If the area outlined in the blob is less than the threshold, it would be filtered.
* pixels_threshold
A blob would be filtered if its pixels are less than the threshold.
* merge
If you set it as true, then all the overlapping pixels would be merged to be one. Note: This would merge blobs of all colors. And if you want to mix blobs of more than one color, you just need to employ the find_blobs of the threshold of those colors.
all_blobs = img.find_blobs([red,blue,yellow],merge=True)
red_blobs = img.find_blobs([red],merge=True) blue_blobs = img.find_blobs([blue],merge=True) yellow_blobs = img.find_blobs([yellow],merge=True)
* margin
If you set the margin as 1, then every two blobs of which the interval is one pixel would be merged.
## Threshold
The construction of the threshold of a color is like this:
red = (minL, maxL, minA, maxA, minB, maxB)
The data in the tuple in the minimum and maximum of the L A B.
If you want to acquire the threshold in the image of IDE, click here:[Quickstart in 10 minutes ](/quick-starter.md)
In the new vision of IDE, we provide a more convenient selection tool of threshold. Read the following.
## Threshold Editor
We add threshold selection tool into the IDE of OpenMV, which facilitate the debugging of the threshold of colors.
First, start the hello world.py, making the pattern displayed by frame butter in IDE.
![](/assets/05-05-001.jpg)
Then click tool → Machine Vision → Threshold Editor
![](/assets/05-05-002.jpg)
Click Frame Buffer to acquire the image in IDE. You can choose an image file by yourself.
![](/assets/05-05-003.jpg)
Drag the six sliding bloc, then you can observe the result of the threshold in real time. What we want to achieve is turning the color of the target into white and others black.
![](/assets/05-05-004.jpg)
##Blobs is a list
find\_blobs
What it return to is the list with more blobs. (Pay attention to the difference between blob and blobs, which are just names to distinguish one blob and more than one blobs.
Here list means the array similar with C language. A blobs list includes many objects and the objects are blobs. And each object provides the information of a blob.
blobs = img.find_blobs([red])
Blobs are many color lumps.
We can search all the blobs with the for circulation.
for blob in blobs: print(blob.cx()) ```
ReadThe background knowledge of Pythonif you want to know more about the usage of for circulation.
The object of blobs
There are many methods of blobs:
blob.rect() Return to the outline of the blob, the rectangular tuple(x, y, w, h), which can be directly used in image.draw_rectangle.
blob.x() Return to the x coordinate (int) of the outline of the blob, which can also be acquired through blob[0].
blob.y() Return to the y coordinate (int) of the outline of the blob, which can also be acquired through blob[1].
blob.w() Return to the width (int) of the outline of the blob, which can also be acquired through blob[2].
blob.h() Return to the height (int) of the outline of the blob, which can also be acquired through blob[3].
blob.pixels() Return to the amount of the pixels (int) of the outline of the blob, which can also be acquired through blob[4].
blob.cx() Return to the x coordinate (int) of the center of the outline of the blob, which can also be acquired through blob[5].
blob.cy() Return to the y coordinate (int) of the center of the outline of the blob, which can also be acquired through blob[6].
blob.rotation() Return to the rotation angle of the blob(Unit: radian). If the blob is similar with a pencil, then its value is 0~180°. If the blob is a circle, then the value is invalid. If the blob has no symmetry, then you will get 0~360°, which can also be acquired through blob[7].
blob.code() Return to a 16bit number and each bit would correspond with a threshold. For example:
blobs = img.find_blobs([red, blue, yellow], merge=True)
If the blob is red, then its code is 0001 while its code is 0010 if blue. Note: Blobs could be emerged. And if a blob is emerged from red and blue, its code should be 0011. This function can be used to search the code of colors, which can also be acquired through blob[8].
blob.count() If you set merge as True, then the blobs would be merged to be one. What the function return to is its amount. If you set merge as False, the returned value is always 1, which can be acquired through blob[9].
blob.area() Return to the area of the outline of the blob, which should be (w * h).
blob.density() Return to the density of the blob, which is the result of dividing the pixels of the blob by the area of the area of the outline. If its density is low, it means that the target is not locked that well. For example, to identify a circle, the blob.pixels() it returns to is the pixel of the circle and blob.area() is the area of the external square of the circle.