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

3
我想要处理这两张图片: frame 1 frame 2 最终得到的结果如下: result 我已经使用了comparefuzz来定义发生变化的部分。是否可以获取该区域的边界框并裁剪第二个帧?
1个回答

3
我会按照以下方式处理:

我会这样做:

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

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

enter image description here

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

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像素的blob。

-connected-components 8参数表示允许考虑与同一对象相连通的8个像素点。8个像素点包括标准的北、东、南、西4个相邻像素点,以及斜向相邻的4个像素点。默认情况下,ImageMagick仅考虑4个相邻像素点 - 这样做更快。

而您的玩家是物品id 17,位于第二行。您可以看到其边界框,并使用以下命令从原始图像中提取出该部分:

convert b.jpg -crop 76x147+326+564 player.jpg

输入图像描述

注意:进行连通组件分析需要使用ImageMagick 6.8.9-10或更高版本。


很高兴为你效劳!您可能需要稍微调整一下数字和技术,以优化其对其他图像(我看不到)的适应性,但这应该可以让您开始。 - Mark Setchell
+2 很棒的回答! :-) - Kurt Pfeifle
哇,我不知道ImageMagick可以做到OpenCV的所有功能啊 ;) - Thomas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接