我正在尝试在 opencv 中跟踪视频中移动的蚂蚁。我无法追踪蚂蚁。建议?提供代码和 link

I am trying to track the moving ants in the video in opencv. I am not able to track the ants. suggestions? Code and link is provided

这是视频 link:I 我试图在提供的视频 link 中追踪移动的蚂蚁。不幸的是,我无法这样做。有什么建议吗?

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()

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

  1. 每帧A

    模糊 A 一点以去除噪音

  2. substract/threshold 最后一帧 A0 来自 A

    所以 dA=A-A0 并通过 |dA|>threshold 创建 ROI。这个面具将包含蚂蚁所在的区域和现在所在的区域。

  3. 设置最后一帧

    所以A0=A

要识别 ROI 的哪一部分是旧的和实际的蚂蚁位置,只需检查 A 中对应的黑色像素 ...

我不使用 Python 也不使用 OpenCV 所以我没有信心提供任何代码 ...

您还可以使用背景图像(没有蚂蚁)或 integrated/averaged 随着时间的推移图像...

而不是 A0