opencv安装
最近配置新的主机,以前虽然在自己的笔记本上配置过许多环境,但是没有记录下来有些步骤也忘了,以后在自己的博客里更新一些环境的配置和软件安装。
下载opencv2.4.13
Opencv Releases
我下载的是opencv2.4.13.6,之前在笔记本上安装的是2.4.11,但是之前碰到过这样的问题:
1. 安装opencv出现error: ‘NppiGraphcutState’ has not been declared :
/modules/gpu/src/graphcuts.cpp:120:54:
error: ‘NppiGraphcutState’ has not been declared typedef NppStatus (*init_func_t)
(NppiSize oSize,NppiGraphcutState** ppState, Npp8u* pDeviceMem);
/modules/gpu/src/graphcuts.cpp:135:18:
error: expected type-specifier before ‘NppiGraphcutState’ operator NppiGraphcutState*()
/graphcuts.cpp: In constructor ‘{anonymous}::NppiGraphcutStateHandler::NppiGraphcutStateHandler(NppiSize, Npp8u*, {anonymous}::init_func_t)’:
/home/relaybot/mumu/opencv/opencv-2.4.11/modules/gpu/src/graphcuts.cpp:127:39: error: ‘pState’ was not declared in this scope
nppSafeCall( func(sznpp, &pState, pDeviceMem) );
^
解决方案:
修改opencv的源码:
在/opencv-2.4.11/modules/gpu/src/graphcuts.cpp:
中将
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
更改为:
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)
其原因是cuda8.0较新,opencv-2.4.11较早,要编译通过需要修改opencv中的源码;
但是最新的opencv2.4.13没这个问题,所以我安装opencv2.4.13.6安装很简单就成功了;
2. 安装了cuda后安装opencv出现:nvcc fatal : Unsupported gpu architecture ‘compute_11’
nvcc fatal : Unsupported gpu architecture 'compute_11'
CMake Error at cuda_compile_generated_matrix_operations.cu.o.cmake:206 (message):
Error generating
解决方案:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..
正确的安装opencv2.4.13.6的方式:
下载好opencv2.4.13.6进行解压
cd opencv-2.4.13.6/
mkdir release
cd release/
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D CUDA_GENERATION=Kepler ..
make -j6
//这里看自己的主机
sudo make install
安装到/usr/local/文件夹下了
测试opencv
新建一个文件夹:
添加imageBasics.cpp
文件:
#include <iostream>
#include <chrono>
#include <vector>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
string imgtest = "../ubuntu.png";
/*
* /imageBasics/build$ cmake ..
* ./imageBasics ../ubuntu.png
* 因为此处的int main() 是有输入的
* */
int main ( int argc, char** argv )
{
// 读取argv[1]指定的图像
cv::Mat image;
// image = cv::imread ( argv[1] ); //cv::imread函数读取指定路径下的图像
image = cv::imread ( imgtest );
// 判断图像文件是否正确读取
if ( image.data == nullptr ) //数据不存在,可能是文件不存在
{
cerr<<"文件"<<argv[1]<<"不存在."<<endl;
return 0;
}
// 文件顺利读取, 首先输出一些基本信息
cout<<"图像宽为"<<image.cols<<",高为"<<image.rows<<",通道数为"<<image.channels()<<endl;
cv::imshow ( "image", image ); // 用cv::imshow显示图像
cv::waitKey ( 0 ); // 暂停程序,等待一个按键输入
// 判断image的类型
if ( image.type() != CV_8UC1 && image.type() != CV_8UC3 )
{
// 图像类型不符合要求
cout<<"请输入一张彩色图或灰度图."<<endl;
return 0;
}
// 遍历图像, 请注意以下遍历方式亦可使用于随机像素访问
// 使用 std::chrono 来给算法计时
chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
for ( size_t y=0; y<image.rows; y++ )
{
for ( size_t x=0; x<image.cols; x++ )
{
// 访问位于 x,y 处的像素
// 用cv::Mat::ptr获得图像的行指针
unsigned char* row_ptr = image.ptr<unsigned char> ( y ); // row_ptr是第y行的头指针
unsigned char* data_ptr = &row_ptr[ x*image.channels() ]; // data_ptr 指向待访问的像素数据
// 输出该像素的每个通道,如果是灰度图就只有一个通道
for ( int c = 0; c != image.channels(); c++ )
{
unsigned char data = data_ptr[c]; // data为I(x,y)第c个通道的值
}
}
}
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>( t2-t1 );
cout<<"遍历图像用时:"<<time_used.count()<<" 秒。"<<endl;
// 关于 cv::Mat 的拷贝
// 直接赋值并不会拷贝数据
cv::Mat image_another = image;
// 修改 image_another 会导致 image 发生变化
image_another ( cv::Rect ( 0,0,100,100 ) ).setTo ( 0 ); // 将左上角100*100的块置零
cv::imshow ( "image", image );
cv::waitKey ( 0 );
// 使用clone函数来拷贝数据 消耗时间
cv::Mat image_clone = image.clone();
image_clone ( cv::Rect ( 0,0,100,100 ) ).setTo ( 255 );
cv::imshow ( "image", image );
cv::imshow ( "image_clone", image_clone );
cv::waitKey ( 0 );
// 对于图像还有很多基本的操作,如剪切,旋转,缩放等,限于篇幅就不一一介绍了,请参看OpenCV官方文档查询每个函数的调用方法.
cv::destroyAllWindows();
return 0;
}
编写CMakeLists.txt文件
cmake_minimum_required( VERSION 2.8 )
project( imageBasics )
# 添加c++ 11标准支持
set( CMAKE_CXX_FLAGS "-std=c++11" )
# 寻找OpenCV库
find_package( OpenCV REQUIRED )
# 添加头文件
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( imageBasics imageBasics.cpp )
# 链接OpenCV库
target_link_libraries( imageBasics ${OpenCV_LIBS} )
放上一张图片ubuntu.png
打开终端进入此文件夹:
mkdir build
cmake ..
make
./imageBasics
图像宽为1200,高为674,通道数为3
遍历图像用时:0.0528485 秒。
OpenCV Installation in Linux
依赖包:
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
Configuring. Run cmake [ <some optional parameters>
]<path to the OpenCV source directory>
For example
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
or cmake-gui
set full path to OpenCV source code, e.g. /home/user/opencv
set full path to <cmake_build_dir>, e.g. /home/user/opencv/build
set optional parameters
run: “Configure”
run: “Generate”
Note
Usecmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
, without spaces after -D if the above example doesn’t work.
Description of some parameters
build type: CMAKE_BUILD_TYPE=Release\Debug
to build with modules from opencv_contrib set OPENCV_EXTRA_MODULES_PATH to <path to opencv_contrib/modules/>
set BUILD_DOCS for building documents
set BUILD_EXAMPLES to build all examples
[optional] Building python. Set the following python parameters:
PYTHON2(3)_EXECUTABLE = <path to python>
PYTHON_INCLUDE_DIR = /usr/include/python<version>
PYTHON_INCLUDE_DIR2 = /usr/include/x86_64-linux-gnu/python<version>
PYTHON_LIBRARY = /usr/lib/x86_64-linux-gnu/libpython<version>.so
PYTHON2(3)_NUMPY_INCLUDE_DIRS = /usr/lib/python<version>/dist-packages/numpy/core/include/
[optional] Building java.
Unset parameter: BUILD_SHARED_LIBS
It is useful also to unset BUILD_EXAMPLES, BUILD_TESTS, BUILD_PERF_TESTS - as they all will be statically linked with OpenCV and can take a lot of memory.