在ubuntu20.04下安装opencv4(C++版)

1. 更新

sudo apt-get update
sudo apt-get upgrade

2. 安装依赖项

$ sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev 
$ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev

3. 下载opencv

进入这个网址:https://github.com/opencv/opencv/releases 选择对应的版本。

sudo su # change to root
cd /opt # 3rd party software install dir, not necessary
git clone https://github.com/opencv/opencv.git
cd opencv 
git checkout 4.5.3
cd ..

git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 4.5.3
cd ..

4. 编译和安装opencv

/opt$ cd opencv
/opt/opencv$ mkdir release
/opt/opencv$ cd release
/opt/opencv/release$ cmake -D BUILD_TIFF=ON -D WITH_CUDA=OFF -D ENABLE_AVX=OFF -D WITH_OPENGL=OFF -D WITH_OPENCL=OFF -D WITH_IPP=OFF -D WITH_TBB=ON -D BUILD_TBB=ON -D WITH_EIGEN=OFF -D WITH_V4L=OFF -D WITH_VTK=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules /opt/opencv/
/opt/opencv/release$ make -j4 # multi task
/opt/opencv/release$ make install
/opt/opencv/release$ ldconfig
/opt/opencv/release$ exit # exit root
/opt/opencv/release$ cd ~

之后检测安装情况:

pkg-config --modversion opencv

 之后写一个测试程序,可能会报错:

 这个时候进入/usr/local/include/ 目录里面,你能看到只有opencv4这个文件,这个时候把opencv4里面opencv2复制到/usr/local/include/目录里面。

/usr/local/include/opencv4$ sudo mv opencv2 /usr/local/include/

随便一个测试程序:

需要在有一个测试图片“test.jpg”,图片的格式可以是jpg,png,bmp等,取决于第二步依赖项所支持的格式。

#include 
#include 
int main( int argc, char** argv ) {
    cv::Mat image;
    image = cv::imread("test.jpg" , CV_LOAD_IMAGE_COLOR);	      
    if(! image.data ) {
        std::cout <<  "Could not open or find the image" << std::endl ;
	    return -1;
    }

    std::cout << "image wide: "<< image.cols << ",image high: " << image.rows << ",image channels: "<< image.channels() << std::endl;
    /* display image
    cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
    cv::imshow( "Display window", image );		    
    cv::waitKey(0);
    */
    size_t y,x;// y is row, x is col
    int c;     // c is channel
    y = x = 250;
    c = 2;
    // row_ptr is the head point of y row
    unsigned char *row_ptr = image.ptr(y);
    // data_ptr points to pixel data
    unsigned char *data_ptr = &row_ptr[x * image.channels()]; 
    unsigned char data =  data_ptr[c];

    // use cv::Mat::at() to get the pixel value
    // unsigned char is not printable
    // std::cout << std::isprint(data)<(y,x)[c]) << std::endl;
    std::cout << "pixel value at y, x ,c"<(image.at(y,x)[c]) << std::endl;
    return 0;
}

编译:

g++ test.cpp -o test `pkg-config --cflags --libs opencv`

使用g++记得带上:

`pkg-config --cflags --libs opencv`

参考

https://blog.csdn.net/weixin_44278406/article/details/103619191

你可能感兴趣的:(opencv,linux,opencv,linux,ubuntu)