使用opencv python

时间:2017-02-16 18:20:12

标签: python-2.7 image-processing opencv3.0

你能告诉我如何使用帧差分获得阈值图像吗? 我对各种背景减法方法有所了解,但我不想使用它们。

cv2.substract(img1,img2)给出了这个图像

substract: Screenshot

但我想要这个结果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.0 python 2.7

1 个答案:

答案 0 :(得分:3)

因为你可以在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