This is OpenMv's own routine for single color recognition
Tracking the ball is the most used function of OpenMV
1 key explanation
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)
thresholds is the color threshold,
Note: this parameter is a list and can contain multiple colors. If you only need one color, you only need one color value in this list. If you want multiple color thresholds, the list needs multiple color thresholds.
Note: in the returned color block object blob, you can call the code method to determine what color block it 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 the "region of interest"** Which area of the picture do you want to identify
eg: the rectangle in the middle of the picture? The whole picture?
The format of roi is tupple of (x, y, w, h)
The four parameters are the (x,y) coordinates of the fixed point in the upper left corner, and the length and width
x: The X coordinate of the upper left corner in the ROI area
y: Y coordinate of upper left corner in ROI area
w: Width of ROI
If RIO is not set, it is to recognize the color of the whole image
left_roi = [0,0,160,240] blobs = img.find_blobs([red],roi=left_roi)
x_ Stripe is the pixel with the minimum width in the X direction of the color block to be searched. The default value is 2. If you only want to search color blocks with a width of more than 10 pixels, set this parameter to 10:
blobs = img.find_blobs([red],x_stride=10)
y_ Stripe is the pixel with the minimum width in the Y direction of the color block to be searched. The default value is 1. If you only want to search 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 and finds the color other than the threshold as the threshold
eg: the threshold setting in the routine is to find red
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds
If find_ If convert = true in blobs, you can find colors other than red
area_threshold area threshold. If the framed area of the color block is less than this value, it will be filtered out
Is the entire area of the frame (may contain the number of pixels of some other background)
pixels_threshold the number of pixels (area) of the color block. If the number of pixels of the color block is less than this value, it will be filtered out.
Refers to the color of all color blocks in the box, excluding others (such as the color of the background)
merge. If it is set to True, all overlapping blob s will be merged into one.
Note: this will merge all blobs, regardless of color. If you want to confuse multiple color blobs, you only need to call find with different color thresholds_ blobs.
Go through all the color blob s
for blob in blobs: print(blob.cx())
blob has several methods:
blob.rect() Returns the outer frame of this color block - Rectangular tuple(x, y, w, h)，Can be directly in image.draw_rectangle Used in. blob.x() Returns the outline of the color block x Coordinates( int)，You can also blobTo get it. blob.y() Returns the outline of the color block y Coordinates( int)，You can also blobTo get it. blob.w() Returns the width of the outer frame of the color block w(int)，You can also blobTo get it. blob.h() Returns the height of the outer frame of the color block h(int)，You can also blobTo get it. blob.pixels() Returns the number of pixels in the color block( int)，You can also blobTo get it. blob.cx() Returns the center of the outer frame of the color block x Coordinates( int)，You can also blobTo get it. blob.cy() Returns the center of the outer frame of the color block y Coordinates( int)，You can also blobTo get it. blob.rotation() Returns the rotation angle of the color block in radians( float). If the color block is similar to a pencil, this value is 0~180°. If the color block is a circle, this value is useless. If the color block has no symmetry at all, you will get 0~360°，You can also blobTo get it. blob.code() Returns a 16 bit Numbers, each bit Will correspond to each threshold. For example: blobs = img.find_blobs([red, blue, yellow], merge=True)
If the color block is red, its code is 0001. If it is blue, its code is 0010. Note: a blob may be merged. If it is red and blue, the blob is 0011. This function can be used to find the color code. It can also be obtained through blob.
blob.count() If merge=True，Then there will be more than one blob Merged into one blob，This function returns the number of this. If merge=False，Then the return value is always 1. You can also use blobTo get it. blob.area() Returns the area of the outer frame of the color block. It should be equal to(w * h) blob.density() Returns the density of the color block. This is equal to the number of pixels of the color block divided by the area of the outer frame. If the density is low, the target is not locked well. For example, identify a red circle and return blob.pixels()Is the number of pixels of the target circle, blob.area()Is the area of a circle circumscribed by a square.
2 source program
To avoid too many comments, see the previous section for the main find_blobs
# Single Color RGB565 Blob Tracking Example # # This example shows off single color RGB565 tracking using the OpenMV Cam. import sensor, image, time, math #Import the built-in module threshold_index = 0 # 0 for red, 1 for green, 2 for blue #Set the color threshold, three maximum values and three minimum values of LAB # Color Tracking Thresholds (L Min, L Max, A Min, A Max, B Min, B Max) # The below thresholds track in general red/green/blue things. You may wish to tune them... thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds (30, 100, -64, -8, -32, 32), # generic_green_thresholds (0, 30, 0, 64, -128, 0)] # generic_blue_thresholds sensor.reset() #Reset sensor, reset camera sensor.set_pixformat(sensor.RGB565) #Reset color format to RGB56 sensor.set_framesize(sensor.QVGA) #The size of the image is QVGA sensor.skip_frames(time = 2000) sensor.set_auto_gain(False) # Be sure to turn off white balance sensor.set_auto_whitebal(False) # Be sure to turn off automatic gain #Because opening will affect the effect of color recognition and change the color threshold clock = time.clock() # Only blobs that with more pixels than "pixel_threshold" and more area than "area_threshold" are # returned by "find_blobs" below. Change "pixels_threshold" and "area_threshold" if you change the # camera resolution. "merge=True" merges all overlapping blobs in the image. while(True): clock.tick() img = sensor.snapshot() #First intercept a picture of the photosensitive element #In the img.find_blobs function, color recognition is performed for blob in img.find_blobs([thresholds[threshold_index]], pixels_threshold=200, area_threshold=200, merge=True): #If the roi value is not set here, the whole image will be color recognized # These values depend on the blob not being circular - otherwise they will be shaky. if blob.elongation() > 0.5: img.draw_edges(blob.min_corners(), color=(255,0,0)) img.draw_line(blob.major_axis_line(), color=(0,255,0)) img.draw_line(blob.minor_axis_line(), color=(0,0,255)) # These values are stable all the time. img.draw_rectangle(blob.rect()) #If we recognize the color, we draw a box around the color block and circle it img.draw_cross(blob.cx(), blob.cy()) #Draw a cross in the middle, (blob.cx(), blob.cy()) is the center coordinate of the color block # Note - the blob rotation is unique to 0-180 only. img.draw_keypoints([(blob.cx(), blob.cy(), int(math.degrees(blob.rotation())))], size=20) #blob is the parameter returned by find_blobs print(clock.fps())