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

时间:2017-04-30 00:03:04

标签: python opencv image-processing object-detection

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

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 个答案:

答案 0 :(得分:1)

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

    每个框架上的
  1. A

    模糊A以消除噪音

  2. 减去/来自A0的最后一帧A

    所以dA=A-A0|dA|>threshold创建投资回报率。这个面具将包含蚂蚁现在和现在的区域。

  3. 设置最后一帧

    所以A0=A

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

    我不使用 Python ,也不使用 OpenCV ,因此我无法提供任何代码......

    而不是A0您也可以使用背景图片(没有蚂蚁)或整合/平均图片...