opencv安装后cv::imshow无法运行

报错长这样

terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.5.5) /home/xiexiaotong/下载/opencv-4.5.5/modules/highgui/src/window.cpp:1268: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

解决方案(bushi)

网上大部分编译安装的流程都是按类似下面这样,然后会产生上面的报错。

unzip opencv-4.5.5.zip
cd opencv-4.4.5
mkdir build
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

按照报错正常安装libgtk2.0-devsudo apt install libgtk2.0-dev ,通常也会报错,我这里采用aptitude,成功安装了。安装过程注意看说明,不能无脑yes过去

sudo aptitude install libgtk2.0-dev

卸载opencv并重装

进入源码build文件夹中

# 卸载opencv
sudo make uninstall
cd .. #使用cd命令进入安装opencv的build目录 
sudo rm -rf build

sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*

cd ..
sudo rm -rf opencv-4.5.5

# 重装opencv
unzip opencv-4.5.5.zip
cd opencv-4.5.5
mkdir build
cd build
sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_GENERATE_PKGCONFIG=ON ..
sudo make -j16
sudo make install
# 中间配置环境变量的过程若之前已配置则可忽略
pkg-config --modversion opencv # 出现版本号即成功

补充

cmake的时候,也可调整后面的变量为

cmake 
-D CMAKE_BUILD_TYPE=RELEASE \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D WITH_GTK=ON \
-D WITH_FFMPEG=ON \
-D WITH_TBB=ON \
-D BUILD_TBB=ON \
-D BUILD_TIFF=ON ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK=ON -D WITH_FFMPEG=ON -D WITH_TBB=ON -D BUILD_TBB=ON -D BUILD_TIFF=ON ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D OPENCV_GENERATE_PKGCONFIG=ON -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_GTK_2_X=ON -D BUILD_TESTS=OFF

若没有把libgtk2.0-dev装上,WITH_GTK打开似乎没有啥用。
结果还是报错,cv::imshow仍然无法运行,但可以把图片保存下来看作为替代方案=_=,凑合用。

---------------------------------------------------------------------------------------------------------------------------------------------------————————
此外,Failed to load module "canberra-gtk-module"这个错误还是会产生的,并且不能解决掉,解决了就会出现最开始的问题,两瓶毒药选一瓶把。

你可能感兴趣的:(opencv,人工智能)