简体   繁体   English

使用opencv和python进行激光曲线检测

[英]laser curved line detection using opencv and python

I have taken out the laser curve of this image : 我已经取出了这张图的激光曲线:

原始图像
(source: hostingpics.net ) (来源: hostingpics.net

曲线

And now, I'm trying to obtain a set of points (the more, the better), which are in the middle of this curve. 现在,我试图获得一组点(越多越好),这些点位于曲线的中间。 I have tried to split the image into vertical stripes, and then to detect the centroid. 我试图将图像分成垂直条纹,然后检测到质心。 But it doesn't calculate lots of points, and it's not satisfactory at all ! 但这并不能计算很多点,也不能令人满意!

img = cv2.Canny(img,50,150,apertureSize = 3)
sub = 100
step=int(img.shape[1]/sub)
centroid=[]
for i in range(sub):
    x0= i*step
    x1=(i+1)*step-1
    temp = img[:,x0:x1]
    hierarchy,contours,_ = cv2.findContours(temp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    if contours <> []:   
        for i in contours :     
            M = cv2.moments(i)
            if M['m00'] <> 0:
            centroid.append((x0+int(M['m10']/M['m00']),(int(M['m01']/M['m00']))))

I also tried cv2.fitLine() , but it wasn't satisfactory either. 我也尝试了cv2.fitLine() ,但也不令人满意。 How could I detect points in the middle of this curve efficiently ? 如何有效检测该曲线中间的点? regards. 问候。

I think you are getting fewer points because of the following two reasons: 我认为您得到的积分减少是因为以下两个原因:

  • using an edge detector: depending on the thresholds, sometimes the edges may not reasonably represent the curve 使用边缘检测器:根据阈值,有时边缘可能无法合理地代表曲线
  • sampling the image using a large step 使用较大的步骤对图像进行采样

Try the following instead. 请尝试以下方法。

# threshold the image using a threshold value 0
ret, bw = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY)
# find contours of the binarized image
contours, heirarchy = cv2.findContours(bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# curves
curves = np.zeros((img.shape[0], img.shape[1], 3), np.uint8) 

for i in range(len(contours)):
    # for each contour, draw the filled contour
    draw = np.zeros((img.shape[0], img.shape[1]), np.uint8) 
    cv2.drawContours(draw, contours, i, (255,255,255), -1)
    # for each column, calculate the centroid
    for col in range(draw.shape[1]):
        M = cv2.moments(draw[:, col])
        if M['m00'] != 0:
            x = col
            y = int(M['m01']/M['m00'])
            curves[y, x, :] = (0, 0, 255)

I get a curve like this: 我得到这样的曲线:

在此处输入图片说明

You can also use distance transform and then get the row associated with max distance value for each column of individual contours. 您还可以使用距离变换,然后为各个轮廓的每一列获取与最大距离值关联的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM