Finding color blocks

Video tutorial 4 - Color recognition: https://singtown.com/learn/49993/

find_blobs function

Tracking the ball is the most used function of OpenMV. In 10-minute quick start,\ we can find the color block through the find_blobs function. Let's discuss the details of find_blobs.

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)

There are many parameters here.

  • thresholds is the color threshold. Note: This parameter is a list that can contain multiple colors. If you only need one color, then there is only one color value in this list. If you want multiple color thresholds, then this list needs multiple color thresholds. Note: The code method can be called on the returned color block object blob to determine what color the color block is.
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 is "region of interest". It has been introduced in the use of statistics.

    left_roi = [0,0,160,240]\ blobs = img.find_blobs([red],roi=left_roi)

  • x_stride is the minimum pixel width in the x direction of the color block to be searched. The default value is 2. If you only want to find color blocks with a width of more than 10 pixels, set this parameter to 10:

    blobs = img.find_blobs([red],x_stride=10)

  • y_stride is the minimum pixel width in the y direction of the color block to be searched. The default value is 1. If you only want to find color blocks with a width of more than 5 pixels, set this parameter to 5:

    blobs = img.find_blobs([red],y_stride=5)

  • invert inverts the threshold value and uses the color outside the threshold value as the threshold value for search

  • area_threshold area threshold value. If the area of the color block framed is smaller than this value, it will be filtered out

  • pixels_threshold Pixel count threshold, if the number of pixels in a color block is less than this value, it will be filtered out

  • merge merge, if set to True, merge all overlapping blobs into one.\ Note: This will merge all blobs, regardless of color. If you want to confuse blobs of multiple colors, just call find_blobs with different color thresholds separately.

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 margin, if set to 1, then two blobs will be merged if they are 1 pixel apart.

threshold

The structure of a color threshold is as follows:

red = (minL, maxL, minA, maxA, minB, maxB)

The values in the tuple are the maximum and minimum values of L A B respectively.

If you want to get this threshold in the image of the IDE, see: 10 minutes quick start

In the new version of the IDE, there is a more convenient threshold selection tool, see below.

Color threshold selection tool

OpenMV's IDE has added a threshold selection tool, which greatly facilitates the debugging of color thresholds.

First run hello world.py to display the pattern in the framebuffer in the IDE.\ \ Then open Tools → Mechine Vision → Threshold Editor\

Click Frame Buffer to get the image in the IDE, and Image File can select an image file by yourself.

Drag the six sliders to see the threshold results in real time. The result we want is to change our target color to white and all other colors to black.

Blobs is a list

The find_blobs object returns a list of multiple blobs. (Note the difference between blobs and blob. This is just a name used to distinguish multiple color blocks from one color block).\ A list is similar to an array in C language. A blobs list contains many blob objects. Blob objects are color blocks, and each blob object contains information about a color block.

blobs = img.find_blobs([red])

Blobs are many color blocks.

You can use a for loop to find all the color blocks.

for blob in blobs:
    print(blob.cx())

For the use of for loops, see Python background knowledge

Blob color block object

Blob has multiple methods:

  • blob.rect() returns the outer frame of this color block - a rectangular tuple (x, y, w, h), which can be used directly in image.draw_rectangle.

  • blob.x() returns the x-coordinate of the blob's border (int), also available from blob[0].

  • blob.y() returns the y-coordinate of the blob's border (int), also available from blob[1].

  • blob.w() returns the width w of the blob's border (int), also available from blob[2].

  • blob.h() returns the height h of the blob's border (int), also available from blob[3].

  • blob.pixels() returns the number of pixels in the blob (int), also available from blob[4].

  • blob.cx() returns the x-coordinate of the center of the blob's border (int), also available from blob[5].

  • blob.cy() returns the y-coordinate of the center of the blob's border (int), also available from blob[6].

  • blob.rotation() returns the rotation angle of the blob in radians (float). If the blob resembles a pencil, this value is between 0 and 180°. If the blob is a circle, this value is useless. If the blob has no symmetry at all, you will get 0~360°. You can also get this from blob[7].

  • blob.code() returns a 16-bit number, where each bit corresponds to a threshold. For example:

    blobs = img.find_blobs([red, blue, yellow], merge=True)

If the blob is red, its code is 0001, and if it is blue, its code is 0010. Note: a blob may be merged, so if it is a red and blue blob, the blob code is 0011. This function can be used to find color codes. You can also get this from blob[8].

  • blob.count() If merge=True, multiple blobs are merged into one blob, and this function returns the number of blobs. If merge=False, the return value is always 1. You can also get this from blob[9].

  • blob.area() returns the area of the blob's outer frame. It should be equal to (w * h)

  • blob.density() returns the density of the blob. This is equal to the number of pixels in the blob divided by the area of the outer frame. If the density is low, it means that the target is not locked very well.\ For example, to recognize a red circle, the returned blob.pixels() is the number of pixels of the target circle, and blob.area() is the area of the circumscribed square of the circle.

results matching ""

    No results matching ""