有没有办法在Python OpenCV2中减去两个图像?
- 图像1:任何图像(例如房屋图像)(静态图像)
- 图像2:带有对象的相同图像(在房子里,一个人正在站立...)(静态图像+动态物体)
- 图像3 = 图像2-图像1
如果我们从Image1
中减去Image2
,那么Image3
应该仅给出Object(person)
。
尝试使用背景减除。
使用cv2.subtract(img1, img2)
代替算术运算,因为cv2将处理负值。
image1 = imread("/path/to/image1")
image2 = imread("/path/to/image2")
image3 = image1 - image2
@dvigneshwr的回答对负值进行减法运算,结果为负数时会被舍入为0。@Taha Anwar M-Holmes的回答保留了负数,但更改了结果数组的数据类型,因此不再是传统的图像类型。
对于那些想要基于值的绝对差异来识别前景和背景图像,并返回与输入相同数据类型的数组(这就是我来到这里的原因),请使用absdiff。
假设数组具有相同的宽度和高度...
import cv2 as cv
image3 = cv.absdiff(image1, image2)
cv2.subtract不起作用,它只是将值绑定在0-255之间,因此如果您想要获得负值,只需将图像从unit8转换为int32或int64即可。请注意,unint8只能取0-255的值,因此无法处理负值。
image1= np.int32(image1)
image2= np.int32(image2)
image3 = image1 - image2
bitwise_not()
进行操作:
image3 = bitwise_not( bitwise_not(image1) - bitwise_not(image2) )
# find moving image.
#
# running the program pops up a window to watch the video.
# the program video window shows the first monitor,
# but watch the program video window on second extended monitor
import cv2
import numpy as np
# Path to video file
cap = cv2.VideoCapture(
1,
apiPreference=cv2.CAP_ANY,
params=[cv2.CAP_PROP_FRAME_WIDTH, 1280, cv2.CAP_PROP_FRAME_HEIGHT, 720],
) # I made cap = 1280, 720 resolution to speed the program up on my computer. I have a rtx 3060, obs studio at 60 fps
# Used as counter variable
count = 1
# checks whether frames were extracted
success = 1
# create frame_1 and frame_2 to be able to use the frames between if conditions
frame_1 = 0
frame_2 = 0
# get 2 frames to start with
count_subtraction = 0
while success:
# function extract frames
success, image = cap.read()
if count_subtraction == 0:
if count <= 2:
# Saves the frames with frame-count
cv2.imwrite("frame_%d.jpg" % count, image, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) # jpg 100% quality
count += 1
if count == 3:
frame_1 = cv2.imread("frame_1.jpg", 0)
frame_2 = cv2.imread("frame_2.jpg", 0)
# use the frames below
# Create the sharpening kernel
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
# Sharpen the image
frame_1 = cv2.filter2D(frame_1, -1, kernel)
frame_2 = cv2.filter2D(frame_2, -1, kernel)
# subtract the images
subtracted = cv2.subtract(frame_2, frame_1)
subtracted_sharpened = cv2.filter2D(subtracted, -1, kernel)
# TO show the output
cv2.imshow("image", subtracted_sharpened)
# the else condition to count_subtraction needs a first frame
frame_1 = frame_2
count = 1
count_subtraction = 1
else:
cv2.imwrite("frame_2.jpg", image, [int(cv2.IMWRITE_JPEG_QUALITY), 100]) # jpg 100% quality
frame_2 = cv2.imread("frame_2.jpg", 0)
# use the frames below
# Create the sharpening kernel
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
# Sharpen the image
frame_2 = cv2.filter2D(frame_2, -1, kernel)
# subtract the images
subtracted = cv2.subtract(frame_2, frame_1)
subtracted_sharpened = cv2.filter2D(subtracted, -1, kernel)
# TO show the output
cv2.imshow("image", subtracted_sharpened)
# the second frame becomes a first frame
frame_1 = frame_2
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()