sudo apt-get update
sudo apt-get upgrade
$ 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
进入这个网址: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 ..
/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`
`pkg-config --cflags --libs opencv`
https://blog.csdn.net/weixin_44278406/article/details/103619191