使用Opencv python进行精确测量

Abh*_*aji 6 python opencv measurement dimension computer-vision

我实际上正在使用OpenCV和Python开发机器视觉项目.

目标:该项目的目标是高精度地测量元件的尺寸.

主要硬件:

  • Basler 500万像素摄像头(aca-2500-14gm)

  • 红色背光(100毫米x 100毫米)(我的组件尺寸约为60毫米)

实验

由于我正在考虑非常严格的公差限制,我首先进行了精确研究.我将组件保留在背光源上并拍摄了100张图像而没有移动部件(想象像100帧的视频).我测量了所有100张图像的外径(OD).我的mm /像素比为0.042.我测量了测量的标准偏差,以找出精度,结果是0.03毫米左右,这是不好的.触摸组件和设置因此我期望精度为0.005 mm.但是我已经退了一个数量级了.我正在使用OpenCV的Hough圆来计算组件的OD.

码:

import sys
import pickle
import cv2
import matplotlib.pyplot as plt
import glob
import os
import numpy as np
import pandas as pd

def find_circles(image,dp=1.7,minDist=100,param1=50,param2=50,minRadius=0,maxRadius=0):
    """ finds the center of circular objects in image using hough circle transform

    Keyword arguments
    image -- uint8: numpy ndarray of a single image (no default).
    dp -- Inverse ratio of the accumulator resolution to the image resolution (default 1.7).
    minDist -- Minimum distance in pixel distance between the centers of the detected circles (default 100).
    param1 -- First method-specific parameter (default = 50).
    param2 -- Second method-specific parameter (default = 50).
    minRadius -- Minimum circle radius in pixel distance (default = 0).
    maxRadius -- Maximum circle radius in pixel distance (default = 0).

    Output
    center -- tuple: (x,y).
    radius -- int : radius.
    ERROR if circle is not detected. returns(-1) in this case    
    """

    circles=cv2.HoughCircles(image, 
                             cv2.HOUGH_GRADIENT, 
                             dp = dp, 
                             minDist = minDist, 
                             param1=param1, 
                             param2=param2, 
                             minRadius=minRadius, 
                             maxRadius=maxRadius)
    if circles is not None:
            circles = circles.reshape(circles.shape[1],circles.shape[2])
            return(circles)
    else:
        raise ValueError("ERROR!!!!!! circle not detected try tweaking the parameters or the min and max radius")

def find_od(image_path_list):
    image_path_list.sort()
    print(len(image_path_list))
    result_df = pd.DataFrame(columns=["component_name","measured_dia_pixels","center_in_pixels"])
    for i,name in enumerate(image_path_list):
        img = cv2.imread(name,0) # read the image in grayscale
        ret,thresh_img = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY_INV)
        thresh_img = cv2.bilateralFilter(thresh_img,5,91,91) #smoothing
        edges = cv2.Canny(thresh_img,100,200)
        circles = find_circles(edges,dp=1.7,minDist=100,param1=50,param2=30,minRadius=685,maxRadius=700)
        circles = np.squeeze(circles)
        result_df.loc[i] = os.path.basename(name),circles[2]*2,(circles[0],circles[1])
    result_df.sort_values("component_name",inplace=True)
    result_df.reset_index(drop=True,inplace=True)
    return(result_df)

df = find_od(glob.glob("./images/*"))
mean_d = df.measured_dia_pixels.mean()
std_deviation = np.sqrt(np.mean(np.square([abs(x-mean_d) for x in df.measured_dia_pixels])))

mm_per_pixel = 0.042
print(std_deviation * mm_per_pixel)
Run Code Online (Sandbox Code Playgroud)

输出:0.024

组件的图像:

在此输入图像描述

由于拍摄图像时不会干扰设置,我希望测量的重复性大约为0.005毫米(5微米)(对于100张图像).但事实并非如此.这是霍布圈的问题吗?或者我在这里失踪了什么

Cri*_*ngo 6

Hough 设计用于检测,而不是用于量化。如果您想要精确的度量,则必须使用为此设计的库。OpenCV 不是为了量化,因此在那里的能力很差。

很久以前,我写了一篇关于使用 Radon 变换更精确地估计尺寸的论文(Hough 变换是离散化 Radon 变换的一种方法,在某些情况下速度很快,但不精确):

但是因为您的设置得到了很好的控制,所以您实际上并不需要所有这些来获得精确的测量值。这是一个非常简单的 Python 脚本来量化这些漏洞:

import diplib as dip
import math

# Load image and set pixel size
img = dip.ImageRead('N6uqw.jpg')
img.SetPixelSize(0.042, "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)))
Run Code Online (Sandbox Code Playgroud)

这给了我输出:

import diplib as dip
import math

# Load image and set pixel size
img = dip.ImageRead('N6uqw.jpg')
img.SetPixelSize(0.042, "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)))
Run Code Online (Sandbox Code Playgroud)

请注意,上面的代码中有很多假设。还有一个问题是相机没有正好位于物体正上方,您可以看到孔的右侧反射光。这肯定会增加这些测量的不精确性。但还要注意,我在测量物体时没有使用物体是圆形的知识(仅在将面积转换为直径时)。或许可以使用圆度标准来克服一些成像缺陷。

上面的代码使用DIPlib,这是一个具有相当粗糙的 Python 接口的 C++ 库。Python语法是C++语法的直接翻译,有些东西还是挺别扭的,非Pythonic的。但它专门针对量化,因此我建议您在您的应用中试用它。