使用 opencv python 使用图像减法的阈值图像
Threshold image using image subtraction using opencv python
谁能告诉我如何使用帧差分得到阈值图像
我对各种背景减法有一个想法,但我不想使用它们。
cv2.substract(img1,img2) 给出这张图片
substract:
但我想要这个结果threshold
谁能告诉我怎么做
我不想使用 cv2.createBackgroundSubtractorMOG2() 或此类函数
这是我的
import cv2
import numpy as np
t=0.01
background=cv2.imread('background2.png')
cap = cv2.VideoCapture('car.mp4')
ret,img=cap.read()
avg_img = np.float32(img)
while 1:
ret,img=cap.read()
cv2.accumulateWeighted(img,avg_img,0.1)
res1 = cv2.convertScaleAbs(avg_img)
sub_img=cv2.subtract(res1,img)
cv2.imshow('sub_img',sub_img)
cv2.imshow('sub_img',sub_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我的opencv版本3.0.0python2.7
因为您可以在 cv2 之外完成此操作:
# Subtraction, assuming img1 and img2 are numpy arrays with same dimension; optionally, use np.abs(img1-img2) if you don't care about the sign of the difference
sub = img1 - img2
# Thresholding
threshold = 128 # Set your threshold value
sub[sub >= threshold] = 255 # Above or equal threshold go to max value
sub[sub < threshold] = 0 # Below threshold go to min value
谁能告诉我如何使用帧差分得到阈值图像 我对各种背景减法有一个想法,但我不想使用它们。
cv2.substract(img1,img2) 给出这张图片
substract:
但我想要这个结果threshold 谁能告诉我怎么做
我不想使用 cv2.createBackgroundSubtractorMOG2() 或此类函数 这是我的
import cv2
import numpy as np
t=0.01
background=cv2.imread('background2.png')
cap = cv2.VideoCapture('car.mp4')
ret,img=cap.read()
avg_img = np.float32(img)
while 1:
ret,img=cap.read()
cv2.accumulateWeighted(img,avg_img,0.1)
res1 = cv2.convertScaleAbs(avg_img)
sub_img=cv2.subtract(res1,img)
cv2.imshow('sub_img',sub_img)
cv2.imshow('sub_img',sub_img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
我的opencv版本3.0.0python2.7
因为您可以在 cv2 之外完成此操作:
# Subtraction, assuming img1 and img2 are numpy arrays with same dimension; optionally, use np.abs(img1-img2) if you don't care about the sign of the difference
sub = img1 - img2
# Thresholding
threshold = 128 # Set your threshold value
sub[sub >= threshold] = 255 # Above or equal threshold go to max value
sub[sub < threshold] = 0 # Below threshold go to min value