使用opencv和python进行激光曲线检测 [英] laser curved line detection using opencv and python

查看:1181
本文介绍了使用opencv和python进行激光曲线检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经取出了这张图的激光曲线:

I have taken out the laser curve of this image :


(来源: hostingpics.net )


(source: 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']))))

我也尝试过cv2.fitLine(),但这也不令人满意. 如何有效检测该曲线中间的点?问候.

I also tried cv2.fitLine(), but it wasn't satisfactory either. 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:

  • 使用边缘检测器:根据阈值,有时边缘可能无法合理地代表曲线
  • 大步采样图像

请尝试以下操作.

# 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)

我得到这样的曲线:

您还可以使用距离变换,然后为各个轮廓的每一列获取与最大距离值关联的行.

You can also use distance transform and then get the row associated with max distance value for each column of individual contours.

这篇关于使用opencv和python进行激光曲线检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆