How to crop the difference of two images?

拟墨画扇 提交于 2021-01-27 14:10:37

问题


I'd like to take these 2 images:

frame 1

frame 2

And essentially produce this:

result

I've gotten as far as using compare with fuzz to define the part that has changed. Is it possible to then get the bounding box of this area and crop the second frame?


回答1:


I would do something along these lines:

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

Convert to greyscale and blur a little to hide small differences, then calculate the difference and threshold to make a binarised image like this:

enter image description here

Then I would go for a Connected Components Analysis to find the biggest object in the image, like this:

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)

The -define connected-components:verbose=true causes the individual blobs to be output as text for parsing.

The -define connected-components:area-threshold=100 says to only output blobs larger than 100 pixels in area.

The -connected-components 8 says to allow pixels that are 8-connected to be considered as belonging to the same object. 8-connected means North-East, South-East, South-West and North-East neighbours in addition to the normal North, East, South and West. By default, ImageMagick only considers 4-connected pixels as belonging to the same object - it's faster ;-)

And your player is item id 17 - the second row and you can see the bounding box, and cut that out of the orignal with

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

enter image description here

Note: You will need ImageMagick 6.8.9-10 or better for Connected Components Analysis.



来源:https://stackoverflow.com/questions/28043317/how-to-crop-the-difference-of-two-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!