在使用OpenCV中的Viz模块的过程中,会遇到:
vtkTransformPolyDataFilter (0x5612083903c0): No input data
vtkTransformPolyDataFilter所带来的问题就是,在可视化相机的视锥的时候,图像不能有效显示,仅仅一个空的视锥。
https://discourse.vtk.org/t/logging-error-messages-for-empty-input-data/453
我当前的开发环境为:
OpenCV library status:
-- OpenCV Version: 3.4.1
--
-- VTK library status:
-- VTK Version: 7.1.1
我严重怀疑是我的VTK及OpenCV版本的问题,当我使用 OpenCV 3.4 + VTK 8.2版本时候发现编译问题,不能编译通过,所以我改用
OpenCV library status:
-- OpenCV Version: 4.2.0
--
-- VTK library status:
-- VTK Version: 8.2.0
因为ORB_SLAM等还是依赖的OpenCV 3,因此需要安装多版本的OpenCV及VTK,通过CMakeLists链接不同版本的库进行版本的切换。
当然,在更换了OpenCV和VTK版本以后,可以成功显示图像视锥,而且vtkTransformPolyDataFilter也没有报错!
OpenCV编译的时候需要指定VTK的版本,因此首先需要编译VTK,我预先已经安装了VTK 7.1.1,所以我需要安装VTK 8.2.0版本。
下载地址:https://vtk.org/download/
下载后解压-在根目录创建build文件夹:
终端运行 cmake-gui:
where is the source code :选择解压目录
where to build the binaries: 选择 解压目录/build
Configure:
在build下创建一个文件夹install:
然后在Search中搜索CMAKE_INSTALL_PREFIX,并设置该路径为上面所创建的install目录:
再次Configure,然后Generate,如果没有问题就可以关闭 cmake-gui 了
打开终端,进入 /解压目录/build:依次执行
make
sudo make install
在 /解压目录/build/install 下应该可以找到 头文件(includes)及库文件(lib)
OpenCV与VTK编译一样,也是首先 下载OpenCV + contrib模块(注意版本对应),然后在cmake-gui中设置 CMAKE_INSTALL_PREFIX 中设置安装路径(非系统 /usr/local路径),最后 make + sudo make install(一定要加sudo,要不然会出现各种错误)
在CMakeLists.txt中的设置就两个命令:
set(OpenCV_DIR /home/lucas/OpenCV/build/install/lib/cmake/opencv4) # OpenCV 4的路径
set(VTK_DIR /home/lucas/VTK-8.2.0/build) # VTK 的路径
/home/lucas/OpenCV/build/install/lib/cmake/opencv4 下文件有OpenCVConfig.cmake等配置文件:
因为 /home/lucas/OpenCV/build 即build目录下也有同样的文件,所以也可以OpenCV也可以设置为:
set(OpenCV_DIR /home/lucas/OpenCV/build/install/lib/cmake/opencv4) # OpenCV 4的build路径
/home/lucas/VTK-8.2.0/build 下也是存在VTKConfig.cmake等配置文件,指代头文件及库文件的目录:
在编译完成OpenCV后,使用Viz是不再需要链接Viz的库
编译时会输出OpenCV 及 VTK 版本,可以查看是否切换成功:
cmake_minimum_required(VERSION 3.16)
project(Stereo_OpenCV_PCL)
set(OpenCV_DIR /home/lucas/OpenCV/build)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " OpenCV Version: ${OpenCV_VERSION}")
message(STATUS " library:${OpenCV_LIBS}")
message(STATUS " include path:${OpenCV_INCLUDE_DIRS}")
set(CMAKE_CXX_STANDARD 14)
set(VTK_DIR /home/lucas/VTK-8.2.0/build)
find_package(VTK REQUIRED)
message(STATUS "VTK library status:")
message(STATUS " VTK Version: ${VTK_VERSION}")
message(STATUS " library:${VTK_LIBRARIES}")
message(STATUS " include path:${VTK_INCLUDE_DIRS}")
#include(${VTK_USE_FILE})
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${VTK_INCLUDE_DIRS})
add_executable(vtkDisplay ./main.cpp )
target_link_libraries(vtkDisplay ${OpenCV_LIBS} ${VTK_LIBRARIES} )
使用Viz是不再需要链接VTK的库,因此CmakeLists.txt可以简写为:
cmake_minimum_required(VERSION 3.16)
project(Stereo_OpenCV_PCL)
set(OpenCV_DIR /home/lucas/OpenCV/build)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " OpenCV Version: ${OpenCV_VERSION}")
message(STATUS " library:${OpenCV_LIBS}")
message(STATUS " include path:${OpenCV_INCLUDE_DIRS}")
set(CMAKE_CXX_STANDARD 14)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${VTK_INCLUDE_DIRS})
add_executable(vtkDisplay ./main.cpp )
target_link_libraries(vtkDisplay ${OpenCV_LIBS} )
主文件:
#include
#include "opencv2/viz.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char **argv) {
viz::Viz3d myWindow("Viz Demo");
myWindow.showWidget("World_Coordinate",viz::WCoordinateSystem());
Mat cameraMatrix = (Mat_<double>(3,3)<<3.3512766975396431e+02, 0., 1.7470916794631543e+02, 0.,
3.3545176025968442e+02, 1.2638178637159211e+02, 0., 0., 1.);
Mat src = imread("./left1.png",1);
if(src.empty()){
cout<<"Could not load Image"<<endl;
}
viz::WCameraPosition cam1((Matx33d)cameraMatrix,src,1.0,viz::Color::blue());
Vec3d R(0,1,0),T(1,1,1);
Affine3d Cam1_pose(R,T);
myWindow.showWidget("Cam1_Coordinate",viz::WCoordinateSystem(),Cam1_pose);
myWindow.showWidget("Cam1",cam1,Cam1_pose);
myWindow.spin();
cout << "Hello VIZ" << endl;
return 0;
}
结果:
测试用图片:
这篇博客,总的就说明了一个问题:
如果遇到了vtkTransformPolyDataFilter (XXXXXXX): No input data等问题,换个OpenCV和VTK版本重新编译一下试试呗!
代码及源文件保存在Github:
https://github.com/zhangzelu/Blog_Files.git