我正在使用显微镜观察小4微米珠子的运动。我有一个珠子移动的视频,我想处理视频,以提取珠子位置作为时间的函数,以获得他们的运动的数学模型。
我目前正在使用opencv并在python中编程
我的代码是从文件导入视频,对图像进行阈值处理,然后应用HoughCircles变换来查找球形珠子。
import numpy as np
import cv2
def nothing(x):
pass
cap = cv2.VideoCapture('testvideo.mp4')
cv2.namedWindow('trackbar')
cv2.createTrackbar('Param1','trackbar',40,255,nothing)
cv2.createTrackbar('Param2','trackbar',10,255,nothing)
cv2.createTrackbar('MaxRadius','trackbar',18,255,nothing)
while(cap.isOpened()):
e1 = cv2.getTickCount()
ret, frame = cap.read()
#get grayscale image for HoughCircles
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
p1 = cv2.getTrackbarPos('Param1','trackbar')
p2 = cv2.getTrackbarPos('Param2','trackbar')
rMax = cv2.getTrackbarPos('MaxRadius','trackbar')
#Threshold grayscale image
ret,th1 = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
#Find circles in image and store locations to circles list
circles = cv2.HoughCircles(th1, cv2.cv.CV_HOUGH_GRADIENT, 1, 10,
param1=p1, param2=p2,minRadius=0,
maxRadius=rMax)
#Hack for fixing the list if it is empty so program wont crash
if circles == None:
circles = [[[0,0,0.000],[0,0,0.000]]]
#convert circles list to integer list
circles = np.uint16(np.around(circles))
#store points to a file
datafile = file('datafile.txt', 'a')
np.savetxt(datafile, circles[0], fmt ='%i',delimiter=',', newline = ',')
datafile.write('\n')
datafile.close()
for i in circles[0,:]:
# draw the outer circle
cv2.circle(frame,(i[0],i[1]),i[2],(0,255,0),2)
# draw the center of the circle
cv2.circle(frame,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',frame)
cv2.imshow('threshold video',th1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
e2 = cv2.getTickCount()
time = (e2 - e1)/ cv2.getTickFrequency()
print time
cap.release()
cv2.destroyAllWindows()
以下是我用于检测珠子的视频的示例帧。 http://imgur.com/bVZbH3c
以下是我使用之前算法执行的单帧跟踪的示例 http://imgur.com/4VWJI2F
我不需要检测每一个珠子。只需聚合就可以了。
珠子是球形的并且应该看起来相同,所以是否有一个库可以用来将珠子图像整合到整个图像上并查看图像中点最相关的位置?有时珠子没有聚焦,这就是为什么我现在的节目一直在弹跳并给我误报珠子。
我最终需要这个过程实时发生,所以尽可能让算法尽可能高效。
如果有人知道这种问题的好方法,我们将不胜感激。