卸载
1. $ sudo make uninstall
2. $ cd ..
3. $ sudo rm -r build
4. $ 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*
推荐下面第二段安装opencv-3.4.9的版本
1.先要提前安装依赖库
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
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
一般是源的问题,请设置为阿里或清华源
2.将opencv源码库拷贝到系统中,解压;
opencv-3.4.6.tar.gz 库下载:
链接:https://pan.baidu.com/s/1IhukVJVe0NJtvb7voRqkkw
提取码:8wy4
3. 在源码文件夹中,新建build目录,进到build目录运行cmake;
安装目录 /home/sukai/opencv_libs 自己另外指定;
注意3.2 末尾的两个点 .. 不能少
控制台运行
3.1 mkdir build cd build
3.2 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/home/sukai/opencv_libs -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_OPENGL=ON ..
3.3 make
3.4 安装 安装完成后才能看到在这个 /home/sukai/目录下看到 opencv_libs 文件夹
sudo make install
4.Clion中添加依赖
4.1
打开clion 在当前工程下新建一个cmake文件夹,将 FindOpenCV.cmake 文件拷贝进去
改下安装opencv的路径 /home/sukai/opencv_libs
FindOpenCV.cmake 文件:
# once done, this will define
#OpenCV_FOUND - whether there is OpenCV in the prebuilt libraries
#OpenCV_INCLUDE_DIRS - include directories for OpenCV
#OpenCV_LIBRARY_DIRS - library directories for OpenCV
#OpenCV_LIBRARIES - link this to use OpenCV
unset(OpenCV_FOUND)
set(PREBUILT_DIR /home/sukai/opecv_libs)
set(OpenCV_DIR ${PREBUILT_DIR})
MESSAGE(STATUS "TEST ${PREBUILT_DIR}")
set(OpenCV_INCLUDE_DIRS ${OpenCV_DIR}/include)
SET(OpenCV_LIB_COMPONENTS opencv_videostab;opencv_video;opencv_superres;opencv_stitching;opencv_photo;
opencv_objdetect;opencv_ml;opencv_imgproc;opencv_highgui;opencv_flann;opencv_features2d;
opencv_core;opencv_calib3d;opencv_imgcodecs)
find_path(OpenCV_INCLUDE_DIRS NAMES opencv.h HINTS ${OpenCV_DIR}/include NO_SYSTEM_ENVIRONMENT_PATH)
set(OpenCV_LIBRARY_DIRS ${OpenCV_DIR}/lib)
FOREACH(cvcomponent ${OpenCV_LIB_COMPONENTS})
find_library(lib_${cvcomponent} NAMES ${cvcomponent} HINTS ${OpenCV_DIR}/lib NO_DEFAULT_PATH)
set(OpenCV_LIBRARIES ${OpenCV_LIBRARIES};${lib_${cvcomponent}})
ENDFOREACH()
set(OpenCV_LIBS ${OpenCV_LIBRARIES})
set(OpenCV_INCLUDE_DIRS ${OpenCV_INCLUDE_DIRS};${OpenCV_INCLUDE_DIRS}/opencv)
if (OpenCV_INCLUDE_DIRS AND OpenCV_LIBRARIES)
set(OpenCV_FOUND TRUE)
endif (OpenCV_INCLUDE_DIRS AND OpenCV_LIBRARIES)
if (OpenCV_FOUND)
if (NOT OpenCV_FIND_QUIETLY)
message(STATUS "Found OpenCV: ${OpenCV_LIBRARIES}")
endif (NOT OpenCV_FIND_QUIETLY)
else(OpenCV_FOUND)
if (OpenCV_FIND_REQUIRED)
message(FATAL_ERROR "Could not find the OpenCV library")
endif ()
endif (OpenCV_FOUND)
# 导入opencv相关的头文件
include_directories(${OpenCV_INCLUDE_DIRS})
4.2 cmakeList.txt 配置文件
cmakeList.txt 文件
cmake_minimum_required(VERSION 3.14)
project(CalibrationCamera)
set(CMAKE_CXX_STANDARD 14)
set(OUTPUT_DIRECTORY_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/build) # ${CMAKE_BUILD_TYPE}
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${OUTPUT_DIRECTORY_ROOT}/bin" CACHE PATH "Runtime directory" FORCE)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${OUTPUT_DIRECTORY_ROOT}/lib" CACHE PATH "Library directory" FORCE)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${OUTPUT_DIRECTORY_ROOT}/lib" CACHE PATH "Archive directory" FORCE)
# 设置CMAKE模块路径
#set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ~/3rdparty/OpenCV-3.4.9/share/OpenCV)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(CalibrationCamera main.cpp)
add_subdirectory(src)
-----------------------------------------------------------------
参考:https://docs.opencv.org/3.4.1/d7/d9f/tutorial_linux_install.html
下载opencv库
cd ~/
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
=========================================================================================
推荐安装这个:
链接:https://pan.baidu.com/s/1qaUe1BUJwPJlTrwgpVOPUA
提取码:pzjr
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libv4l-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 gtk-3.0-dev
sudo apt-get install libatlas-base-dev gfortran
mkdir ~/opencvenv
cd ~/opencvenv
git clone -b 3.4 https://github.com/opencv/opencv.git git clone -b 3.4 https://github.com/opencv/opencv_contrib.git
opencv-3.4.9.zip opencv_contrib.tar.gz
```
提前将.cache目录解压到源码根目录:
把这个cache.tar.gz 拷贝到
opencven 文件夹下
在当前opencven 文件夹下解压运行
tar -zxvf cache.tar.gz -C opencv-3.4.9/
保证源码目录中有.cache
在后边执行cmake时候, 会自动检查本地是否有这些文件, 如果有, 则不再联网下载.
cd opencv-3.4.9
mkdir build
cd build
注意末尾的两个点 .. 不能少
OPENCV_EXTRA_MODULES_PATH=/home/sk/opencvenv/opencv_contrib-3.4.9/modules 指定的是第三方源码目录
拷贝执行以下其中一条:
1.需要改自己的目录 /home/sukai/3rdparty/OpenCV-3.4.9
cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D INSTALL_PYTHON_EXAMPLES=ON -D CMAKE_INSTALL_PREFIX=/home/sk/3rdparty/OpenCV-3.4.9 -D OPENCV_EXTRA_MODULES_PATH=/home/sk/opencvenv/opencv_contrib-3.4.9/modules -D BUILD_EXAMPLES=ON ..
2.不需要改目录 ~/3rdparty/OpenCV-3.4.9
cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=ON -D INSTALL_PYTHON_EXAMPLES=ON -D CMAKE_INSTALL_PREFIX=~/3rdparty/OpenCV-3.4.9 -D OPENCV_EXTRA_MODULES_PATH=~/opencvenv/opencv_contrib-3.4.9/modules -D BUILD_EXAMPLES=ON ..
cmake -D CMAKEBUILDTYPE=Release \ -D BUILDopencvpython2=OFF \ -D BUILDopencvpython3=ON ..
cmake -D CMAKEBUILDTYPE=Release \ -D BUILDopencvpython2=OFF \ -D BUILDopencvpython3=ON \ -D CMAKEINSTALLPREFIX=/usr/local \ -D INSTALLPYTHONEXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/sk/opencvenv/opencv_contrib-3.4.9/modules \ -D BUILDEXAMPLES=ON ..
cmake -D CMAKEBUILDTYPE=Release \ -D BUILDopencvpython2=OFF \ -D BUILDopencvpython3=ON \ -D INSTALLPYTHONEXAMPLES=ON \ -D CMAKEINSTALLPREFIX=~/3rdparty/OpenCV-3.4.9 \ -D OPENCV_EXTRA_MODULES_PATH=/home/sk/opencvenv/opencv_contrib-3.4.9/modules \ -D BUILDEXAMPLES=ON ..
cmake -D CMAKE_BUILD_TYPE=Release \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_python3=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D CMAKE_INSTALL_PREFIX=~/3rdparty/OpenCV-3.4.9 \
-D OPENCV_EXTRA_MODULES_PATH=/home/sk/opencvenv/opencv_contrib-3.4.9/modules \
-D BUILD_EXAMPLES=ON ..
make -j8
sudo make install
建议在直接将cache.tar.gz内容解压缩到源码根目录下的.cache,即可避免由于需要在线下载会出现的问题。
1. ippicv
由于是外国服务器原因,这里极大概率会出现下载错误,会提示ippicv_2019_lnx_intel64_general_20180723.tgz
下载失败!!解决方式如下
方式1:推荐
将ippicv_2019_lnx_intel64_general_20180723.tgz
拷贝为opencv/.cache/ippicv/c0bd78adb4156bbf552c1dfe90599607-ippicv_2019_lnx_intel64_general_20180723.tgz
即可
方式2:
```shell
gedit ~/opencv_env/opencv/3rdparty/ippicv/ippicv.cmake #就是这个文件的路径
"https://raw.githubusercontent.com/opencv/opencv3rdparty/${IPPICVCOMMIT}ippicv/"
"file:///home/itheima/opencv_env/" #(仅供参考,根据自己的路径填写,ippicv所在的目录) ```
2. facelandmarkmodel.dat
cmake过程中会到https://raw.githubusercontent.com/opencv/opencv3rdparty/8afa57abc8229d611c4937165d20e2a2d9fc5a12/facelandmark_model.dat下载dat文件,这里提前准备好,就不需要联网下啦,进入opencv源码根目录,执行:
``` mkdir -p .cache/data
cp facelandmarkmodel.dat .cache/data/7505c44ca4eb54b4ab1e4777cb96ac05-facelandmarkmodel.dat ```
3. opencv boostdesc_bgm.i
opencv boostdesc_bgm.i: No such file or directory
查看下载失败的文件
shell opencv-3.4.9/build$ cat CMakeDownloadLog.txt
直接自行下载到指定目录,或将一下文件拷贝到指定目录
4.Clion中添加依赖
4.1
打开clion 在当前工程下新建一个cmake文件夹,将 FindOpenCV.cmake 文件拷贝进去
改下安装opencv的路径 /home/sukai/3rdparty/OpenCV-3.4.9
FindOpenCV.cmake 文件:
# once done, this will define
#OpenCV_FOUND - whether there is OpenCV in the prebuilt libraries
#OpenCV_INCLUDE_DIRS - include directories for OpenCV
#OpenCV_LIBRARY_DIRS - library directories for OpenCV
#OpenCV_LIBRARIES - link this to use OpenCV
set(OpenCV_DIR ~/3rdparty/OpenCV-3.4.9)
unset(OpenCV_FOUND)
MESSAGE(STATUS "OpenCV_DIR TEST ${PREBUILT_DIR}")
set(OpenCV_INCLUDE_DIRS "${OpenCV_DIR}/include" "${OpenCV_DIR}/include/opencv")
set(OpenCV_LIB_COMPONENTS opencv_core;opencv_ml;opencv_calib3d;opencv_highgui;opencv_superres;opencv_photo;
opencv_imgcodecs;opencv_features2d;opencv_flann;opencv_shape;opencv_stitching;opencv_dnn;
opencv_videostab;opencv_imgproc;opencv_objdetect;opencv_video;opencv_videoio;opencv_reg;opencv_aruco;
opencv_dpm;opencv_xobjdetect;opencv_xfeatures2d;opencv_surface_matching;opencv_plot;opencv_hfs;
opencv_tracking;opencv_rgbd;opencv_text;opencv_dnn_objdetect;opencv_face;opencv_optflow;opencv_bgsegm;
opencv_bioinspired;opencv_ximgproc;opencv_saliency;opencv_freetype;opencv_stereo;opencv_img_hash;
opencv_fuzzy;opencv_ccalib;opencv_line_descriptor;opencv_datasets;opencv_phase_unwrapping;
opencv_xphoto;opencv_structured_light)
find_path(OpenCV_INCLUDE_DIRS NAMES opencv.h HINTS ${OpenCV_DIR}/include NO_SYSTEM_ENVIRONMENT_PATH)
set(OpenCV_LIBRARY_DIRS ${OpenCV_DIR}/lib)
FOREACH(cvcomponent ${OpenCV_LIB_COMPONENTS})
find_library(lib_${cvcomponent} NAMES ${cvcomponent} HINTS ${OpenCV_DIR}/lib NO_DEFAULT_PATH)
set(OpenCV_LIBRARIES ${OpenCV_LIBRARIES};${lib_${cvcomponent}})
ENDFOREACH()
set(OpenCV_LIBS ${OpenCV_LIBRARIES})
set(OpenCV_INCLUDE_DIRS ${OpenCV_INCLUDE_DIRS};${OpenCV_INCLUDE_DIRS}/opencv)
if (OpenCV_INCLUDE_DIRS AND OpenCV_LIBRARIES)
set(OpenCV_FOUND TRUE)
endif (OpenCV_INCLUDE_DIRS AND OpenCV_LIBRARIES)
if (OpenCV_FOUND)
if (NOT OpenCV_FIND_QUIETLY)
message(STATUS "Found OpenCV: ${OpenCV_LIBRARIES}")
endif (NOT OpenCV_FIND_QUIETLY)
else(OpenCV_FOUND)
if (OpenCV_FIND_REQUIRED)
message(FATAL_ERROR "Could not find the OpenCV library")
endif ()
endif (OpenCV_FOUND)
4.2 cmakeList.txt 配置文件
cmakeList.txt 文件
cmake_minimum_required(VERSION 3.16)
project(opencv)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(OpenCV REQUIRED)
add_executable(opencv_cpp main.cpp)
target_link_libraries(opencv_cpp ${OpenCV_LIBS})
add_executable(zfngtu 04_图像直方图.cpp)
target_link_libraries(zfngtu ${OpenCV_LIBS})
数据类型定义
```c++ CV_位数(S|U|F)C通道数量 位数: 8位 , 16位, 32位 ,64位 单通道颜色取值范围是0-255,所以通常是8位
S|U|F: S--代表---signed int---有符号整形 U--代表--unsigned int--无符号整形 F--代表--float---------单精度浮点型
C通道数量: 通道数量 = 1:灰度图片,单通道图像 通道数量 = 3:灰度图片,单通道图像 通道数量 = 4:带Alph通道的RGB图像,png图片是这种
```
===========================================================================================
ps:ros系统python2.7与 python3.7安装 OpenCV有冲突所以使用anaconda创建虚拟环境,在虚拟环境中使用opencv-3.4.6
1. ubuntu 把pip也换成国内源
mkdir ~/.pip
cd ~/.pip
touch pip.conf
sudo nano ~/.pip/pip.conf
打开pip.conf 文件后,添加以下内容:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn
更新一下:
sudo apt-get update
2.下载 与安装anaconda
自己百度! 或用我准备好的;
链接:https://pan.baidu.com/s/1rs4rpQV78LuLhk-4O687ew
提取码:e6bc
3.使用anaconda创建虚拟环境 2和3版本都可以,但是安装opencv时要注意版本号;
# 1.
export PATH=~/anaconda3/bin:$PATH
或 配置环境变量
echo "export PATH=~/anaconda3/bin:\$PATH" >> ~/.bashrc
# 2. 创建虚拟环境
conda create -n opencvenv python=3.7
安装这一步时会报以下错 :
Solving environment: failed
# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<
`$ /home/sk/anaconda3/bin/conda create -n opencvenv python=3.7`
environment variables:
CIO_TEST=
CMAKE_PREFIX_PATH=/opt/ros/melodic
CONDA_ROOT=/home/sk/anaconda3
这是因为anaconda3文件没有权限 ,给它个权限,如果还是报这个错就多执行几次这命令,因为有可能对方服务器没及时响应
sudo chmod 777 -R anaconda3
# 3. 激活新创建的环境
source activate
source deactivate
conda activate opencvenv
# 4. 安装opencv-python
conda install opencv==3.4.2
# 5. 这时我们还需要安装扩展包
pip install opencv-contrib-python==3.4.2.17
上面报错就用下面这行 后面的版本跟上; 不跟版本默认最新的,最新的没试过可能存在问题
pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-contrib-python==3.4.2.17
# 6.
sudo apt-get install libcanberra-gtk-module
# 7. 可能会出现下面问题:
QObject::moveToThread: Current thread (0x55fd81f67a60) is not the object's thread (0x55fd820ef150). Cannot move to target thread (0x55fd81f67a60)
切换到自己的 虚拟环境中
conda info -e 查看自己的环境
conda activate python_env 切换到需要的环境中然后执行下面的命令
执行: conda install pyqt 可以就绝上述问题
pycharm中如果编写代码没有提示,可以考虑将工程下面的.idea文件删除,再重新打开pycharm
# 采用这种方式安装,在当前版本中会出现如下异常
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'
如果 报conda 不存在:
1.控制台执行这个 export PATH=~/anaconda3/bin:$PATH
或
修改环境变量
vim ~/.bashrc
在最后添加:
export PATH=~/anaconda3/bin:$PATH
重启环境变量:
source ~/.bashrc
2.
也可以在pychram 中的控制台中执行,但前提是要配编译环境,编译环境配置在最下面
pycharm 添加环境
运行 cv.imshow("src",imwrite) 显示以下黑框原因是你安装了 4.3 版本的openCV,安装时一定要跟上版本号;
控制台报错:
QObject::moveToThread: Current thread (0x56175a10a400) is not the object's thread (0x56175a2abbe0).
Cannot move to target thread (0x56175a10a400)
执行:
尝试以下 一个一个试不要一次性都执行
切换到自己的 虚拟环境中
conda info -e 查看自己的环境
conda activate python_env 切换到需要的环境中然后执行下面的命令
conda install pyqt
sudo pip uninstall opencv-python
sudo pip install libopencv-dev opencv-python
sudo apt-get install libopencv-dev opencv-python
以下安装notebook看自己需求,需要就安装notebook
安装jupyter notebook
conda install jupyter notebook
安装jupyter notebook的代码提示功能
pip install jupyter_contrib_nbextensions
//上一行报错就用这行代替
pip install --user -i https://pypi.tuna.tsinghua.edu.cn/simple jupyter_contrib_nbextensions
jupyter contrib nbextension install --user --skip-running-check
启动jupyter notebook
jupyter notebook
点开 Nbextensions 的选项,并勾选 Hinterland