线型匹配 - 打开简历?

时间:2018-02-22 01:22:21

标签: python opencv

我是OpenCV的新手。我打算用它来匹配P& ID中的线条样式。

线条样式是这样的:

https://i0.wp.com/hardhatengineer.com/wp-content/uploads/2017/09/Pipeline-PID-Symbols.png?w=558&ssl=1

这里的新手,如何接近它?

结果:代码应标识行的起点和终点,并突出显示特定颜色的行。

到目前为止的努力:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('image.jpg',0)
img2 = img.copy()
template = cv2.imread('linepattern.jpg',0)
w, h = template.shape[::-1]

# All the 6 methods for comparison in a list
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
            'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']

for meth in methods:
    img = img2.copy()
    method = eval(meth)

    # Apply template Matching
    res = cv2.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

    cv2.rectangle(img,top_left, bottom_right, (0,0,255), 2)

    plt.subplot(121),plt.imshow(res,cmap = 'gray_r')
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img,cmap = 'gray_r')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)

    plt.show()

只需使用模板匹配教程来匹配匹配。但我想要的是跟踪图像中匹配的线条图案的颜色。

编辑2: enter image description here

模板:

enter image description here

结果:

enter image description here

enter image description here

预期结果:

好的以上是预期的结果:绿色突出显示主要管道和蓝色突出重叠点缀未来的扩展线(假设它是点缀的)。现在,如何解决这个问题?忘记边界框,一个亮点就行了!

1 个答案:

答案 0 :(得分:1)

让我们把问题分成三部分:

1)突出显示特定颜色的线条样式(模板):

步骤:

  • 使用模板匹配技术查找模板的位置。
  • 在模板区域中创建一个带有白色像素的遮罩,并留下黑色像素
  • 在值低于255的区域中突出显示不同的线条颜色(假设线条为黑色)
  • 使用模板, Template
  • 这是输出Highlighting the line style

2)查找起点和终点:

步骤:

  • 找到仅包含线条的蒙版图像的轮廓。
  • 找出轮廓的极点(最左边和最右边)
  • 点数显示为Start and End points

3)找到检测到的线条样式的相应标签:

  • 使用template_ids及其相应的标签创建字典
  • 在循环中处理每个模板,打印标签

最终结果:

  • 对于5个模板,这是我得到的结果。您可以对其余模板执行相同操作 Final Result
  • 这些是打印的输出语句
  

标签:连接线; Starting_point:(24,352); Ending_point:(169,   354)

     

标签:主要流程; Starting_point:(18,203); Ending_point:(163,205)

     

标签:液压; Starting_point:(22,557); Ending_point:(168,558)

     

标签:光学,核; Starting_point:(16,64); Ending_point:(165,69)

     

标签:夹套或双重遏制; Starting_point:(23,434); Ending_point:(167,436)