ubuntu|Ubuntu18.04安装opencv3.4.1+opencv_contrib3.4.1

一、ubuntu18.04安装opencv3.4.1环境配置 (1)由于自己使用的是虚拟机,出现了很多问题,所以出现了很多问题,因此,记录整个安装过程。
(2)首先在github上下载源文件解压.
opencv下载 .
opencv_contrib下载.
(3)打开终端,依次执行

sudo apt-get install build-essential sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev sudo apt-get install libgtk2.0-dev pkg-config #防止后面报错 ##第三条命令在ubuntu20.04下安装错误

二使用cmake-gui编译文件 (1)自动编译会报一些错,这里选择手动编译
sudo apt-get install cmake sudoapt-get install cmake-qt-gui sudo cmake-gui

创建一个build文件作为存放编译后的文件,这里我们在opencv3.4.1文件夹内创建build.然后手动编译
注意事项:
OPENCV_EXTRA_MODULES_PATH:选择自己的opencv_contrib中的module
勾选BULID_opencv_world
CMAKE_BULID_TYPE :填写RELEASE
ubuntu|Ubuntu18.04安装opencv3.4.1+opencv_contrib3.4.1
文章图片

先开始configure,然后修改了,再configure一次,最后再generate,就完成了build文件,在整个build过程中,可能会存在缺失文件。
cd opencv3.4.1/build sudo make sudo make install sudo vim /etc/ld.so.conf.d/opencv.conf 添加: /usr/local/lib

sudo ldconfig#使其保存生效 sudo vim /etc/bash.bashrc

在文件末尾添加:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH

保存退出,刷新一次bashrc
source /etc/bash.bashrc

三、测试是否安装成功 进入opencv/samples/cpp/example_cmake目录下
cmake . make ./opencv_example

运行结果:Hello OpenCV
四 、安装过程中常见问题 1、ubuntu 常见问题系列:E:Could not get lock /var/lib/dpkg/lock-frontend - open
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavialable)
E: Unable to acquire the dpkg fronted lock (/var/lib/dpkg/lock-frontend), is another process using it?
sudo rm /var/lib/dpkg/lock-frontend sudo rm /var/lib/dpkg/lock

2、OpenCV(3.4.1) 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 cvShowImage, file /opt/conda/conda-bld/opencv-suite_1530789967746/work/modules/highgui/src/window.cpp, line 636
Traceback (most recent call last):
File “drawing.py”, line 9, in
cv2.imshow(“Canvas”, canvas)
cv2.error: OpenCV(3.4.1) /opt/conda/conda-bld/opencv-suite_1530789967746/work/modules/highgui/src/window.cpp:636: error: (-2) 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 cvShowImage
解决办法:卸载opencv,然后安装一个两个包,再重新编译
sudo apt-get install libgtk2.0-dev pkg-config

opencv 调用KCF跟随代码
#include #include #include #include #include using namespace std; using namespace cv; void draw_rectangle(int event, int x, int y, int flags, void*); Mat firstFrame; Point previousPoint, currentPoint; Rect2d bbox; int main(int argc, char *argv[]) { cout << 123 << endl; //启用摄像头进行跟踪 VideoCapture capture(0); Mat frame; capture >> frame; //使用事先录好的视频进行检验 //VideoCapture capture; //frame = capture.open("E:\\imagelib\\1.avi"); if (!capture.isOpened()) { printf("can not open ...\n"); return -1; } //获取视频的第一帧,并框选目标 capture.read(firstFrame); if (!firstFrame.empty()) { namedWindow("output", WINDOW_AUTOSIZE); imshow("output", firstFrame); setMouseCallback("output", draw_rectangle, 0); waitKey(); } //使用不同跟踪算法进行跟踪 //Ptr tracker= TrackerMIL::create(); // Ptr tracker= TrackerTLD::create(); Ptr tracker = TrackerKCF::create(); //Ptr tracker = TrackerMedianFlow::create(); //Ptr tracker= TrackerBoosting::create(); capture.read(frame); tracker->init(frame, bbox); namedWindow("output", WINDOW_AUTOSIZE); while (capture.read(frame)) { tracker->update(frame, bbox); rectangle(frame, bbox, Scalar(255, 0, 0), 2, 1); imshow("output", frame); if (waitKey(20) == 'q') return 0; } capture.release(); destroyWindow("output"); return 0; } //框选目标 void draw_rectangle(int event, int x, int y, int flags, void*) { if (event == EVENT_LBUTTONDOWN) { previousPoint = Point(x, y); } else if (event == EVENT_MOUSEMOVE && (flags&EVENT_FLAG_LBUTTON)) { Mat tmp; firstFrame.copyTo(tmp); currentPoint = Point(x, y); rectangle(tmp, previousPoint, currentPoint, Scalar(0, 255, 0, 0), 1, 8, 0); imshow("output", tmp); } else if (event == EVENT_LBUTTONUP) { bbox.x = previousPoint.x; bbox.y = previousPoint.y; bbox.width = abs(previousPoint.x - currentPoint.x); bbox.height = abs(previousPoint.y - currentPoint.y); } else if (event == EVENT_RBUTTONUP) { destroyWindow("output"); }}

【ubuntu|Ubuntu18.04安装opencv3.4.1+opencv_contrib3.4.1】编译命令
g++ test.cpp -o test `pkg-config --cflags --libs opencv`

    推荐阅读