如何使用python opencv2减去两个图像以获得前景对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21425992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:49:21  来源:igfitidea点击:

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

pythonopencv

提问by Thamizh

Is there any way to subtract two images in python opencv2 ?

有没有办法在 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
  • 图像 1 :任何图像(例如房屋图像)(静态图像)
  • Image 2 : 同一个Image with an Object(在房子里,一个人站着......)(静态图像+动态物体)
  • 图像 3 = 图像 2 - 图像 1

If we subtract Image2from Image1means Image3should give Object(person)only.

如果我们Image2Image1手段中减去Image3应该Object(person)只给。

采纳答案by Mohammad Moghimi

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

回答by dvigneshwer

Try background subtraction.

尝试背景减法

Use cv2.subtract(img1,img2)instead of arithmetic operation, as cv2 will take care of negative values.

使用cv2.subtract(img1,img2)代替算术运算,因为 cv2 将处理负值。

回答by Taha Anwar M-Holmes

cv2.subtract does not work it just binds the values between 0-255 so if you wanna get negative values just convert the image from unit8 to int32 or int64. Note unint8 could only take on the values of 0-255 thus it could not handle negative values

cv2.subtract 不起作用,它只是绑定 0-255 之间的值,所以如果你想得到负值,只需将图像从 unit8 转换为 int32 或 int64。注意 unint8 只能取 0-255 的值,因此它不能处理负值

image1= np.int32(image1)

image2= np.int32(image2)

image3 = image1 - image2

回答by RTbecard

@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.

@dvigneshwr 的答案做了一个减法,其中产生的负值四舍五入为 0。@Taha Anwar M-Holmes 的答案保留了负数,但改变了结果数组的数据类型,因此它不再是传统的图像类型。

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

对于那些想要根据绝对差异从背景图像中识别前景并返回与输入相同数据类型的数组(这就是我在这里结束的方式)的人,请使用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.

值得注意的是,OP 没有为此处减去的图像提供任何细节……取决于图像的内容,所有这些方法都可以回答 OP 的问题。