zoukankan      html  css  js  c++  java
  • python+opencv模板定位图片

    # -*- coding: utf-8 -*-
    import cv2
    
    file1=r'E:aabb
    esize
    esize_image11.jpg'
    file2=r'E:aabb
    esize
    esize_image12.jpg'
    temp=r'E:aabb
    esize	emp.jpg'
    
    
    method = cv2.TM_SQDIFF_NORMED
    
    # Read the images from the file
    small_image = cv2.imread(temp)
    large_image = cv2.imread(file2)
    
    result = cv2.matchTemplate(small_image, large_image, method)
    # 需要最小平方差
    mn,_,mnLoc,_ = cv2.minMaxLoc(result)
    
    # 开始画矩形:
    # Extract the coordinates of our best match
    MPx,MPy = mnLoc
    print(MPx, MPy)
    
    # Step 2: Get the size of the template. This is the same size as the match.
    trows,tcols = small_image.shape[:2]
    
    # Step 3: Draw the rectangle on large_image
    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)
  • 相关阅读:
    117. Populating Next Right Pointers in Each Node II
    50. Pow(x, n)
    494. Target Sum
    Word Ladder
    HDU 4417
    POJ 2104
    POJ 3277
    【转】图论500题
    POJ 2991
    POJ 1436
  • 原文地址:https://www.cnblogs.com/yansc/p/14700387.html
Copyright © 2011-2022 走看看