问题描述
我尝试使用稳健的局部光流(RLOF)函数(https://docs.opencv.org/master/d2/d84/group__optflow.html)的opencv库。为此,我使用opencv的FAST函数提取视频(帧)的不同特征点。我的视频大小为640 * 480(30 fps)。然后,在视频的第n帧和第n-1帧的主函数中使用FAST函数。然后,我通过对图像n和图像n-1函数进行FAST功能对特征点检测使用RLOF,并通过阅读该算法的文档可以找到具有不同参数的参数。
此外,在每一帧之间,我没有检测到完全相同数量的特征点,因此我认为这就是选项setUseInitialFlow(True)
带给我这个opencv cv2 error. error: OpenCV(4.4.0) /tmp/pip-req-build-b_zf9wbm/opencv_contrib/modules/optflow/src/rlofflow.cpp:372: error: (-215:Assertion Failed) nextPtsMat.checkVector(2,CV_32F,true) == npoints in function 'calc'
的原因。
我的代码有效,我的变量keypoints_old将其包含为FAST(大于500)找到的坐标:
keypoints_old =
[[250. 3.]
[400. 3.]
[411. 4.]
...
[360. 465.]
[607. 466.]
[519. 471.]]
但是,我使用RLOF函数找到的用于找到下一个图像的两个图像之间的运动量的坐标并不是真正一致的。在opencv文档中,我应该从中获取点坐标。我想也许这些坐标代表了运动的数量,但是,阅读文档并不应该是这样。
keypoints_new = [[-1.0420260e+20 4.5582838e-41]
[-1.0420260e+20 4.5582838e-41]
[ 3.5204788e-38 0.0000000e+00]
...
[-3.5464977e-24 4.5582838e-41]
[-3.5465167e-24 4.5582838e-41]
[-3.5465356e-24 4.5582838e-41]]
我的问题是,如何正确使用此功能RLOF?为什么第二张图像的找到点的坐标与前一张图像(第一张)的坐标不合逻辑?我的代码有什么问题?谢谢您的回答。
我的功能:
import numpy as np
import cv2 as cv
import os
import argparse
import re
import os
import argparse
def FAST(img):
# Initiate FAST object with default values
fast = cv.FastFeatureDetector_create()
keypoints = fast.detect(img,None)
pts = cv.KeyPoint_convert(keypoints)
return pts
def RLOF(old_frame,new_frame,keypoints_old,keypoints_new):
# Parameters for RLOF
instance = cv.optflow.RLOFOpticalFlowParameter_create()
# instance.setUseInitialFlow(True)
instance.setMaxIteration(30)
instance.setnormSigma0(3.2)
instance.setnormSigma1(7.0)
instance.setLargeWinSize(21)
instance.setSmallWinSize(9)
instance.setMaxLevel(9)
instance.setmineigenValue(0.0001)
instance.setCrossSegmentationThreshold(25)
instance.setGlobalMotionRansacThreshold(10.0)
instance.setUseIlluminationModel(True)
instance.setUseGlobalMotionPrior(False)
keypoints_new,st,err = cv.optflow.calcopticalFlowSparseRLOF(
old_frame,None,rlofParam = instance,forwardBackwardThreshold = 0)
return keypoints_new
def __main__():
parser = argparse.ArgumentParser(description='Process some video.')
parser.add_argument('file_path',type=str,help='Video_file path')
args = parser.parse_args()
video_file = args.file_path
cap = cv.VideoCapture(video_file)
fps = cap.get(cv.CAP_PROP_FPS)
frame_count = int(cap.get(cv.CAP_PROP_FRAME_COUNT))
ret,new_frame = cap.read() # 0
ret,new_frame = cap.read() # 1
p1 = FAST(new_frame)
print('frame_count = {}'.format(frame_count))
while cap.isOpened():
for i in range(frame_count):
old_frame = new_frame # 1 2 3 4
p0 = p1
ret,new_frame = cap.read() # 2 3 4 5 ..
if ret:
p1 = FAST(new_frame)
RLOF(old_frame,p0,p1,i)
else:
cap.release()
break
__main__()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)