惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

Lan小站-嗯,不错! - Python

从 pip 到 uv:一场 Python 包管理的「换引擎」革命 通过终端管理宝塔Python项目管理器里面的Python项目 - Lan小站-嗯,不错! requests优雅的重试 - Lan小站-嗯,不错! 某牛某客专栏文章爬虫 - Lan小站-嗯,不错! 解决Mac下ssl.SSLCertVerificationError:[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate(_ssl.c:1056) Debian11安装部署stable-diffusion-webui记录 - Lan小站-嗯,不错! 调用ChatGPT超过4096Token后自动截取保留指定长度的Token - Lan小站-嗯,不错! django怎么在迁移数据库的时候,自动生成数据 - Lan小站-嗯,不错! python datetime 东八区时间 - Lan小站-嗯,不错!
滑块验证图片匹配 - Lan小站-嗯,不错!
Lan · 2026-03-03 · via Lan小站-嗯,不错! - Python
    bg_w, bg_h, bg_ch, bg_rows = decode_png(bg_path)
    bk_w, bk_h, bk_ch, bk_rows = decode_png(block_path)
    if bg_ch != 3:
        raise ValueError("background.png 需要是 RGB 图")
    if bk_ch != 4:
        raise ValueError("block.png 需要是 RGBA 图")

    bg_gray = to_gray(bg_rows, bg_ch)
    bk_gray = to_gray(bk_rows, bk_ch)
    bk_mask = alpha_mask(bk_rows, bk_ch)
    tpl_gray, tpl_mask = crop_by_mask(bk_gray, bk_mask)

    bg_edges = edge_map(bg_gray)
    tpl_edges = edge_map(tpl_gray)

    x, y, score = find_best_position(bg_edges, tpl_edges, tpl_mask)

    print(f"背景图尺寸: {bg_w}x{bg_h}")
    print(f"滑块图尺寸: {bk_w}x{bk_h}")
    print(f"缺口左上角坐标: (, {y})")
    print(f"建议滑动距离(像素): ")
    print(f"匹配误差(越小越好): {score:.2f}")


if __name__ == "__main__":
    main()

核心原理

本质是:把滑块图当“模板”,在背景图里滑动对比,找到误差最小的位置。

参考实现都在
main.py

分步骤对应你看到的图片

  1. step_01 灰度化
    把 RGB 转灰度,减少颜色维度干扰,只保留亮度结构。
  2. step_02 Alpha 掩码
    block.png 的透明通道提取有效像素(非透明=1,透明=0),只比较有意义区域。
  3. step_03 背景边缘图
    对背景灰度图做简易梯度(和左/上像素差分),突出轮廓。
  4. step_04 模板边缘图
    对裁剪后的滑块模板做同样边缘处理,保证比较口径一致。
  5. 模板匹配(关键)
    在背景每个候选 (x,y) 上,按掩码点计算 SSE 误差:
    [
    score(x,y)=\frac{1}{N}\sum (bg\_edgey+py-tpl\_edgepy)^2
    ]
    score 最小的位置作为缺口位置。
  6. step_05 / step_06 可视化
    step_05 画匹配框;step_06 把滑块半透明叠加到匹配位置,验证直观效果。