gpt4 book ai didi

python - 我正在尝试在opencv的视频中跟踪移动的 Ant 。我无法追踪 Ant 。建议?提供代码和链接

转载 作者:行者123 更新时间:2023-12-02 17:41:47 24 4
gpt4 key购买 nike

这是视频链接:我正在尝试在提供的视频链接中跟踪移动的 Ant 。不幸的是,我不能这样做。建议?

https://www.youtube.com/watch?v=bc_OdLgGrPQ&feature=youtu.be%22

import cv2
import numpy as np
import matplotlib.pyplot as plt
import imutils

black_lower = (0,0,0) #this two-blacks corresponds to the colour range in hsv, which we are looking in ants
black_upper = (130, 130, 138)
kernel_erode = np.ones((1.0,1.0),np.uint8)
#these kernels are defined for eroding and dialating image
#kernel_dialate = np.ones((1,1),np.uint8)
#r,h,c,w = 600, 400, 100, 700 # i have to play with the size of the window for getting the roi (currently cutting defined roi)
#track_window = (c,r,w,h)
vid = cv2.VideoCapture('/home/marcusidaho/Desktop/opencv_files/tandem_run_classic.mp4')
while (vid.isOpened()):
ret,frame = vid.read()
frame = imutils.resize(frame,width=600) #we have resized the frame, now we have to convert to hsv color space!
ret1,thresh = cv2.threshold(frame,100,255,cv2.THRESH_BINARY_INV)
hsv_frame = cv2.cvtColor(thresh,cv2.COLOR_RGB2HSV)# we have converted the frame in to hsv space!
cv2.imshow('thresh',thresh)
cv2.imshow('frame',frame)
mask = cv2.inRange(hsv_frame,black_lower,black_upper)#now we sh*ould define a mask for color ranges from black_lower to black_upper and remove other noices!
mask = cv2.erode(mask,kernel_erode,iterations=1)#for increasing the object detection , we have to reduce the erode_kernel_size and increase the dialate_kernel_size
mask = cv2.Canny(mask, 150, 255)
mask = cv2.bitwise_and(frame,frame,mask=mask)#for extracting specific region of the image
mask = cv2.cvtColor(mask,cv2.COLOR_BGR2GRAY)
mask3 = cv2.dilate(mask2,kernel_dialate,iterations=2)
mask4 = cv2.GaussianBlur(mask3,(1,1),0)
x,y,w,h = track_window # these are for defining the roi in the video, will have to play with this!
cv2.rectangle(mask4,(x,y),(x+w,y+h),(255,0,0),2)
dst = np.zeros_like(mask4)
dst[y:y+h,x:x+w] = mask4[y:y+h,x:x+w]
cv2.imshow('gray',frame)
#this is finding the specific object in the video and drawing a contour against it
counts = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] #findcountour saves the x and y coordinates of the boundary
#print counts
center = None
if len(counts) > 0:
calculate = max(counts,key = cv2.contourArea)
#print calculate
((x,y),radius) = cv2.minEnclosingCircle(calculate)
print(radius)
M = cv2.moments(calculate) #used to calculate the center of mass of the object
center = (int(M['m10']/M['m00']),int(M['m01']/M['m00'])) #used to extract the centroid
if radius > 6 :
cv2.circle(frame, (int(x),int(y)),int(radius),(0,255,255),2)
cv2.circle(frame, center, 5 ,(0,0,255),-1)
pts = np.append(center,(int(x),int(y)))

for i in xrange(1,len(pts)):
if pts[i-1] is None or pts[i] is None:
continue
thickness = int(np.sqrt(64/float(i+1))*2.5)
cv2.line(frame,pts[i-1],pts[i],(0,0,255),thickness)
cv2.imshow('frame',frame)
cv2.imshow('mask',mask)
if cv2.waitKey(50) and 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()

最佳答案

看起来像静态背景,所以我会:

每一帧上的

  • A
    稍微模糊A以消除噪音
  • A0减去最后一个A阈值
    因此dA=A-A0并通过|dA|>threshold创建 ROI 。此蒙版将包含 Ant 所在的位置和现在所在的区域。
  • 设置最后一帧
    所以A0=A

  • 要识别 ROI 的哪个部分是旧的和实际的 Ant 位置,只需检查 A中的对应像素是否为黑色...
    我不使用 Python 也不使用 OpenCV ,所以我不确定要提供任何代码...
    除了 A0,您还可以使用背景图像(无 Ant )或一段时间内的积分/平均图像...

    关于python - 我正在尝试在opencv的视频中跟踪移动的 Ant 。我无法追踪 Ant 。建议?提供代码和链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43701628/

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