如何裁剪两张图片的差异?

How to crop the difference of two images?

我想拍这两张照片:

并且基本上产生这个:

我已经使用 comparefuzz 来定义已更改的部分。是否可以获取该区域的边界框并裁剪第二帧?

我会按照这些思路做一些事情:

convert a.jpg b.jpg -colorspace gray -blur 0x2 \
       -compose difference -composite          \
       -threshold 20% out.jpg

转换为灰度并模糊一点以隐藏小差异,然后计算差异和阈值以生成如下所示的二值化图像:

然后我会进行 连通分量分析 以找到图像中最大的对象,如下所示:

convert a.jpg b.jpg -colorspace gray -blur 0x2     \
   -compose difference -composite -threshold 20%   \
   -define connected-components:verbose=true       \
   -define connected-components:area-threshold=100 \
   -connected-components 8 out.jpg

Objects (id: bounding-box centroid area mean-color):
  0: 1029x1079+0+0 515.0,538.4 1102870 srgb(0,0,0)
  17: 76x147+326+564 366.5,641.4 5827 srgb(252,252,252)
  22: 18x50+358+612 365.1,635.3 568 srgb(0,0,0)
  11: 34x31+810+345 825.5,361.1 317 srgb(255,255,255)
  16: 57x97+25+539 52.3,587.2 286 srgb(255,255,255)
  14: 46x65+120+414 144.0,444.3 203 srgb(255,255,255)
  18: 27x49+23+579 36.9,601.0 118 srgb(255,255,255)
  24: 16x8+703+641 710.6,644.5 102 srgb(255,255,255)

-define connected-components:verbose=true 导致将单个 blob 输出为文本以供解析。

-define connected-components:area-threshold=100 表示只输出面积大于 100 像素的斑点。

-connected-components 8表示允许将8-连接的像素视为属于同一对象。 8-connected 表示 North-East、South-East、South-West 和 North-East 除了正常的北、东、南和西邻居。默认情况下,ImageMagick 仅将 4 个连接的像素视为属于同一对象 - 它更快 ;-)

你的播放器是项目 id 17 - 第二行,你可以看到边界框,然后用

将其从原始文件中剪掉
convert b.jpg -crop 76x147+326+564 player.jpg

注意:您需要 ImageMagick 6.8.9-10 或更高版本才能进行连通分量分析。