当前算法使用goodFeaturestoTrack来选择角点,但我想选择自己的点。其次,我想保存关于该点移动到哪些像素的数据。我该如何解决这两个问题呢?我目前使用的代码是: -
import numpy as np
import cv2
cap = cv2.VideoCapture('output.avi')
# params for ShiTomasi corner detection
# throw every other corners below quality level. Sort rest in descending order. Pick greatest, throw rest in min and pick N greatest
feature_params = dict( maxCorners = 1, # how many pts. to locate
qualityLevel = 0.3, # b/w 0 & 1, min. quality below which everyone is rejected
minDistance = 7, # min eucledian distance b/w corners detected
blockSize = 7 ) #
# Parameters for lucas kanade optical flow
lk_params = dict( winSize = (15,15), # size of the search window at each pyramid level
maxLevel = 2, # 0, pyramids are not used (single level), if set to 1, two levels are used, and so on
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Criteria : Termination criteria for iterative search algorithm.
# after maxcount { Criteria_Count } : no. of max iterations.
# or after { Criteria Epsilon } : search window moves by less than this epsilon
# Create some random color for the pt. chosen
color = np.random.randint(0,255,(1,3))
# Take first frame and find corners in it
ret, old_frame = cap.read() #read frame
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) #convert to grayscale
p0 = cv2.goodFeaturesToTrack(old_gray, mask = None, **feature_params) #use goodFeaturesToTrack to find the location of the good corner.
#cvPoint pl = new cvPoint(2,3)
# Create a mask image for drawing purposes filed with zeros
mask = np.zeros_like(old_frame)
while(1):
ret,frame = cap.read()
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# calculate optical flow
# err kind of gives us the correlation error(matching error)
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)
# Select good points
good_new = p1[st==1]
good_old = p0[st==1]
# draw the tracks
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel()
c,d = old.ravel()
mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2)
frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1)
img = cv2.add(frame,mask)
cv2.imshow('frame',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
#
# # Now update the previous frame and previous points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1,1,2)
#
## release and destroy all windows.
cv2.destroyAllWindows()
cv2.release()
答案 0 :(得分:1)
正如Mark Miller所说,你可以使用任何特征向量作为输入点。 cv2.calcOpticalFlowPyrLK将使用它们通过比较给定坐标周围的像素块来在新图像上找到这些要素的新位置。
要保存新的,您需要捕获cv2.calcOpticalFlowPyrLK
的返回值关于其他功能的一点点: cv2.goodFeaturesToTrack(施 - 托马斯或哈里斯的角落)被使用,因为两个垂直的边缘很容易找到,它提供了很多功能,所以即使它们中的一些被遮挡,你也不会一无所获。像ORB这样的更高级的功能在描述符中有更多的信息,可用于链接不同图像上的特征,而不是Shi-Thomasi或Harris角落中获得的两个参数,将它们与简单的Lucas一起使用会有点浪费 - Kanade算法。