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

前端 未结 4 873
北海茫月
北海茫月 2021-02-19 18:48

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 Objec
4条回答
  •  -上瘾入骨i
    2021-02-19 19:23

    @dvigneshwr's answer does a subtraction where the resulting negative values are rounded up to 0. @Taha Anwar M-Holmes' answer preserves the negatives but changes the datatype of the resulting array so its no longer a conventional image type.

    For those wanting to identify a foreground from a background image based on the absolute difference in values and return an array of the same data type as the inputs (that's how I ended up here), use absdiff.

    Assuming the arrays are the same width and height...

    import cv2 as cv
    
    image3 = cv.absdiff(image1, image2)
    

    Its worth noting that the OP did not provide any details wrt to the images being subtracted here... depending on what the contents of the images are, all of these approaches may answer the OP's question.

提交回复
热议问题