1. 安装pkg-config
    终端输入:apt-get install pkg-config
  2. 下载opencv包
    https://opencv.org/releases/
    我选的是4.3.10版本的,点击sources下载,得到opencv-4.3.10.zip
  3. 包解压后,进入这个包目录里面,即此时的位置为opencv-4.3.10目录下
  4. 新建一个目录进行OpenCV的编译目录
    终端输入:
    mkdir build
    cd bulid
    cmake …
  5. 进入build目录,即cd build
    make
    输入sudo make install

(这里有很多博主都是直接make install,我自己安装的时候报错了,加了sudo就成功了)

等make install执行完毕之后,需要对环境变量进行配置
终端输入:vim /etc/ld.so.conf.d/opencv.conf
在文件末尾添加:/usr/local/lib(可能是空文件)
配置环境变量
终端输入:vim /etc/bash.bashrc
在文件末尾添加:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
配置完成后重新打开终端,使得配置生效
6. opencv测试
终端输入:sudo ldconfig -v | grep opencv
检查opnecv是否安装成功,不报错就算成功

随便找个地方新建一个test.cpp文件测试

#include "highgui.h"
#include "opencv2/opencv.hpp"
#include <iostream>
 
#define Usage() \
        { std::cerr << "Usage: ./showpic FILE" << std::endl; }
//using namespace cv;
 
int main(int argc, char** argv)
{
        if (argc != 2) Usage();
        IplImage* img = cvLoadImage(argv[1]);
        cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE);
        cvShowImage("Example1", img);
        cvWaitKey(0);
        cvReleaseImage(&img);
        cvDestroyWindow("Example1");
}


然后进到这个cpp所在的目录进行编译
终端输入
g++ test.cpp pkg-config --libs --cflags opencv -o a.out
进行编译之后,在该目录下复制过来一个.jpg格式的图片文件

终端输入./a.out ./kk.jpg(图片名)

成功执行后将会出现一个窗口显示图片

如果执行之后,提示错误如下:

error: (-2: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 function ‘cvNamedWindow’
我按照提示安装libgtk2.0-dev又出现报错,后来的解决方案如下:

(其实我已经装了libgtk2.0-dev,只是版本不太对还是咋地)
检查自己是否安装libgtk2.0-dev
sudo aptitude search libgtk2.0-dev
若安装成功会出现以下提示:

p  libgtk2.0-dev         - development files for the GTK+ library

接着,你需要重新build opencv文件。

进入刚刚创建好的build文件夹,然后执行cmake命令

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GTK=ON -D WITH_OPENGL=ON ..

谨记设置WITH_GTK=ON,然后接着执行

make
sudo make install

ok,解决啦!

GitHub 加速计划 / opencv31 / opencv
193
14
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:7 个月前 )
b5d38ea4 Optimize gaussian blur performance in FastCV HAL #27217 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake 1 天前
767dd838 Fix configuring with CMake version 4 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐