Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获取一个图像在另一个图像中的位置_Node.js_Image_Image Processing_Imagemagick_Frontend - Fatal编程技术网

Node.js 获取一个图像在另一个图像中的位置

Node.js 获取一个图像在另一个图像中的位置,node.js,image,image-processing,imagemagick,frontend,Node.js,Image,Image Processing,Imagemagick,Frontend,我有一个显示网站的浏览器截图。 现在我想找出网站(视口)的位置(相对于整个屏幕截图)。如图中黑色边框的矩形所示: 在开始图像处理之前,我有可能在网站的DOM中添加任何内容 我已经尝试生成二维码,将其添加到视口的左上角和右下角,然后使用imagemagick确定二维码在较大图像中的位置: compare-度量“rmse”-子图像搜索-相异度阈值“0.1”-虚拟像素“edge”“haystack.png”“needle.png”“results.png” 然而,这需要很长时间。事实上,我在40分

我有一个显示网站的浏览器截图。 现在我想找出网站(视口)的位置(相对于整个屏幕截图)。如图中黑色边框的矩形所示:

在开始图像处理之前,我有可能在网站的DOM中添加任何内容

我已经尝试生成二维码,将其添加到视口的左上角和右下角,然后使用imagemagick确定二维码在较大图像中的位置:

compare-度量“rmse”-子图像搜索-相异度阈值“0.1”-虚拟像素“edge”“haystack.png”“needle.png”“results.png”
然而,这需要很长时间。事实上,我在40分钟后就退出了

我使用了二维码,因为通过使用时间戳,我可以非常确定这张图片不会在网站上的任何其他地方找到

另外,截图中二维码的大小是原始二维码的两倍,但我想这是因为我的mac屏幕有144dpi

我使用的是node.js,所以我需要一些可以通过命令行执行的东西(比如imagemagick),这样我就可以从节点或直接节点模块执行它


我有一个优势,我可以选择什么图像,我想在更大的图像搜索。我想知道什么是被发现的是一个有用的信息来加速这个过程(但是我不知道如何使用这些信息)。

< P>我有一些建议,如果你发现子图像搜索太慢,你可以考虑加快搜索速度。p> 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
并发现大小如何影响搜索时间,如下所示:

#!/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
正如你所看到的,图像的大小有很大的不同

以下是三个大小不等的干草堆,每个干草堆包含两个白色“针”:

以下是ImageMagick认为“针”所在的“结果”图像:

2。尽快停止

如果添加参数
-similarity threshold
并将其设置为合理的值,则可以在找到第一个良好匹配项后立即停止搜索,如
grep-m 1

这样设置将使其在第一次完美匹配时停止(相似性差为零):

或者这样设置会使它在第一次“非常好的匹配”时停止

默认设置为
1.0
,该设置从不匹配,从而导致搜索在整个图像上继续

现在我知道您想要查找视口的顶部和底部,这是两个匹配项,而且快速查找第一个匹配项似乎没有用。但是孔子,他说“旋转你的形象”:-)

因此,找到您的第一个(即顶部)匹配,然后将图像(和针)旋转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

我有一些建议,如果你发现子图像搜索速度太慢,你可能会考虑加快搜索速度。p> 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
并发现大小如何影响搜索时间,如下所示:

#!/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
正如你所看到的,图像的大小有很大的不同

以下是三个大小不等的干草堆,每个干草堆包含两个白色“针”:

以下是ImageMagick认为“针”所在的“结果”图像:

2。尽快停止

如果添加参数
-similarity threshold
并将其设置为合理的值,则可以在找到第一个良好匹配项后立即停止搜索,如
grep-m 1

这样设置将使其在第一次完美匹配时停止(相似性差为零):

或者这样设置会使它在第一次“非常好的匹配”时停止

默认设置为
1.0
,该设置从不匹配,从而导致搜索在整个图像上继续

现在我知道您想要查找视口的顶部和底部,这是两个匹配项,而且快速查找第一个匹配项似乎没有用。但是孔子,他说“旋转你的形象”:-)

因此,找到您的第一个(即顶部)匹配,然后将图像(和针)旋转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
我真的不懂