简体   繁体   English

如何使用 OpenCV-Python 顺利检测焊接接头?

[英]How can I smoothly detect the welding joint using OpenCV-Python?

I have tried to detect the welding joint (bead) using the codes attached below in the last part of this question.我已尝试使用此问题最后一部分中附加的代码来检测焊接接头(焊道)。 I aim to draw a contour on the joint as shown in the third image but my results are poor and don't look similar to the expected results.我的目标是在关节上绘制轮廓,如第三张图片所示,但我的结果很差,并且看起来与预期结果不相似。 here is the summary of what I did but the codes are well clear: 1. Reading the image .jpg format 2. Image blurring 3. Thresholding 4. Morphological operations 5. Creating mask 6. Finding contour这是我所做的总结,但代码很清楚: 1. 读取图像.jpg格式 2. 图像模糊 3. 阈值 4. 形态学操作 5. 创建蒙版 6. 寻找轮廓

But the results are not promising, how can I get out from here但结果并不乐观,我怎么能从这里脱身

img_new = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur_image = cv2.bilateralFilter(img_new,5,21,21)
thresh = cv2.adaptiveThreshold(blur_image,255,
                               cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                               cv2.THRESH_BINARY_INV,15,3)
kernel = np.ones((5,5),dtype='uint8')
thresh_dilated = cv2.dilate(thresh, kernel, iterations = 1)
num_labels, labels, stats, centroids = cv2.connectedComponentsWithStats(thresh_dilated, connectivity=4)
sizes = stats[1:,-1]
min_size = 5000
num_labels = num_labels -1
img2 = np.zeros((labels.shape))
for i in range(0,num_labels):
    if sizes[i]>=min_size:
        img2[labels==i+1]=255
closing = cv2.morphologyEx(img2,cv2.MORPH_CLOSE,kernel)
opening = cv2.morphologyEx(closing,cv2.MORPH_OPEN,kernel)
result = opening.copy()
new_result = result.astype(dtype=np.uint8)

black = np.full((new_result.shape[0],new_result.shape[1],3),(0,0,0),np.uint8)
black1 = cv2.ellipse(black,(700,750),(300,140),0,0,360,(255,255,255),-1)
grayscale = cv2.cvtColor(black1,cv2.COLOR_BGR2GRAY)
ret,b_mask = cv2.threshold(grayscale,127,255,0)
img_result = cv2.bitwise_and(new_result,b_mask)

contours, hierarchy = cv2.findContours(img_result, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, contours, -1, (0,255,0), 2)
cv2.imshow('result',img)
cv2.waitKey(0)```

Read any papers ?阅读任何论文

If this for automatic processing of many welding joints on a assembly line?如果这适用于流水线上许多焊接接头的自动加工? Does this have to work for many other similiar images?这是否必须适用于许多其他类似的图像? It does not make sense to fine tune an algorithm for a single picture in that case.在这种情况下,为单张图片微调算法是没有意义的。

If yes, than you need to use more images to create the algorithm, maybe use a different light setup, maybe images taken with an IR camera right after the welding to get a "hot" mask.如果是,那么您需要使用更多图像来创建算法,也许使用不同的灯光设置,也许是在焊接后立即使用红外摄像机拍摄的图像以获得“热”面罩。 Or use light from left and right sight separately to combine two images for a single mark.或者分别使用左右视线的光线将两个图像组合为一个标记。

Another thing that would be very helpful is to get a "before" image from the parts.另一件非常有用的事情是从零件中获取“之前”图像。 without the welding joint.没有焊接接头。 In that case it could get easy.在这种情况下,它可能会变得容易。 You would just create the difference between the images, do some filtering to remove the welding beads and the reddish layer.您只需创建图像之间的差异,进行一些过滤以去除焊珠和微红色层。

Edit 1:编辑1:

Another thing I forgot to mention is please look at the RGB layers separately.我忘了提到的另一件事是请分别查看 RGB 层。 This is something that you should always try early on.这是您应该尽早尝试的事情。 Often there is something useful to see, eg in your case it could be that the blue layer might be interesting.通常有一些有用的东西可以看到,例如,在您的情况下,蓝色层可能很有趣。 Please add the layers to your question.请将图层添加到您的问题中。

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

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