Precision Measurement with Opencv python

前端 未结 1 947
小鲜肉
小鲜肉 2021-01-02 09:38

I am actually working on a Machine Vision project using OpenCV and Python.

Objective : The objective of the project is to measure the dimensions of

相关标签:
1条回答
  • 2021-01-02 10:13

    Hough is designed for detecting, not for quantifying. If you want precise measures, you'll have to use a library designed for that. OpenCV is not meant for quantification, and consequently has poor capabilities there.

    A long time ago I wrote a paper about more precise estimates of size using the Radon transform (the Hough transform is one way of discretizing the Radon transform, it's fast for some cases, but not precise):

    • C.L. Luengo Hendriks, M. van Ginkel, P.W. Verbeek and L.J. van Vliet, The generalized Radon transform: sampling, accuracy and memory considerations, Pattern Recognition 38(12):2494–2505, 2005, doi:10.1016/j.patcog.2005.04.018. Here is a PDF.

    But because your setup is so well controlled, you don't really need all that to get a precise measure. Here is a very straight-forward Python script to quantify these holes:

    import PyDIP as dip
    import math
    
    # Load image and set pixel size
    img = dip.ImageReadTIFF('/home/cris/tmp/machined_piece.tif')
    img.SetPixelSize(dip.PixelSize(0.042 * dip.Units("mm")))
    
    # Extract object
    obj = ~dip.Threshold(dip.Gauss(img))[0]
    obj = dip.EdgeObjectsRemove(obj)
    
    # Remove noise
    obj = dip.Opening(dip.Closing(obj,9),9)
    
    # Measure object area
    lab = dip.Label(obj)
    msr = dip.MeasurementTool.Measure(lab,img,['Size'])
    objectArea = msr[1]['Size'][0]
    
    # Measure holes
    obj = dip.EdgeObjectsRemove(~obj)
    lab = dip.Label(obj)
    msr = dip.MeasurementTool.Measure(lab,img,['Size'])
    sz = msr['Size']
    holeAreas = []
    for ii in sz.Objects():
       holeAreas.append(sz[ii][0])
    
    # Add hole areas to main object area
    objectArea += sum(holeAreas)
    
    print('Object diameter = %f mm' % (2 * math.sqrt(objectArea / math.pi)))
    for a in holeAreas:
       print('Hole diameter = %f mm' % (2 * math.sqrt(a / math.pi)))
    

    This gives me the output:

    Object diameter = 57.947768 mm
    Hole diameter = 6.540086 mm
    Hole diameter = 6.695357 mm
    Hole diameter = 15.961935 mm
    Hole diameter = 6.511002 mm
    Hole diameter = 6.623011 mm
    

    Note that there are a lot of assumptions in the code above. There is also an issue with the camera not being centered exactly above the object, you can see the right side of the holes reflecting light. This will certainly add imprecision to these measurements. But note also that I didn't use the knowledge that the object is round when measuring it (only when converting area to diameter). It might be possible to use the roundness criterion to overcome some of the imaging imperfections.

    The code above uses PyDIP, a very rough Python interface to a C++ library, DIPlib 3. The Python syntax is a direct translation of the C++ syntax, and some things are still quite awkward for a Python person (actually it's easier to use in C++, I think!). But it is aimed specifically at quantification, and hence I recommend you try it out for your application.

    0 讨论(0)
提交回复
热议问题