Filament diameter measurement - dynamic threshold & calibration help

My current thresholds:

dark_th = [(0, 70)] # catches black filament
white_th = [(170, 255)] # catches white filament

Gray filament (70-170) is not detected! Lighting changes also break it.

Question: How to create ONE dynamic threshold that works for ALL filament colors and lighting conditions?

Problem 2: Diameter Calculation

I calibrate with:

  • 1.50mm → 100 pixels

  • 1.75mm → 115 pixels

  • 3.00mm → 200 pixels

My linear interpolation:

def calc_diameter(px):
if px <= px2: # 115 pixels
ratio = (115-100)/(1.75-1.50)
return ((px-100)/ratio) + 1.50
else:
ratio = (200-115)/(3.00-1.75)
return ((px-115)/ratio) + 1.75

But results are inaccurate, especially for 3.00mm.

Question: What’s the correct mathematical relationship between pixels and actual diameter? Linear? Quadratic? Pixels-per-mm ratio?

Hardware:

  • OpenMV Cam (VGA, Grayscale)

  • Fixed distance, manual exposure

What I need:

  1. Code for dynamic threshold that works for any filament color

  2. Correct calibration formula for 3-point measurement

  3. Simple ROI tracking (filament can move slightly)

Thanks!

Hi, I have no idea what you are looking at. Please post a picture of the different setups and what the camera sees.





Fotoğrafta kalibre edilmiş filamanın çapı milimetre cinsinden gösteriliyor, ancak gerçek çap 2,62 mm değil, 1,50 mm’dir.




How can I solve these problems? I want to dynamically measure diameters according to the lighting condition s.


The last image should show 3.00mm, but ROI is measuring incorrectly .

Oh, I see.

The issue you have is over exposure, you can reduce the camera exposure with sensor.set_auto_expousure() to reduce the image brightness. You can then use get_stats() on the image to determine the image brightness and then reduce the exposure dynamically until it’s ready for use with your measurement algorithm.