- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一张显示网站的浏览器屏幕截图。现在我想找出网站(视口(viewport))的位置(相对于整个屏幕截图)。如下图中所示,带有黑色边框的矩形:
在开始图像处理之前,我可以向网站的 DOM 添加任何内容。
我已经尝试生成一个 QR 码,将其添加到视口(viewport)的左上角和右下角,然后使用 imagemagick 来确定 QR 码在更大的位置图片:
compare -metric "rmse" -subimage-search -dissimilarity-threshold "0.1" -virtual-pixel "edge" "haystack.png" "needle.png" "results.png"
但是,这需要很长时间。事实上,40分钟后我就退出了。
我使用了二维码,因为通过使用时间戳,我可以非常确定这张图片不会在网站上的其他任何地方找到。
此外,屏幕截图中 QR 码的大小是原始 QR 码大小的两倍,但我猜这是因为我的 Mac 屏幕具有 144dpi。
我正在使用node.js,所以我需要可以通过命令行执行的东西(例如imagemagick),以便我可以从 Node 或a执行它直接 Node 模块。
我的优点是我可以在更大的图像中选择我想要搜索的图像。我想准确了解要找到的内容可能是加快进程的有用信息(但我还不知道如何使用这些信息)。
最佳答案
如果您发现子图像搜索太慢,我有一些建议您可以考虑加快搜索速度。
<强>1。缩小图像尺寸
我进行了一个小实验来测试在不同大小的干草堆中搜索不同大小的针,如下所示:
#!/bin/bash
# Create a range of haystack sizes
for h in 200 400 800; do
# And a range of needle sizes
for n in 10 20 40; do
# Create haystack to search in, containing two needles
convert -size ${h}x${h}! gradient:red-black -fill white \
-draw "rectangle 100,100 139,139" \
-draw "rectangle 150,150 189,189" \
haystack.png
# Create a needle this size to search for
convert -size ${n}x${n}! xc:white needle.png
cp haystack.png haystack_${h}x${h}.png
cp needle.png needle${n}x${n}.png
# Search, measuring the time
start=$SECONDS
compare -dissimilarity-threshold 1.0 -metric rmse -subimage-search haystack.png needle.png null: > /dev/null 2>&1
end=$SECONDS
((elapsed=end-start))
echo Haystack:${h}x${h}, needle:${n}x${n}, time:$elapsed
done
done
并发现大小如何影响搜索时间,如下所示:
Haystack:200x200, needle:10x10, time:2
Haystack:200x200, needle:20x20, time:2
Haystack:200x200, needle:40x40, time:2
Haystack:400x400, needle:10x10, time:8
Haystack:400x400, needle:20x20, time:8
Haystack:400x400, needle:40x40, time:10
Haystack:800x800, needle:10x10, time:33
Haystack:800x800, needle:20x20, time:36
Haystack:800x800, needle:40x40, time:47
如您所见,图像的大小有很大差异。
这是三个大小不同的干草堆,每个干草堆包含 2 个白色的“针”:
以下是 ImageMagick 认为“针”所在位置的“结果”图像:
<强>2。尽快停止
如果添加参数-similarity-threshold
并将其设置为合理的值,则可以在找到第一个良好匹配后立即停止搜索 - 就像grep -m 1
.
这样设置将使其停止在第一个完美匹配处(相似度为零差异):
-similarity-threshold 0.0
或者像这样设置将使它停止在第一个“非常好的匹配”
-similarity-threshold 0.05
默认设置为 1.0
,它永远不会匹配,从而导致搜索继续遍及整个图像。
现在我知道您想要找到视口(viewport)的顶部和底部,这是两个匹配项,并且看起来快速仅找到第一个匹配项是不行的使用。但是孔子,他说“旋转你的形象”:-)
因此,找到您的第一个(即顶部)匹配项,然后将图像(和针)旋转 180 度并再次搜索,但这次您是从底部搜索,并且可以再次停在第一个匹配项处。 (也旋转你的结果。)
<强>3。使用您付费购买的所有可爱核心 - 并行化!
您可以将图像分割成多个部分并并行搜索,以利用您花费巨资购买的所有可爱的英特尔内核。你需要小心一点,重叠一点,这样你的针就不会跨越你切割的边界,但你所需要的只是在搜索区域添加一条与你的针宽度相同的条子......就像这样
#!/bin/bash
# Create a range of haystack sizes
for h in 200 400 800; do
# And a range of needle sizes
for n in 10 20 40; do
# Create haystack to search in, containing two needles
convert -size ${h}x${h}! gradient:red-black -fill white \
-draw "rectangle 100,100 139,139" \
-draw "rectangle 150,150 189,189" \
haystack.png
# Create a needle this size to search for
convert -size ${n}x${n}! xc:white needle.png
cp haystack.png haystack_${h}x${h}.png
cp needle.png needle${n}x${n}.png
# Search, measuring the time
start=$SECONDS
compare -dissimilarity-threshold 1.0 -metric rmse -subimage-search haystack.png needle.png null: > /dev/null 2>&1
end=$SECONDS
((elapsed=end-start))
echo Haystack:${h}x${h}, needle:${n}x${n}, time:$elapsed
((a=h/2))
((b=h/2))
((c=a+n))
((d=b+n))
((e=a-n))
((f=b-n))
# Measure time for parallel search, including dividing up image
start=$SECONDS
convert haystack.png -crop ${c}x${d}+0+0 +repage h1.png
convert haystack.png -crop ${a}x${b}+${a}+0 +repage h2.png
convert haystack.png -crop ${a}x${b}+0+${b} +repage h3.png
convert haystack.png -crop ${c}x${d}+${e}+${f} +repage h4.png
for p in 1 2 3 4; do
compare -dissimilarity-threshold 1.0 -metric rmse -subimage-search h${p}.png needle.png null: > /dev/null 2>&1 &
done
wait
end=$SECONDS
((elapsed=end-start))
echo Parallel Haystack:${h}x${h}, needle:${n}x${n}, time:$elapsed
done
done
您可以看到并行时间比单线程时间几乎加快了 4 倍:
Haystack:200x200, needle:10x10, time:2
Parallel Haystack:200x200, needle:10x10, time:0
Haystack:200x200, needle:20x20, time:2
Parallel Haystack:200x200, needle:20x20, time:1
Haystack:200x200, needle:40x40, time:2
Parallel Haystack:200x200, needle:40x40, time:1
Haystack:400x400, needle:10x10, time:8
Parallel Haystack:400x400, needle:10x10, time:2
Haystack:400x400, needle:20x20, time:8
Parallel Haystack:400x400, needle:20x20, time:3
Haystack:400x400, needle:40x40, time:10
Parallel Haystack:400x400, needle:40x40, time:4
Haystack:800x800, needle:10x10, time:33
Parallel Haystack:800x800, needle:10x10, time:10
Haystack:800x800, needle:20x20, time:36
Parallel Haystack:800x800, needle:20x20, time:11
Haystack:800x800, needle:40x40, time:47
Parallel Haystack:800x800, needle:40x40, time:14
关于node.js - 获取一张图像在另一张图像中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29883798/
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
我的可执行 jar 中有一个模板文件 (.xls)。不需要在运行时我需要为这个文件创建 100 多个副本(稍后将唯一地附加)。用于获取 jar 文件中的资源 (template.xls)。我正在使用
我在查看网站的模型代码时对原型(prototype)有疑问。我知道这对 Javascript 中的继承很有用。 在这个例子中... define([], function () { "use
影响我性能的前三项操作是: 获取滚动条 获取偏移高度 Ext.getStyle 为了解释我的应用程序中发生了什么:我有一个网格,其中有一列在每个单元格中呈现网格。当我几乎对网格的内容做任何事情时,它运
我正在使用以下函数来获取 URL 参数。 function gup(name, url) { name = name.replace(/[\[]/, '\\\[').replace(/[\]]/,
我最近一直在使用 sysctl 来做很多事情,现在我使用 HW_MACHINE_ARCH 变量。我正在使用以下代码。请注意,当我尝试获取其他变量 HW_MACHINE 时,此代码可以完美运行。我还认为
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
由于使用 main-bower-files 作为使用 Gulp 的编译任务的一部分,我无法使用 node_modules 中的 webpack 来require 模块code> dir 因为我会弄乱当
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 Gridlayout 在一行中放置 4 个元素。首先,我有一个 JPanel,一切正常。对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改。现在我的 JPanel 上添加了一个 J
由于以下原因,我想将 VolumeId 的值保存在变量中: #!/usr/bin/env python import boto3 import json import argparse import
我正在将 MSAL 版本 1.x 更新为 MSAL-browser 的 Angular 。所以我正在尝试从版本 1.x 迁移到 2.X.I 能够成功替换代码并且工作正常。但是我遇到了 acquireT
我知道有很多关于此的问题,例如 Getting daily averages with pandas和 How get monthly mean in pandas using groupby但我遇到
This is the query string that I am receiving in URL. Output url: /demo/analysis/test?startDate=Sat+
我正在尝试使用 javascript 中的以下代码访问 Geoserver 层 var gkvrtWmsSource =new ol.source.ImageWMS({ u
API 需要一个包含授权代码的 header 。这就是我到目前为止所拥有的: var fullUrl = 'https://api.ecobee.com/1/thermostat?json=\{"s
如何获取文件中的最后一个字符,如果是某个字符,则删除它而不将整个文件加载到内存中? 这就是我目前所拥有的。 using (var fileStream = new FileStream("file.t
我是这个社区的新手,想出了我的第一个问题。 我正在使用 JSP,我成功地创建了 JSP-Sites,它正在使用jsp:setParameter 和 jsp:getParameter 具有单个字符串。
在回答 StoreStore reordering happens when compiling C++ for x86 @Peter Cordes 写过 For Acquire/Release se
我有一个函数,我们将其命名为 X1,它返回变量 Y。该函数在操作 .on("focusout", X1) 中使用。如何获取变量Y?执行.on后X1的结果? 最佳答案 您可以更改 Y 的范围以使其位于函
我是一名优秀的程序员,十分优秀!