टेम्पलेट मिलान एक ऐसी तकनीक है, जिसके द्वारा वास्तविक छवि से पैच या टेम्पलेट का मिलान किया जा सकता है। यह मूल रूप से एक पैटर्न मिलान तंत्र है।
पायथन में ओपनसीवी मॉड्यूल है। ओपनसीवी का उपयोग करके, हम आसानी से मैच ढूंढ सकते हैं। तो इस समस्या में, OpenVC टेम्पलेट मिलान तकनीकों का उपयोग किया जाता है।
OpenCV कार्यक्षमता का उपयोग करने के लिए, हमें उन्हें pip . का उपयोग करके डाउनलोड करना होगा ।
sudo pip3 install opencv-python
टेम्प्लेट मिलान कार्य के लिए, एक सटीकता कारक होता है, इस कारक को थ्रेशोल्ड के रूप में जाना जाता है। एक उदाहरण के रूप में, हम कह सकते हैं कि हम इस टेम्पलेट मिलान समाधान का उपयोग करके आसानी से चेहरा पहचानने वाली योजना बना सकते हैं। हम आंखों या चेहरे के किसी अन्य हिस्से की कुछ छवियां प्रदान कर सकते हैं, फिर उन छवियों को टेम्पलेट के रूप में उपयोग करके, यह आसानी से मैच ढूंढ सकता है, लेकिन आंखों में विभिन्न भिन्नताएं हैं। इसलिए यदि हम सटीकता स्तर को 50% पर सेट करते हैं, तो यह सटीकता स्तर 100% से बेहतर पता लगाएगा। आम तौर पर, विभिन्न मामलों में सटीकता का स्तर 80% होता है।
टेम्पलेट्स से मिलान करने के चरण
-
वास्तविक छवि लें और इसे ग्रे स्केल छवि में परिवर्तित करें।
-
टेम्प्लेट को ग्रे स्केल इमेज के रूप में लें
-
टेम्प्लेट वास्तविक छवि पर स्लाइड करता है और वह स्थान ढूंढता है जहां सटीकता स्तर मेल खाता है।
-
जब परिणाम सटीकता के स्तर से अधिक हो, तो उस स्थिति को चिह्नित के रूप में चिह्नित करें।
पहले मामले के लिए इनपुट छवि और टेम्पलेट है -
मुख्य छवि
<केंद्र>
टेम्पलेट
<केंद्र>
उदाहरण कोड
import cv2
import numpy as np
#open the main image and convert it to gray scale image
main_image = cv2.imread('main_image.png')
gray_image = cv2.cvtColor(main_image, cv2.COLOR_BGR2GRAY)
#open the template as gray scale image
template = cv2.imread('template1.png', 0)
width, height = template.shape[::-1] #get the width and height
#match the template using cv2.matchTemplate
match = cv2.matchTemplate(gray_image, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
position = np.where(match >= threshold) #get the location of template in the image
for point in zip(*position[::-1]): #draw the rectangle around the matched template
cv2.rectangle(main_image, point, (point[0] + width, point[1] + height), (0, 204, 153), 0)
cv2.imshow('Template Found', main_image)
cv2.waitKey(0)
आउटपुट
<केंद्र>
उपरोक्त कोड मल्टी-स्केलिंग का समर्थन नहीं करता है। तो यदि टेम्पलेट का आकार समान नहीं है, तो यह पता नहीं लगाएगा। तो अगले भाग में, हम देखेंगे कि हम टेम्प्लेट का पता लगाने के लिए मल्टी-स्केलिंग फीचर का उपयोग कैसे कर सकते हैं।
इस दृष्टिकोण में, वास्तविक छवि को विभिन्न आकारों में परिवर्तित किया जाता है, हर बार यह पैटर्न से मेल खाता है, और मिलान का पता लगाने के लिए सबसे बड़ा सहसंबंध गुणांक ढूंढता है।
यहाँ वास्तविक छवि समान है, टेम्पलेट यहाँ है -
<केंद्र>
उदाहरण कोड
import imutils
import cv2
import numpy as np
#Open template and get canny
template = cv2.imread('template3.jpg')
template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
template = cv2.Canny(template, 10, 25)
(height, width) = template.shape[:2]
#open the main image and convert it to gray scale image
main_image = cv2.imread('main_image.png')
gray_image = cv2.cvtColor(main_image, cv2.COLOR_BGR2GRAY)
temp_found = None
for scale in np.linspace(0.2, 1.0, 20)[::-1]:
#resize the image and store the ratio
resized_img = imutils.resize(gray_image, width = int(gray_image.shape[1] * scale))
ratio = gray_image.shape[1] / float(resized_img.shape[1])
if resized_img.shape[0] < height or resized_img.shape[1] < width:
break
#Convert to edged image for checking
e = cv2.Canny(resized_img, 10, 25)
match = cv2.matchTemplate(e, template, cv2.TM_CCOEFF)
(_, val_max, _, loc_max) = cv2.minMaxLoc(match)
if temp_found is None or val_max>temp_found[0]:
temp_found = (val_max, loc_max, ratio)
#Get information from temp_found to compute x,y coordinate
(_, loc_max, r) = temp_found
(x_start, y_start) = (int(loc_max[0]), int(loc_max[1]))
(x_end, y_end) = (int((loc_max[0] + width)), int((loc_max[1] + height)))
#Draw rectangle around the template
cv2.rectangle(main_image, (x_start, y_start), (x_end, y_end), (153, 22, 0), 5)
cv2.imshow('Template Found', main_image)
cv2.waitKey(0)
आउटपुट
<केंद्र>