center coordinates jumpy

Hi Folks,

Has anyone else noticed that when doing color blob tracking, the cx,cy coordinates tend to be rather jumpy even when the rectangle around the blob is very stable?

I am talking about this in the code:

# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
# Draw the color label. b[8] is the color label.
img.draw_string(b[0]+2, b[1]+2, "%d" % b[8])

the rect from b[0:4] stays very stable but the x,y crosshairs from img.draw_cross jumps around a lot.

Hi, the centroid is the average position of the center of all the pixels in the blob. So, if it’s jumping around a lot you should widen your color bounds.

OK, but then I get a lot of detection of other objects that are not the color I am looking for.

Would it be better for me if I base the center on the center of the rectangle coordinates instead of the pixels?

Sure, that’s fine. You can also use the pixels value to determine the quality of the lock. If the object size is fixed then you should roughly know how many pixels should be tracked.

Use the histogram in the GUI to get the exact color bounds needed. Note that you can select an area in the framebuffer to get the color values for that area.

Where the center cx,cy coordinates seem to really jump around is when using find_markers with merged_blobs and I have a color code of say, two colors.

The bounding box using draw_rectangle in this case stays dead on, though.

When using one color and just find_blobs, the cx,cy coordinates are much more stable.

That’s because the centroid in that case is the average of the two blobs.

Hmm… well, that kind of behavior is somewhat to be expected. All find markers does is merge overlapping blobs for you. If you’re merging a bunch of small blobs then the output will be jumpy.

Can you save a picture of what you’re tracking? You can right click on the frame buffer and press save.

You could try to check if delta x,y are within some threshold, if not move x,y. I’m assuming you’re tracking the blob with servos and small changes are causing servos to move a lot.
Example:

thresh_x = 5
thresh_y = 5

new_x = b[5]
new_x = b[6]
delta_x = abs(x - new_x)
delta_y = abs(y - new_y)

If (delta_x > thresh_x):
    x  = new_x

If (delta_y > thresh_y):
    y  = new_y

Edit:
You could also try blurring the image after snapshot, if you have the latest firmware you could use Gaussian:

img = sensor.snapshot()
img.gaussian(3) # or 5

If not the median or midpoint, check the image filters examples.

Note:
You can get really fast blurring if you just defoucs the lens :slight_smile: