Reputation: 915
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:
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.
template:
Result:
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
Reputation: 1978
Let's break the problem into 3 parts:
1) Highlighting the line style (template) in particular color:
Steps:
2) Finding the start and end points:
Steps:
3) Finding the appropriate label of the detected line style:
Final Result:
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