How to subtract two images using python opencv2 to get the foreground object

opencv, python

Solution

If the background in the two images are exactly the same, you can subtract them as you mention in your post.

image1 = imread("/path/to/image1")
image2 = imread("/path/to/image2")
image3 = image1 - image2

Problem

Is there any way to subtract two images in python opencv2 ? - Image 1 : Any image (eg. a house Image) (static image) - Image 2 : The same Image with an Object (In house, a person is standing...) (static image + dynamic objects) - Image 3 = Image 2 - Image 1 If we subtract `Image2` from `Image1` means `Image3` should give `Object(person)` only.

Original source