用Python解析opencv UpdateMoonHistory的输出

2024-10-04 09:29:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从实时图像馈送(网络摄像头)生成灰度运动历史图像,以供我构建的CNN模型处理,从UpdateEmotionHistory函数我得到以下输出:

[width x height] array of type np.float32

我想把这个数组转换成灰度图像,运动中的变化越亮like this

编辑:下面添加了代码示例

import cv2
import numpy as np
import time
import genMHI_util

cap = cv2.VideoCapture(0)
fgbg = cv2.createBackgroundSubtractorMOG2(history=1000,detectShadows=False)
colourThreshold = 0.975
frame_reduction_counter = 0

mhi = np.zeros((genMHI_util.MHI_WIDTH, genMHI_util.MHI_HEIGHT), np.float32)

while True:
    ret, frame = cap.read()
    # kernel = np.ones((5, 5), np.float32) / 25
    # frame = cv2.medianBlur(frame, 5)
    # frame = cv2.filter2D(frame, -1, kernel)  # Blur image

    if frame_reduction_counter >= 0: 
 # Disregarding frames that do not contain enough movement, below the set threshold
    frame_reduction_counter = 0

    timestamp = cv2.getTickCount() / cv2.getTickFrequency()

    # Do background subtraction on frame to get silhouette
    silhouette = fgbg.apply(frame)
    silhouette = cv2.resize(silhouette, (genMHI_util.MHI_WIDTH, genMHI_util.MHI_HEIGHT))

    # Update MHI
    cv2.motempl.updateMotionHistory(silhouette, mhi, timestamp, 0.5)

    # Do something with 'mhi' object (300x300 float array) ?

    # Convert float array to int
    mask = cv2.convertScaleAbs(mhi,
                               alpha=(255 / genMHI_util.MHI_DURATION),
                               beta=((genMHI_util.MHI_DURATION - timestamp) * 255 / genMHI_util.MHI_DURATION))

    # Preview images
    cv2.imshow('original', frame)
    cv2.imshow('silhouette', silhouette)
    cv2.imshow('mhi', mask)

    cv2.waitKey(1)

frame_reduction_counter += 1


cap.release()
cv2.destroyAllWindows()

常数:

MHI_DURATION = 5
MHI_WIDTH = 300
MHI_HEIGHT = 300

Output without movement - bit of camera noise

Output with movement - kind of works but very badly


Tags: of图像importutilnpcounterarraycv2