gpt4 book ai didi

python - 检测接近的物体

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:19 24 4
gpt4 key购买 nike

我读了this博文中他使用激光和网络摄像头来估计纸板与网络摄像头的距离。

我对此有另一个想法。我不想计算与网络摄像头的距离。

我想检查是否有物体正在接近网络摄像头。根据我的说法,该算法将类似于:

  1. 检测网络摄像头中的对象。
  2. 如果物体靠近网络摄像头,它会在视频源中变得越来越大。
  3. 使用此数据进行进一步计算。

因为我想检测随机对象,所以我使用 findContours() 方法在视频源中查找轮廓。使用它,我至少会在视频源中看到对象的轮廓。源代码是:

import numpy as np
import cv2

vid=cv2.VideoCapture(0)
ans, instant=vid.read()
average=np.float32(instant)
cv2.accumulateWeighted(instant, average, 0.01)
background=cv2.convertScaleAbs(average)

while(1):
_,f=vid.read()
imgray=cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
ret, thresh=cv2.threshold(imgray,127,255,0)
diff=cv2.absdiff(f, background)
cv2.imshow("input", f)
cv2.imshow("Difference", diff)
if cv2.waitKey(5)==27:
break
cv2.destroyAllWindows()

输出是:

enter image description here

我被困在这里了。我将轮廓存储在一个数组中。当尺寸增加时我该怎么办?我该如何继续?

最佳答案

这里的一个问题是识别和区分移动对象与视频源中的其他内容。一种方法可能是让相机“学习”没有物体的背景。然后你可以不断地将它的输入与这个背景进行比较。获取背景的一种方法是使用运行平均值。

任何大于一个小阈值的差异都意味着有一个移动的物体。如果你不断地显示这种差异,你基本上就有了一个运动追踪器。对象的大小只是所有非零(阈值)像素或其边界矩形的总和。您可以跟踪此大小并使用它来猜测对象是靠近还是更远。形态学操作可以帮助将轮廓分组为一个有凝聚力的对象。

由于它将跟踪任何运动,如果有两个对象,它们将被一起计算。在这里您可以使用轮廓来查找和跟踪单个对象,例如使用轮廓边界或质心。您也可以按颜色将它们分开。

以下是使用此策略的一些结果(灰色 Blob 是我的手):

My hand moving towards the camera

My hand moving away from the camera

它实际上很好地猜测了我的手移动的方向。

代码:

import cv2
import numpy as np

AVERAGE_ALPHA = 0.2 # 0-1 where 0 never adapts, and 1 instantly adapts
MOVEMENT_THRESHOLD = 30 # Lower values pick up more movement
REDUCED_SIZE = (400, 600)
MORPH_KERNEL = np.ones((10, 10), np.uint8)

def reduce_image(input_image):
"""Make the image easier to deal with."""
reduced = cv2.resize(input_image, REDUCED_SIZE)
reduced = cv2.cvtColor(reduced, cv2.COLOR_BGR2GRAY)
return reduced

# Initialise
vid = cv2.VideoCapture(0)
average = None

old_sizes = np.zeros(20)
size_update_index = 0

while (True):
got_frame, frame = vid.read()

if got_frame:
# Reduce image
reduced = reduce_image(frame)
if average is None: average = np.float32(reduced)

# Get background
cv2.accumulateWeighted(reduced, average, AVERAGE_ALPHA)
background = cv2.convertScaleAbs(average)

# Get thresholded difference image
movement = cv2.absdiff(reduced, background)
_, threshold = cv2.threshold(movement, MOVEMENT_THRESHOLD, 255, cv2.THRESH_BINARY)

# Apply morphology to help find object
dilated = cv2.dilate(threshold, MORPH_KERNEL, iterations=10)
closed = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE, MORPH_KERNEL)

# Get contours
contours, _ = cv2.findContours(closed, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(closed, contours, -1, (150, 150, 150), -1)

# Find biggest bounding rectangle
areas = [cv2.contourArea(c) for c in contours]
if (areas != list()):
max_index = np.argmax(areas)
max_cont = contours[max_index]

x, y, w, h = cv2.boundingRect(max_cont)
cv2.rectangle(closed, (x, y), (x+w, y+h), (255, 255, 255), 5)

# Guess movement direction
size = w*h
if size > old_sizes.mean():
print "Towards"
else:
print "Away"

# Update object size
old_sizes[size_update_index] = size
size_update_index += 1
if (size_update_index) >= len(old_sizes): size_update_index = 0

# Display image
cv2.imshow('RaptorVision', closed)

显然,这需要在识别、选择和跟踪对象等方面做更多的工作(目前,如果背景中有其他移动的东西,它会做得很糟糕)。还有许多参数可以改变和调整(设置的参数对我的系统很有效)。不过,我会把它留给你。

一些链接:

background extraction

motion tracking

如果您想通过背景去除获得更多高科技,请看这里:

wallflower

关于python - 检测接近的物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24379456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com