python - opencv/ python : motion detect weird thresholding

标签 python opencv motion threshold

我正在尝试使用我的网络摄像头制作一个运动检测程序,但是在对帧差异进行阈值处理时我得到了这个奇怪的结果:

当我移动时:(我猜似乎没问题) ![在此处输入图片描述][1]

当我不动时: ![在此处输入图片描述][2]

这会是什么?我已经运行了几个具有完全相同算法的程序,并且阈值处理正常..

这是我的代码:

import cv2
import random
import numpy as np

# Create windows to show the captured images
cv2.namedWindow("window_a", cv2.CV_WINDOW_AUTOSIZE) 
cv2.namedWindow("window_b", cv2.CV_WINDOW_AUTOSIZE)

# Structuring element
es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9,4))
## Webcam Settings
capture = cv2.VideoCapture(0)

#dimensions
frameWidth = capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)
frameHeight = capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)

while True:
    # Capture a frame
    flag,frame = capture.read()
    
    current = cv2.blur(frame, (5,5))
    difference = cv2.absdiff(current, previous) #difference is taken of the current frame and the previous frame

    frame2 = cv2.cvtColor(difference, cv2.cv.CV_RGB2GRAY)
    retval,thresh = cv2.threshold(frame2, 10, 0xff, cv2.THRESH_OTSU)
    dilated1 = cv2.dilate(thresh, es)
    dilated2 = cv2.dilate(dilated1, es)
    dilated3 = cv2.dilate(dilated2, es)
    dilated4 = cv2.dilate(dilated3, es)

    cv2.imshow('window_a', dilated4)
    cv2.imshow('window_b', frame)

    previous = current
    
    key = cv2.waitKey(10) #20
    if key == 27: #exit on ESC
        cv2.destroyAllWindows()
        break

提前致谢! [1]: http://i.stack.imgur.com/hslOs.png [2]: http://i.stack.imgur.com/7fB95.png

最佳答案

您需要的第一件事是 previous = cv2.blur(frame, (5,5)) 在 while 循环之前的帧抓取之后准备您之前的样本。

这将使您发布的代码有效,但不会解决您的问题。

我认为您遇到的问题是由于您使用的阈值算法类型所致。试试二进制文件,cv2.THRESH_BINARY,而不是 Otsu 的。它似乎解决了我的问题。

关于python - opencv/ python : motion detect weird thresholding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14529439/

相关文章:

python - 将字符串列表从文件转换为整数列表

matlab - 使用riesz变换分析后的相移图像内容

java - 我应该使用哪种语言进行编码以通过语音控制运动?

android - 使用 kivy 在 Android 和 IOS 中播放音频(mp3)

python - Celery/Redis 一些(很多)消息被丢弃

python - 使用 Open CV Python 将 alpha channel 添加到单色图像

无法解析 Android CDT setup std

Android MotionLayout 运动交错

python - 编码 : utf-8 doesn't seem to work

python - 我怎样才能从 url 中获取实时流?