自己在windows下用cmake成功编译了opencv 2.4.13.2,但在实际调用cvShowImage
函数时报错了:
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file D:\caffe-static\source\opencv-2.4.13.2\modules\highgui\src\window.cpp, line 501
terminate called after throwing an instance of ‘cv::Exception’
这个错误提示的意思就是cvShowImage
这个函数没有被实现。
查看了opencv-2.4.13.2\modules\highgui\src\window.cpp
源码,才明白这个文件只是没有函数实现的虚代码。windows下的真正实现代码是同文件夹下的windows_w32.cpp
。应该是编译开关问题造成没有编译windows_w32.cpp
。
于是在opencv-2.4.13.2\modules\highgui\CMakeLists.txt
中找到如下代码 ,可以看到HAVE_WIN32UI
用于控制是否编译windows_w32.cpp
:
elseif(HAVE_WIN32UI)
list(APPEND highgui_srcs src/window_w32.cpp)
elseif(HAVE_GTK)
list(APPEND highgui_srcs src/window_gtk.cpp)
elseif(HAVE_CARBON)
list(APPEND highgui_srcs src/window_carbon.cpp)
list(APPEND HIGHGUI_LIBRARIES "-framework Carbon" "-framework QuickTime")
elseif(HAVE_COCOA)
list(APPEND highgui_srcs src/window_cocoa.mm)
list(APPEND HIGHGUI_LIBRARIES "-framework Cocoa")
endif()
进一步在opencv-2.4.13.2\CMakeLists.txt
下找到了控制HAVE_WIN32UI
的开关选项WITH_WIN32UI
,如下:
else()
if(DEFINED WITH_QT)
status(" QT:" NO)
endif()
if(DEFINED WITH_WIN32UI)
status(" Win32 UI:" HAVE_WIN32UI THEN YES ELSE NO)
else()
if(APPLE)
if(WITH_CARBON)
status(" Carbon:" YES)
else()
status(" Cocoa:" YES)
endif()
else()
status(" GTK+ 2.x:" HAVE_GTK THEN "YES (ver ${ALIASOF_gtk+-2.0_VERSION})" ELSE NO)
status(" GThread :" HAVE_GTHREAD THEN "YES (ver ${ALIASOF_gthread-2.0_VERSION})" ELSE NO)
status(" GtkGlExt:" HAVE_GTKGLEXT THEN "YES (ver ${ALIASOF_gtkglext-1.0_VERSION})" ELSE NO)
endif()
endif()
endif()
至此找到原因:编译opencv的时候设置了WITH_WIN32UI=off
导致cmake没有编译windows_w32.cpp
最终导致运行时调用cvShowImage
出错。
解决办法也很简单,在cmake生成Makefile的命令行参数中添加-DWITH_WIN32UI=on
指定编译加入UI函数支持。
以下是我编译opencv的完整命令行参数(注意最后一行):
cmake .. -G "MinGW Makefiles" \
-DCMAKE_BUILD_TYPE:STRING=release \
-DCMAKE_C_FLAGS="-m64 " \
-DCMAKE_CXX_FLAGS="-m64 " \
-DCMAKE_EXE_LINKER_FLAGS="-static -static-libstdc++ -static-libgcc " \
-DCMAKE_INSTALL_PREFIX="D:\caffe-static\release\opencv_windows_gcc520_x86_64" \
-DBUILD_DOCS=off \
-DBUILD_SHARED_LIBS=off \
-DBUILD_PACKAGE=on \
-DBUILD_PERF_TESTS=off \
-DBUILD_FAT_JAVA_LIB=off \
-DBUILD_TESTS=off \
-DBUILD_TIFF=on \
-DBUILD_JASPER=on \
-DBUILD_JPEG=on \
-DBUILD_OPENEXR=on \
-DBUILD_PNG=on \
-DBUILD_ZLIB=on \
-DBUILD_opencv_apps=off \
-DBUILD_opencv_calib3d=off \
-DBUILD_opencv_contrib=off \
-DBUILD_opencv_features2d=off \
-DBUILD_opencv_flann=off \
-DBUILD_opencv_gpu=off \
-DBUILD_opencv_java=off \
-DBUILD_opencv_legacy=off \
-DBUILD_opencv_ml=off \
-DBUILD_opencv_nonfree=off \
-DBUILD_opencv_objdetect=off \
-DBUILD_opencv_ocl=off \
-DBUILD_opencv_photo=off \
-DBUILD_opencv_python=off \
-DBUILD_opencv_stitching=off \
-DBUILD_opencv_superres=off \
-DBUILD_opencv_ts=off \
-DBUILD_opencv_video=off \
-DBUILD_opencv_videostab=off \
-DBUILD_opencv_world=off \
-DBUILD_opencv_lengcy=off \
-DWITH_DSHOW=off \
-DWITH_JASPER=on \
-DWITH_JPEG=on \
-DWITH_1394=off \
-DWITH_OPENEXR=on \
-DWITH_PNG=on \
-DWITH_TIFF=on \
-DWITH_1394=off \
-DWITH_EIGEN=off \
-DWITH_FFMPEG=off \
-DWITH_GIGEAPI=off \
-DWITH_GSTREAMER_0_10=off \
-DWITH_PVAPI=off -DWITH_CUDA=off \
-DWITH_CUFFT=off \
-DWITH_OPENCL=off \
-DWITH_OPENCLAMDBLAS=off \
-DWITH_OPENCLAMDFFT=off \
-DWITH_QT=off \
-DWITH_VFW=off \
-DWITH_VTK=off \
-DWITH_XIMEA=off \
-DWITH_WIN32UI=on