Programmer_nltk
Programmer_nltk

Reputation: 915

Line style Matching - Open CV?

I am new to the use of OpenCV. I intend to use it to match line styles in P&ID.

Line styles are something like this:

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

Novice here,How to approach it?

Outcome: The code should identify the start point and end point of line and name of it and highlight the line in particular color.

Effort so far:

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

Just using template matching tutorial to box the matching. But what I intend is trace the matching line pattern within the image in color.

Edit 2: enter image description here

template:

enter image description here

Result:

enter image description here

enter image description here

Expected outcome:

Ok the above is expected outcome: The green highlight shows major pipeline and blue highlight overlaps dotted future expansion line(assuming it is dotted). Now, How to approach this problem? Forget bounding box, a highlight would do!

Upvotes: 1

Views: 1187

Answers (1)

janu777
janu777

Reputation: 1978

Let's break the problem into 3 parts:

1) Highlighting the line style (template) in particular color:

Steps:

  • Find the location of template using template matching technique.
  • Create a mask with white pixels in the template area and rest black pixels
  • Highlight with a different line color in areas where the value drops below 255(assuming the line is in black color)
  • Using the template, Template
  • This is the outputHighlighting the line style

2) Finding the start and end points:

Steps:

  • Find the contour of the masked image containing just the line.
  • Find extreme points of the contour (leftmost and rightmost)
  • The points are shown Start and End points

3) Finding the appropriate label of the detected line style:

  • Create a dictionary with template_ids and their corresponding labels
  • With each template processed in the loop print out the labels

Final Result:

  • For 5 templates this is the result I got. You can do the same for rest of the templates Final Result
  • These are the output statements printed

Label: Connecting Line; Starting_point: (24, 352); Ending_point: (169, 354)

Label: Major Process; Starting_point: (18, 203); Ending_point: (163, 205)

Label: Hydraulic; Starting_point: (22, 557); Ending_point: (168, 558)

Label: Optical, Nuclear; Starting_point: (16, 64); Ending_point: (165, 69)

Label: Jacketed or Double Containment; Starting_point: (23, 434); Ending_point: (167, 436)

Upvotes: 1

Related Questions