In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you how to use Python+OpenCV for image template matching (Match Template), the content is simple and easy to understand, organized clearly, I hope to help you solve doubts, let Xiaobian lead you to study and learn "how to use Python+OpenCV for image template matching (Match Template)" this article bar.
First import the required library files, numpy and cv2.
#import cv2 import numpy as np
Then load the original image and the image template to be searched. OpenCV processes the original image, creates a grayscale version, processes and finds matches in the grayscale image. Then restore and output the original image using the same coordinates.
#Load the original RGB image img_rgb = cv2.imread("photo.jpg") #Create a grayscale version of the original image, all operations processed in grayscale, then restore img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) using the same coordinates in the RGB image #Load image template to be searched template = cv2.imread ('face.jpg ',0) #Record image template size w, h = template.shape[::-1]
Here we output and view the original image, the grayscale version of the original image, and the image template separately.
#View three sets of images (image tag name, file name) cv2.imshow ('rgb ',img_rgb) cv2.imshow ('gray',img_gray) cv2.imshow ('template ',template) cv2.waitKey(0) cv2.destroyAllWindows()
Use matchTemplate to find and match the content in the image template in the original image and set thresholds.
#Match the original grayscale image to the image template using matchTemplate res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEF_NORMED) #Set threshold threshold = 0.7 #res greater than 70% loc = np.where( res >= threshold)
After matching, the original image is marked with the coordinates of the gray image.
#Mark the original RGB image with coordinates from the grayscale image for pt in zip(*loc[::-1]): cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (7,249,151), 2) #Display image cv2.imshow('Detected',img_rgb) cv2.waitKey(0) cv2.destroyAllWindows()
The following is the complete code:
def mathc_img(image,Target,value): import cv2 import numpy as np img_rgb = cv2.imread(image) img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY) template = cv2.imread(Target,0) w, h = template.shape[::-1] res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED) threshold = value loc = np.where( res >= threshold) for pt in zip(*loc[::-1]): cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (7,249,151), 2) cv2.imshow('Detected',img_rgb) cv2.waitKey(0) cv2.destrucyAllWindows ()image=("photo.jpg") Target=('face.jpg ') value=0.9 mathc_img(image,Target,value) Above is "How to use Python+OpenCV for image template matching (Match Template)" All the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.