目录
1.问题描述
最近在用opencv提取光流特征时候,参考别人的代码,使用cv2.DualTVL1OpticalFlow_create()时,发现e 'cv2.cv2' has no attribute 'DualTVL1OpticalFlow_create'这样的错误如下图所示
Python 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.DualTVL1OpticalFlow_create()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'cv2.cv2' has no attribute 'DualTVL1OpticalFlow_create'
>>> cv2.__version__
'4.1.2'
>>>
2.解决办法
最后发现:这是因为opencv版本不一致的原因,需要用新的api接口"cv2.optflow.DualTVL1OpticalFlow_create()"进行调用如下图所示:
Python 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.DualTVL1OpticalFlow_create()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'cv2.cv2' has no attribute 'DualTVL1OpticalFlow_create'
>>> cv2.__version__
'4.1.2'
>>>
KeyboardInterrupt
>>> cv2.optflow.DualTVL1OpticalFlow_create()
<optflow_DualTVL1OpticalFlow 0x7f5490dab7b0>
3.注意事项
在python中使用opencv进行光流特征提取时,发现opencv4.0以下版本API接口“cv2.DualTVL1OpticalFlow_create()”,opencv4.0以上版本API接口使用“cv2.optflow.DualTVL1OpticalFlow_create()”;安装opencv时候,不仅要安装opencv-python版本,还需要安装pencv-contrib-python,并且两者安装版本最好一致,命令如下:
pip install opencv-python==4.1.2.30 pip install opencv-contrib-python==4.1.2.30
参考网址:
- AttributeError: module 'cv2.cv2' has no attribute 'DualTVL1OpticalFlow_create'_Hello Word!-CSDN博客
- AttributeError:module 'cv2.cv2' has no attribute 'DualTVL1OpticalFlow_create' 解决_红色小宝马的博客-CSDN博客
- How to use DualTLV1Optical Flow in python ? - OpenCV Q&A Forum
- OpenCV: cv::optflow::DualTVL1OpticalFlow Class Reference