程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+opencv real image positioning

編輯:Python
# -*- coding: utf-8 -*-
import cv2
file=r'D:/Setting.png' # Big picture
temp=r'D:/Battery.png' # Little picture
# After the picture pops up CTRL+S Save pictures to local
method = cv2.TM_SQDIFF_NORMED
# method = cv2.TM_CCOEFF_NORMED
# Read the images from the file
small_image = cv2.imread(temp)
large_image = cv2.imread(file)
result = cv2.matchTemplate(small_image, large_image, method)
# The least square difference is required
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
# Start drawing rectangles :
# Extract the coordinates of our best match
MPx,MPy = mnLoc # To obtain the minimum coordinate 
print(MPx, MPy)
# MPx1,MPy1 = mxLoc # Get the maximum coordinates of
# print(MPx1, MPy1)
# Step 2: Get the size of the template. This is the same size as the match.
trows,tcols = small_image.shape[:2] # Get the width of the picture 
# Step 3: Draw the rectangle on large_image
# Circle the small picture in the big picture with a red line
cv2.rectangle(large_image, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2)
# cv2.rectangle(large_image, (MPx+169,MPy+76),(MPx+719,MPy+117),(0,0,255),2)
# Display the original image with the rectangle around the match.
cv2.imshow('output',large_image)
# The image is only displayed if we call this
cv2.waitKey(0)
# The method is different , Different coordinates obtained
# print(MPx, MPy)
# print(MPx1, MPy1)
# Notice that one of the results of the two methods is the same
#cv2.TM_SQDIFF_NORMED
# result :
# 96 179
# 70 1122
# cv2.TM_CCOEFF_NORMED
# result :
# 72 1631
# 96 179

As shown in the figure below , Found in the background picture   The search of Icon , And circle it with a red line  


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved