码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Python+opencv实图片定位


    # -*- coding: utf-8 -*-
    import cv2
    
    
    file=r'D:/Setting.png'    #大图
    temp=r'D:/Battery.png'   #小图
    
    # 弹出图片后 CTRL+S保存图片到本地
    
    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)
    # 需要最小平方差
    mn,_,mnLoc,_ = cv2.minMaxLoc(result)
    
    # 开始画矩形:
    # Extract the coordinates of our best match
    MPx,MPy = mnLoc #获得最小坐标的
    print(MPx, MPy)
    
    # MPx1,MPy1 = mxLoc #获得最大坐标的
    # 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]  #获得图片的宽度
    
    # 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)
    
    
    #方法不同,获得的坐标不同
    # print(MPx, MPy)
    # print(MPx1, MPy1)
    # 注意看一下两种方法有一个结果是相同的
    #cv2.TM_SQDIFF_NORMED
    # 结果:
    # 96 179
    # 70 1122
    
    # cv2.TM_CCOEFF_NORMED
    # 结果:
    # 72 1631
    # 96 179

    如下图所示,在背景图片中找到的  搜索的 图标,并用红线圈出来 

  • 相关阅读:
    uboot命令
    Volatile和CAS
    【牛客 - SQL篇 - SQL大厂面试真题】SQL162 2021年11月每天的人均浏览文章时长
    WinUI(WASDK)项目实践——优雅的开发上位机应用(新)
    Linux上git的基本操作
    python教程:11种字典常见操作
    Spark提交参数说明和常见优化
    学物联网有前途吗?
    申请专利必须把技术公开吗?
    nginx部署web项目(跟着搞不出来,来砍我)
  • 原文地址:https://blog.csdn.net/qq_30273575/article/details/125447380
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号